09 May 2006

Springing into OC4J MBeans

I noticed a while back that Spring has a funky little mechanism by which it can export its beans as JMX MBeans.

Which pricked my attention since it meant that theoretically with OC4J 10.1.3, any Spring Beans which are configured as part of an application and exported as MBeans, could be accessed through Application Server Control using the Application MBean Browser. Giving developers the power to allow administrators to configure elements of their Spring based applications.

So I tried it out and found that it just bloody worked! Which blew me away. These standards we all operate on are a wonderful thing when they all come together.

Here's an example of a Spring configuration which defines a bean and injects some initial values for its properties:

<bean id="aussieGreetingService"
<property name="greeting" value="gday mate!"/>
<property name="language" value="Aussie"/>

To make this bean configuration available as an MBean then you need to do two extra things in this configuration file.

1. Define a Spring bean which represents an MBeanServer

<bean id="greetingMbeanServer"
<property name="defaultDomain" value="GreetingService" />

Note that this MBeanServer bean creates an MBeanServer using the specified defaultDomain -- this is injected as a property.

2. Specify which of the Spring beans should be exported as MBeans using the MBeanExport Spring bean.

<bean id="exporter"
<property name="beans">
<entry key="bean:name=aussieGreetingService"
value-ref="aussieGreetingService" />
<property name="server" ref="greetingMbeanServer" />

Note that the name of the MBeanServer in which the MBeans are to be created has to be provided -- and it points to the MBeanServer we created in Step 1.

The MBean will expose all of the relevant properties (JMX:attributes) of the JavaBean as well as the methods (jmx:operations) the JavaBean supports.

And now the cool thing -- if this configuration file is deployed as part of an application with the Spring framework libraries and the corresponding JavaBeans, then the Spring beans will be accessible as JMX MBeans using Application Server Control.

Viewing and setting JavaBean properties:

Viewing JavaBean methods:

Invoking a JavaBean method:

And there you have it -- managing Spring beans using JMX with Application Server Control.

No comments: