轉(zhuǎn)帖|其它|編輯:郝浩|2009-01-14 11:29:26.000|閱讀 1003 次
概述:JMX獲得web service的信息
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
文章關鍵字:|JMX|web service|JBOSS|Jetty|
一, JBOSS:
首先得得到RMIAdaptor,下面是獲取RMIAdaptor的方法。
private RMIAdaptor connect2Jboss(String host, int port, String userName, String password, int timeout,int retryTimes) {
RMIAdaptor mbsc = null;
for(int i=0;i< retryTimes;i++){
Properties pro = new Properties();
pro.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
pro.setProperty("java.naming.provider.url", "jnp://" + host + ":" + port);
pro.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
pro.put("jnp.timeout", String.valueOf(timeout * 1000));
pro.put("jnp.sotimeout", String.valueOf(timeout * 1000));
try {
ic = new InitialContext(pro);
mbsc = (RMIAdaptor) ic.lookup("jmx/rmi/RMIAdaptor");
return mbsc;
} catch (Exception e) {
logger.info("Host:[" + host + "] Port:[" + port + "] User:[" + userName + "] Password:[" + password + "] Timeout:[" + timeout + "] Error");
}
}
return mbsc;
}
得到RMIAdaptor后,就可以得到相關的MBean了。例如,想得到server的相關信息,它的ObjectName是:jboss.system:type=Server。
Set mbSet = rmiAdaptor.queryMBeans(new ObjectName("jboss.system:type=Server"), null);
for (Iterator it = mbSet.iterator(); it.hasNext();) {
ObjectInstance oi = (ObjectInstance) it.next();
String version = rmiAdaptor.getAttribute(oi.getObjectName(), "Version").toString();
jbossInfo.setVersion(version);
jbossInfo.setAvailable(true);
}
得到的mbSet是一系列屬性Set,通過遍歷就可以得到它相關的信息。
想要得到jboss下所有的web應用,就可以通過ObjectName:jboss.web.deployment:*。
mbSet = rmiAdaptor.queryMBeans(new ObjectName("jboss.web.deployment:*"), null);[SPAN]
二, Jetty
下面的方法是探測Red5運行情況,Red5是整合了Jetty服務器的一起發(fā)布的。它是開源的P2P flash服務器。
public Red5AppInfo getRed5AppInfo(String host, int port, String objectNameStr, String username, String password, int TimeOut, int retryTimes){
Red5AppInfo red5AppInfo = new Red5AppInfo();
objectNameStr = "org.red5.server:type=org.red5.server.WebScope,name=admin";
String jmxServiceUrl = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/red5";
/*try {
Thread.sleep(10000000l);
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}*/
MBeanInfo mBeanInfo = null;
for (int i = 0; i < retryTimes; i++) {
Date date = null;
try {
Map environment = new HashMap();
String[] credentials = new String[]{username, password};
environment.put(JMXConnector.CREDENTIALS, credentials);
JMXServiceURL address = new JMXServiceURL(jmxServiceUrl);
Date startDate = new Date();
date = new Date();
// JMXConnector jmxConnector = JMXConnectorFactory.connect(address, environment);
JMXConnector jmxConnector = JmxConnection.connectWithTimeout(address, username, password, TimeOut, TimeUnit.SECONDS);
MBeanServerConnection mbServerConn = jmxConnector.getMBeanServerConnection();
Date endDate = new Date();
System.out.println("DefaultDomain: " + mbServerConn.getDefaultDomain());
ObjectName oname = new ObjectName(objectNameStr);
mBeanInfo = mbServerConn.getMBeanInfo(oname);
MBeanAttributeInfo[] mBeanAttributeInfos = mBeanInfo.getAttributes();
for (MBeanAttributeInfo mBeanAttributeInfo : mBeanAttributeInfos) {
if (mBeanAttributeInfo.getName().equals("Running")) {
red5AppInfo.setAvailable(mBeanAttributeInfo.isIs());
red5AppInfo.setResponseTime(endDate.getTime() - startDate.getTime());
}
}
} catch (InstanceNotFoundException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
return red5AppInfo;
}
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:IT專家網(wǎng)