Home > Uncategorized > Extending the Grizzly HTTP Runtime part II: Managing the monster using JMX

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");
            ServletAdapter sa = new ServletAdapter();
            sa.setServlet(new MyServlet());
            ServletAdapter sa2 = new ServletAdapter();
            sa2.setServlet(new MySecondServlet());
        } 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);
 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);
 60         ws.enableJMX(new Management() {
 62             public void registerComponent(Object bean, ObjectName oname, String type)
 63                     throws Exception {
 64                 Registry.getRegistry().registerComponent(bean, oname, type);
 65             }
 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”;


Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: