JMX to manage JAX-WS 2.0 service using NetBeans 5.0



NetBeans 5.0 is out!

Among a full set of exciting features, check the NetBeans Home Page, it brings a very very cool support for Web Services. If you have not already played with web services, you should start doing so. It is becoming much simpler with JAX-WS 2.0.

As you already know, I blogged few weeks ago about that, JMX is now part of the set of stable modules.

Management Of Web Services (MOWS is sometimes found in literature) is a hot topic. More and more people are writing web services that need to be managed. I think that it is an appropriate time to start speaking about a possible bridging of JAX-WS and JMX technologies.

I am not going to detail how to write a webservice using Netbeans. Bobby Bissett and Milan Kuchtiak wrote an excellent tutorial on how to setup netbeans to work with JAX-WS 2.0. I am going to extend their tutorial by adding the necessary steps to add manageability to this webservice.

The ServiceImpl webservice has got a single method named hello. This method prefixes the received string with "hello" string and returns it. In Bobby and Milan's tutorial, the added prefix is hard coded in the hello method implementation. We are going to use this very simple case to see how we could use JMX to make the prefix updated dynamically. I assume that JAX-WS20Project is open and working well in your netbeans IDE.

Generating the HelloPrefixProvider MBean

I wrote a tutorial that explains how to write an MBean. To access the MBean wizard : File > New File > Management > JMX MBean. You can follow the steps defined in the tutorial to generate a HelloPrefixProvider Standard MBean. It contains a single read/write attribute of type String named HelloPrefix. No operation, no notification.

Generating the JMX Agent

I wrote a tutorial that explains how to write a JMX Agent. Follow it to generate a JMXAgent class that will be added to your webservice.

Adding the HelloPrefixProvider MBean to the Agent

I wrote a tutorial that explains how to register an MBean in a JMX Agent. Follow it to create and register the HelloPrefixProvider MBean in the JMXAgent.

Make the JAXWS service ServiceImpl access the Prefix attribute

In this step we are going to update the JMXAgent with an accessor that returns the HelloPrefixProviderMBean interface. This method will be used by the service to access the prefix.

In order to increase our JMX knowledge, we are going to make use of a very useful class MBeanServerInvocationHandler. This class is generally used on the JMX manager side to create MBean proxies. We are going to use it on the agent side to make MBean handling simpler. If you have an MBean server mbs containing an MBean with ObjectName name ("server.sample.my:type=HelloPrefixProvider" in our case), and if the MBean's management interface is described by the Java interface Intf (HelloPrefixProviderMBean in our case), you can construct a proxy for the MBean like this:


 HelloPrefixProviderMBean proxy = (HelloPrefixProviderMBean)
     MBeanServerInvocationHandler.newProxyInstance(mbs,
                                                   new ObjectName("server.sample.my:type=HelloPrefixProvider"),
                                                   HelloPrefixProviderMBean.class,
                                                   false);

So we add a getPrefixProvider() method that returns a proxy to the MBean prefix provider.

Your JMXAgent code should look like this:

Now we add the code to access the prefix attribute from the JAX WS 2.0 ServiceImpl service.
The current method implementation is:

    @WebMethod
    public String hello(@WebParam(name="name") String s) {
        System.out.println("Service received: " + s);
        return "Hello "+s;
    }
It becomes:
    @WebMethod
    public String hello(@WebParam(name="name") String s) throws Exception {
        System.out.println("Service received: " + s);
        return JMXAgent.getDefault().getPrefixProvider().getHelloPrefix() + " " + s;
    }

We simply replaced the "hello " string by a call to access the prefix attribute. Lazily, when the service is first called, we deploy the management and access to the default prefix value.

We are done! The service has been very lightly impacted. A single line of code has been needed to make the service much more dynamic.

Compile your project.

Configuring Tomcat to connect with JConsole

In order to enable JConsole access to the Tomcat JVM management data, the Tomcat launch script must be updated.

  • Edit <your netbeans install path>/enterprise2/jakarta-tomcat-5.5.9/bin/catalina.bat (.sh for Unix users)
  • Add a -Dcom.sun.management.jmxremote system property to the Tomcat JVM run command line. For each exec command, add the required property. The following is a catalina script extract :
    ... 
    exec "$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \\
          -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \\ 
          -Djava.security.manager \\       
          -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \\ 
          -Dcatalina.base="$CATALINA_BASE" \\      
          -Dcatalina.home="$CATALINA_HOME" \\       
          -Djava.io.tmpdir="$CATALINA_TMPDIR" \\      
          -Dcom.sun.management.jmxremote\\       
          org.apache.catalina.startup.Bootstrap "$@" start
    ... 
    
You are DONE with configuration. You are now able to view your MBeans (and all other JVM MBeans) from JConsole.

Running and testing JAX-WS20Project

Click on Run project. The project is compiled, a war is generated, tomcat is started and your web service is deployed ready to serve client requests. Go to the WebService registry, as explained in Bobby and Milan tutorial, and activate the hello operation. The first call to hello request will deploy JMX management.

Now, launch JConsole by clicking on Toolbar button .

Connect to tomcat JVM by selecting org.apache.catalina.startup.Bootstrap JVM as shown in the following image:


Go to the MBeans tab and select HelloPrefixProvider MBean located under server.sample.my node. Type in a value for the HelloPrefix Attribute:


Check the new prefix value from the Web Service registry...

... it has been updated with what you typed in JConsole. The bridge is created! Ok the river was not very wide, but consider it a start to building your own suspension bridge.

I hope that this blog entry helped you better understand how JMX and JAX-WS can be used together. And again, great Welcome to NetBeans 5.0!

Jean-Francois

Comments:

Post a Comment:
Comments are closed for this entry.
About

jeanfrancoisdenise

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today