程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 通過JMX來獲得web service的信息

通過JMX來獲得web service的信息

編輯:關於JAVA

 我們可以通過JMX來獲得web service的相關配置屬性,內存使用、性能、以及部署在web service下的webapp等信息。

這在很多監控web service系統中往往都要用到。

一, 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

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

二, Jetty

下面的方法是探測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;

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved