The Java Management eXtensions are really good to dynamically alter the behaviour of your application. The JMX API is standard in Java 5, but if you’re stuck with an old version then you have to build your own or use an existing API. In WAS 5 you can easily integrate with the WebSphere API. This post will describe how to do that. I will do this by an example of how to get a JMX Managed Log4J configuration in WAS 5.

The integration in WAS 5 is done by making an XML description of your managed bean (MBean) and making the implementation extend from com.ibm.websphere.management.RuntimeCollaborator. The implementing class must contain the methods that are defined in the XML.

The XML must be based on the MbeanDescriptor.dtd (attached) and can include MBeans with attributes, operations and notifications.

  • An attribute is read-only information that can be accessed through the MBean.
  • An operation is callable on the MBean
  • A notification is an event that gets thrown by the MBean

The Logging MBean consists of three attributes and one operation:

  1. Attribute: version (the log4j version)
  2. Attribute: current loggers (all the current Log4j loggers)
  3. Attribute: identification (The name of the root logger)
  4. Operation: reload (Reload the logging configuration file)

The XML of the MBean:

And the implementation:

We now have an MBean description and its implementation. The implementation needs the location of the log4j.xml and the root logger in its constructor to work.

To let WAS know we have a custom MBean we have to register the MBean in WAS. This can be done in multiple ways: the bean can be defined in the ”Extension MBean Providers” section of a WAS server in the Admin Console (see ref 3) and can be automatically started as the server starts. (see ref 3). Because Log4j works (at least for me) on an application basis and not on server basis, I will do it a little different. I will register the MBean programmatically and keep the description XML in the application classpath. (Words of warning: the application provided MBean XML yields for the server defined “Extension MBean Providers” with normal classloading: “PARENT_FIRST”.

To register the MBean I use a ServletContextListener (the complete class implementation is at the end of this blog) that I define in the web.xml of my web application so that whenever my application gets started the MBean will be registered and when my application is stopped, I can de-register the MBean.

And now there is a working MBean extension.

To manipulate the MBean we can use the wsadmin application.
The following JACL commands can be used (these are a subset):

[code]
# Get all the loggers
set loggers [$AdminControl queryNames type=Log4JLogger,*]
# Get the first log
set log [lindex $loggers 0]
# Get all the attibutes of the log
$AdminControl getAttributes $log
# Reload the logging of the log
$AdminControl invoke $log reload {}
[/code]

References:

  1. JMX Explanation
  2. MBean explanation
  3. Developerwork Tutorial

Complete ServletContextListener class: