Mort Learns Monitoring and Admin Commands for GlassFish
By ByronNevins on Sep 16, 2009
- Monitoring is a powerful feature in GlassFish V3.
- Admin Commands are a powerful feature in GlassFish V3.
In this blog I'll show you how to create an Admin Command that exercises the Monitoring Facilities.
- ProbeProvider - A class or interface that has methods (Probes) that can be called at runtime.
- Probe - A method that can be called at runtime that in turn will call any Listeners that are registered
- Listener - A method that is registered to receive "events" coming from a Probe
- Admin Command - An easy to call command running on GlassFish V3. You can access it via REST, HTTP or asadmin. You could easily do this whole tutorial inside a servlet instead. I chose to use an Admin Command so you can see how to create and use commands.
In this tutorial we will work with a ProbeProvider interface. We write an interface with the methods (Probes) that we wish to call at runtime. We never create an implementation of this interface. Instead we hand it off to GlassFish. GlassFish will magically create an implementation class for the interface and return an instance of that implementation. Under the hood, GlassFish has automagically inserted code into your methods that will call any Listeners that you subsequently register.
Using an interface is a fairly unusual case. The much fancier and more usual case is that you give GlassFish an instance of any POJO that has the proper annotations. GlassFish will then rewrite the actual bytecode of the class and add calls to the Listeners!
- The command should setup the Probes and Listeners correctly the first time the command is run
- Each subsequent call should fire the appropriate probe
The complete class is here
You provide LCPP and LCListener. LCPP is the Probe Provider interface and LCListener is the Listener class. The 2 lines that do all the registering and magic are here:
probe = probeProviderFactory.getProbeProvider(LCPP.class);
That's all! You now have a probe object that you can call methods on that will automatically call your listener class.
Probe Provider Interface
The interface is here. As you can see it is mostly a bunch of annotations and 2 methods:
void ping2(@ProbeParam("message") String s);
Note that they must return void You should make all parameters primitives or String to avoid trouble.
The source is here. Now you can see how the Listener gets hooked up with the Probe. The Listener needs the values from the ProbeProvider annotation in the Probe Provider class. It's a lot easier to see how to do it by looking at the source then for me to explain it.
How to Test
- Take the files and drop them into a GlassFish extension project. If you have GlassFish source just do what I did -- drop them into the core/kernel module. Or you can just use the files as a guide and, say, create a Servlet instead of using an Admin Command.
- build and deploy the jar
- start the server
- tail the server log (even better -- run asadmin start-domain --verbose)
- call asadmin ping <optional-string-here>
- Look for the listener messages in the log
- goto 5