彩票走势图

JMX获得web service的信息

转帖|其它|编辑:郝浩|2009-01-14 11:29:26.000|阅读 990 次

概述: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(&quot;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:*&quot;), null);[SPAN]

  二, 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;

  }


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:IT专家网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP