通过JMX来获得webservice的信息
下面的方法是探测Red5运行情况,Red5是整合了Jetty服务器的一起发布的。它是开源的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;
}