Hi,

In this demonstration we will see how we can query the Statistics of a WebService deployed on JBoss using standard MBean code. In this example we will see that we need not to add any JAVA_OPTS in our JBoss configuration files in order to access these MBeans.

Here we will use a Sample webservice which will be deployed on JBoss AS7, for example you can refer to the following article to know how to deploy the WebService in JBoss: “http://middlewaremagic.com/jboss/?p=980” We will try to access the statistics of this webservice using our Standard JMX code. The statistics will include the following useful informations: “StartTime”, “StopTime”, “MinProcessingTime”, “AverageProcessingTime”, “MaxProcessingTime”, “RequestCount”..etc

JBoss side Configuration ( jboss-as-7.1.0.Beta1 )

Step1). Open the “/home/userone/jboss-as-7.1.0.Beta1/standalone/configuration/standalone-full.xml” file and edit the subsystem “urn:jboss:domain:jmx:1.1” as following:

        <subsystem xmlns="urn:jboss:domain:jmx:1.1" show-model="true">
            <jmx-connector registry-binding="jmx-connector-registry" server-binding="jmx-connector-server"/>
        </subsystem>

Step2). Restart your JBoss Profile from “/home/userone/jboss-as-7.1.0.Beta1/bin” directory like following:

[userone@localhost bin]$ ./standalone.sh -c standalone-full.xml

Step3). Deploy your WebService on JBoss. For Testing you can use the webService mentioned in the following link:
http://middlewaremagic.com/jboss/?p=980

Step4). Now Somewhere in your filesystem write a Java Program “WebServiceMonitor.java” as following:

import java.util.Date;
import java.util.Hashtable;
import java.io.IOException;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.lang.management.MemoryMXBean;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import javax.management.*;
import javax.management.openmbean.CompositeDataSupport;
 
public class WebServiceMonitor
{
    private static MBeanServerConnection connection;
    private static JMXConnector connector;
    public static void Connection(String hostname, String port) throws IOException
    {
        Integer portInteger = Integer.valueOf(port);
        Hashtable h = new Hashtable();
        JMXServiceURL address = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+hostname+":"+port+"/jmxrmi");
        connector = JMXConnectorFactory.connect(address,null);
        connection = connector.getMBeanServerConnection();
        System.out.println("GOT THE MBeanServerConnection---SUCCESSFULLY");
    }
 
    private static void getServiceDetail(String contextRoot,String endPointName) throws Exception
    {
        ObjectName serviceRef=new ObjectName("jboss.ws:context="+contextRoot+",endpoint="+endPointName);
        String address=(String)connection.getAttribute(serviceRef, "Address");
        System.out.println("t Address : "+address);
        Date startTime=(Date)connection.getAttribute(serviceRef, "StartTime");
        System.out.println("t StartTime : "+startTime);
        Long minProcessingTime=(Long)connection.getAttribute(serviceRef, "MinProcessingTime");
        System.out.println("t MinProcessingTime : "+minProcessingTime);
        Long maxProcessingTime=(Long)connection.getAttribute(serviceRef, "MaxProcessingTime");
        System.out.println("t MaxProcessingTime : "+maxProcessingTime);
        Long totalProcessingTime=(Long)connection.getAttribute(serviceRef, "TotalProcessingTime");
        System.out.println("t TotalProcessingTime : "+totalProcessingTime);
        Long requestCount=(Long)connection.getAttribute(serviceRef, "RequestCount");
        System.out.println("t RequestCount : "+requestCount);
        Long responseCount=(Long)connection.getAttribute(serviceRef, "ResponseCount");
        System.out.println("t ResponseCount : "+responseCount);
        Long faultCount=(Long)connection.getAttribute(serviceRef, "FaultCount");
        System.out.println("t FaultCount : "+faultCount);
        Date stopTime=(Date)connection.getAttribute(serviceRef, "StopTime");
        System.out.println("t StopTime : "+stopTime);
        Long averageProcessingTime=(Long)connection.getAttribute(serviceRef, "AverageProcessingTime");
        System.out.println("t AverageProcessingTime : "+averageProcessingTime);
    }
 
    public static void main(String[] args) throws Exception
    {
    String hostname = args[0];
    String port = args[1];
    String contextRoot=args[2];
    String endPointName=args[3];
    Connection(hostname, port);
    System.out.println("------------------Getting Service Details------------------");
    System.out.println("jboss.ws:context=CXFTimeoutClientDemo,endpoint=Test_CXF_WS");
    System.out.println("-----------------------------------------------------------");
    getServiceDetail(contextRoot,endPointName);

    // ObjectName will be in the following format
    // "jboss.ws:context="+contextRoot+",endpoint="+endPointName
    //       Example:
    //       jboss.ws:context=CXFTimeoutClientDemo,endpoint=Test_CXF_WS
    }
}

Step5). Now we will write a simple Ant build script to build and execute the above program. So write an Ant build script “build.xml” file in the same directory where the above program is placed.

<project name="JBoss_Monitor" default="run">
<property name="jboss.home" value="/NotBackedUp/JBoss_All/jboss-as-7.1.0.Beta1" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />
<property name="base.dir" value="." />
 
   <path id="jboss.classpath">
     <fileset dir="${jboss.module.dir}">
        <include name="**/*.jar"/>
     </fileset>  
   </path>

   <target name="run">
       <javac srcdir="${base.dir}" destdir="${base.dir}"  includes="WebServiceMonitor.java" classpathref="jboss.classpath"/>
       <java classname="WebServiceMonitor">
           <classpath>
              <pathelement location="${base.dir}"/>
              <path refid="jboss.classpath"/>
           </classpath>
           <arg line="localhost 1090  CXFTimeoutClientDemo Test_CXF_WS"/>
       </java>
   </target>  
</project>

NOTE: The only change in the above file you need to do is to change the “jboss.home” directory path in the second line of the above script is to point to your own JBoss AS7 directory home directory.

Step6). Now before running your ANT script to build and deploy the above webapplication you should have the ANT as well as JAVA set in the $PATH variable of the Shell / command prompt as following:

For Unix Based OS:
export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/org.apache.ant_1.6.5/bin:$PATH

For Windows Based OS:
set PATH=C:/jdk1.6.0_21/bin;C:/org.apache.ant_1.6.5/bin;%PATH%

Step7). Run the command “ant” or “ant run” which will internally build the WebService and deploy it inside the JBoss AS7 standalone profile.

[userone@localhost Monitoring]$ ant run
Buildfile: /home/userone/Monitoring/build.xml

run:
      GOT THE MBeanServerConnection---SUCCESSFULLY
      ------------------Getting Service Details------------------
      jboss.ws:context=CXFTimeoutClientDemo,endpoint=Test_CXF_WS
      -----------------------------------------------------------
      	 Address : http://localhost:8080/CXFTimeoutClientDemo
      	 StartTime : Tue Nov 29 01:17:23 BTT 2011
      	 MinProcessingTime : 8
      	 MaxProcessingTime : 15191
      	 TotalProcessingTime : 15310
      	 RequestCount : 3
      	 ResponseCount : 3
      	 FaultCount : 0
      	 StopTime : null
      	 AverageProcessingTime : 5103

BUILD SUCCESSFUL
Total time: 2 seconds

Step8). If you want to run the above program manually then you will need to run the following command

.
java WebServiceMonitor localhost 1090 CXFTimeoutClientDemo Test_CXF_WS
.

NOTE: Here we are passing 4 Arguments
“localhost” (Here our JBoss is running),
“1090” (This is the jmx-connector-registry connector port of JBoss which is enabled in the step1),
“CXFTimeoutClientDemo” (This is the Context Root of our WebService Application)
“Test_CXF_WS” (This is the Endpoint Name of our WebService)
.
.
.
Thanks
MiddlewareMagic Team

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.