23,404
社区成员
发帖
与我相关
我的任务
分享
public class JMXHelper {
private static final Logger log = new Logger();
private JMXConnector jmxConnector;
public MBeanServerConnection getConnection(final String host, final int port) {
final int timer = 5;
final int retry = 10;
final int sleep = 1000 * 30;
String jmxServer = host;
JMXServiceURL address;
try {
address = new JMXServiceURL("service:jmx:rmi://" + jmxServer + ":"
+ port + "/jndi/jrmp");
} catch (MalformedURLException e) {
log.error(e.getMessage(), e);
return null;
}
Map environment = new HashMap();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.rmi.registry.RegistryContextFactory");
environment
.put(Context.PROVIDER_URL, "rmi://" + jmxServer + ":" + port);
MBeanServerConnection connection = null;
for (int i = 0; i < retry + 1; i++) {
try {
jmxConnector = connectWithTimeout(address, environment, timer,
TimeUnit.SECONDS);
break;
} catch (Exception e) {
if (i < retry) {
try {
Thread.sleep(sleep);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
log.error(e1.getMessage(), e1);
}
continue;
} else {
log.error(e.getMessage(), e);
return null;
}
}
}
if (jmxConnector == null) {
return null;
}
try {
connection = jmxConnector.getMBeanServerConnection();
} catch (IOException e) {
if (jmxConnector != null) {
try {
jmxConnector.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
log.error(e1.getMessage(), e);
}
}
log.error(e.getMessage(), e);
return null;
}
return connection;
}
public MBeanServerConnection getConnection(final String host,
final int port, final String username, final String password) {
String jmxServer = host;
final int timer = 5;
final int retry = 10;
final int sleep = 1000 * 30;
JMXServiceURL address;
try {
address = new JMXServiceURL("service:jmx:rmi://" + jmxServer + ":"
+ port + "/jndi/jrmp");
} catch (MalformedURLException e) {
log.error(e.getMessage(), e);
return null;
}
Map environment = new HashMap();
String[] credentials = new String[] { username, password };
environment.put("jmx.remote.credentials", credentials);
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.rmi.registry.RegistryContextFactory");
environment
.put(Context.PROVIDER_URL, "rmi://" + jmxServer + ":" + port);
MBeanServerConnection connection = null;
for (int i = 0; i < retry + 1; i++) {
try {
jmxConnector = connectWithTimeout(address, environment, timer,
TimeUnit.SECONDS);
break;
} catch (Exception e) {
if (i < retry) {
try {
Thread.sleep(sleep);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
log.error(e1.getMessage(), e1);
}
continue;
} else {
log.error(e.getMessage(), e);
return null;
}
}
}
if (jmxConnector == null) {
return null;
}
try {
connection = jmxConnector.getMBeanServerConnection();
} catch (IOException e) {
log.error(e.getMessage(), e);
if (jmxConnector != null) {
try {
jmxConnector.close();
} catch (IOException e1) {
log.error("close jmx connection error!");
log.error(e1.getMessage(), e);
}
}
return null;
}
return connection;
}
private JMXConnector connectWithTimeout(final JMXServiceURL url,
final Map environment, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<JMXConnector> future = executor
.submit(new Callable<JMXConnector>() {
public JMXConnector call() throws IOException {
return JMXConnectorFactory.connect(url, environment);
}
});
JMXConnector connect = future.get(timeout, unit);
executor.shutdown();
return connect;
}
public Object executeMethod(MBeanServerConnection mbsConnection,
ObjectName name, String operationName, Object params[],
String signature[]) throws RemoteException {
try {
return mbsConnection.invoke(name, operationName, params, signature);
} catch (Exception e) {
if (e instanceof RemoteException) {
throw (RemoteException) e;
} else {
throw new RemoteException("IOException at call jmx.", e);
}
}
}
public Object executeGetMethod(MBeanServerConnection mbsConnection,
ObjectName name, String attributeName) throws RemoteException {
try {
return mbsConnection.getAttribute(name, attributeName);
} catch (Exception e) {
if (e instanceof RemoteException) {
throw (RemoteException) e;
} else {
throw new RemoteException("IOException at call jmx.", e);
}
}
}
public void close() {
if (jmxConnector != null) {
try {
jmxConnector.close();
} catch (IOException e) {
log.error("the connection cannot be closed cleanly.", e);
}
}
}
public ObjectName getONByProjectName(String name) {
ObjectName projectName;
try {
projectName = new ObjectName("CruiseControl Project:name=" + name);
return projectName;
} catch (MalformedObjectNameException e) {
// TODO Auto-generated catch block
log.error(e.getMessage(), e);
return null;
}
}
}
class Logger {
public void error(String msg) {
System.out.println(msg);
}
public void error(String msg, Exception e) {
System.out.println(msg + e);
}
}