JMX远程调用封装

luozhangwen 2010-04-24 10:00:20

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;
}
...全文
293 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
heting1024 2010-04-24
  • 打赏
  • 举报
回复
luozhangwen 2010-04-24
  • 打赏
  • 举报
回复


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);
}
}

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧