web服务——如何统计部署在Glassfish 3.1上的web服务调用



如何统计部署在Glassfish 3.1上的Webservices调用?实际上我可以在控制台命令中得到我想要的?如下

asadmin get -m "server.applications.hello-jaxws2*" server.applications.hello-jaxws2.2.server.Hello.requestcount-count = 14

但是我想知道是否有一种方法可以以编程方式获得web服务的调用计数?

以使用Glassfish 3.1.2为例。和名为"NewWebService"的Web服务,这里是检索该Web服务请求数的代码摘录。

public static void showRequestCount(MBeanServerConnection mbs) throws Exception {
    ObjectName on = new ObjectName("amx:pp=/mon/server-mon[server],type=servlet-instance-mon,name=WebApplication1/server/NewWebService");
    final Set<ObjectInstance> mBeans = mbs.queryMBeans(on, null);
    for (ObjectInstance mbean : mBeans) {
        System.out.println("mbean: " + mbean);
        final MBeanInfo info = mbs.getMBeanInfo(on);
        final MBeanAttributeInfo[] attributes = info.getAttributes();
        for (int i = 0; i < attributes.length; i++) {
            MBeanAttributeInfo mBeanAttributeInfo = attributes[i];
            if (mBeanAttributeInfo.getName().equals("requestcount")) {
                final Object attribute = mbs.getAttribute(on, mBeanAttributeInfo.getName());
                CompositeDataSupport cds = (CompositeDataSupport) attribute;
                final Object requestCount = cds.get("count");
                System.out.println("Object name: " + on.getKeyProperty("name"));
                System.out.println("Request count: " + requestCount);
            }
        }
    }
}

结果是:

mbean: servlet-instance-mon[amx:pp=/mon/server-mon[server],type=servlet-instance-mon,name=WebApplication1/server/NewWebService]
Object name: WebApplication1/server/NewWebService
Request count: 18

请注意,MBean的ObjectName和/或其属性可能因您的Glassfish版本而异。

最新更新