Extending the Grizzly HTTP Runtime part II: Managing the monster using JMX
Now that we are all able to create Grizzly Web Server in less than 10 lines, let’s complicate our day and add JMX management to the monster
In part I, I’ve described how easy it is to create synchronous and asynchronous http based Web Server. One of the most complicated example was:
GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
try{
ServletAdapter sa = new ServletAdapter();
sa.setRootFolder("/Path/To/Exploded/War/File");
sa.setServlet(new MyServlet());
sa.setContextPath("/myServlet");
ws.addGrizzlyAdapter(sa);
ServletAdapter sa2 = new ServletAdapter();
sa2.setRootFolder("/Path/To/Exploded/War2/File");
sa2.setServlet(new MySecondServlet());
sa2.setContextPath("/mySecondServlet");
ws.addGrizzlyAdapter(sa2);
ws.start();
} catch (IOException ex){
// Something when wrong.
}
That wasn’t too difficult, was it? Now let’s add JMX support:
47 GrizzlyWebServer ws = new GrizzlyWebServer(path);
48 ServletAdapter sa = new ServletAdapter();
49 sa.setRootFolder("/Path/To/Exploded/War/File");
50 sa.setServlet(new MyServlet());
51 sa.setContextPath("/myServlet");
52 ws.addGrizzlyAdapter(sa);
53
54 ServletAdapter sa2 = new ServletAdapter();
55 sa2.setRootFolder("/Path/To/Exploded/War2/File");
56 sa2.setServlet(new MySecondServlet());
57 sa2.setContextPath("/mySecondServlet");
58 ws.addGrizzlyAdapter(sa2);
59
60 ws.enableJMX(new Management() {
61
62 public void registerComponent(Object bean, ObjectName oname, String type)
63 throws Exception {
64 Registry.getRegistry().registerComponent(bean, oname, type);
65 }
66
67 public void unregisterComponent(ObjectName oname) throws Exception {
68 Registry.getRegistry().
69 unregisterComponent(oname);
70 }
71 });
72 ws.start();
To enable JMX, you just need to implement the Management interface, and set it using the enableJMX method of the GrizzlyWebServer class. As you can see, you can plug your own JMX implementation easily using the Management interface. In the example above, I’ve just used the Apache Commons Modeler, which does all the JMX “bla bla bla” for me via its org.apache.commons.modeler.Registry static class.
To see it live, I just do:
% java -Dcom.sun.management.jmxremote.port=1199 -Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false -jar grizzly-embed-samples.jar
// Start jconsole
% jconsole
Hey hey I can see:

Miaaaaaam! Next time I will explain how to grab statistics from your embedded GrizzlyWebServer, like number of requests, time spend, thread pool stats, etc. You can download the example above here. The binary can be used as it is and include all the Grizzly required classes to run.
_uacct = “UA-3111670-1″;
urchinTracker();
technorati: grizzly web server embedded jmx
