Category Archives: Spring JMX

JMX and MBean Support With Spring

The context of this post is simply about how to use the Spring Framework to export your Spring-managed pojos for management and monitoring via JMX. Later I’ll post on using Hyperic, in the cloud, etc. First things first – as this is an update from a similar post of mine from ’08 or early ’09.

Let’s start by adding the spring jmx dependency, which will be something like this, depending on the repos you are using:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.core</artifactId>
<version>${spring.framework.version}</version>
</dependency>

Service Pojo

Now let’s set up some java classes for management. I have a business service that I want to monitor and a pojo to instrument.

@BusinessService
@ManagedResource(objectName = "bean:name=inventoryManager", description = "Inventory Service",
        log = true, logFile = "oms.log", currencyTimeLimit = 15, persistPolicy = "OnUpdate", persistPeriod = 200,
        persistLocation = "foo", persistName = "bar")
public class InventoryServiceImpl implements InventoryService {

    @Autowired private InventoryDao inventoryDao;

    @ManagedOperation(description = "Add two numbers")
    @ManagedOperationParameters({
            @ManagedOperationParameter(name = "x", description = "The first number"),
            @ManagedOperationParameter(name = "y", description = "The second number")})
    public int add(int x, int y) {
        return x + y;
    }

    @ManagedOperation(description = "Get inventory levels")
    @ManagedOperationParameters({@ManagedOperationParameter(name = "product", description = "The Product)})
    public long getInventoryLevel(Product product) {
        return getInventoryLevel(product.getSkew());
    }
}

First, let’s peak into my @BusinessService annotation in case you are wondering:

@Transactional
@Service
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface BusinessService {
}

Annotating any of my service layer pojo’s makes them both transactional and with an instance created in the Spring context.

Entity Pojo

Now here is a simple pojo as an entity or javabean, what have you:

@ManagedResource(objectName = "bean:name=myPojoEntity", description = "My Managed Bean", log = true,
        logFile = "oms.log", currencyTimeLimit = 15, persistPolicy = "OnUpdate", persistPeriod = 200,
        persistLocation = "foo", persistName = "bar")
public class MyPojo {

    private long somethingToTuneInRuntime;

    /* Creates a writeable attribute for managing */
    @ManagedAttribute(description = "Tunable In Runtime Attribute",
            currencyTimeLimit = 20,
            defaultValue = "bar",
            persistPolicy = "OnUpdate")
    public void setSomethingToTuneInRuntime(long value) {
        this.somethingToTuneInRuntime = value;
    }

    @ManagedAttribute(defaultValue = "foo", persistPeriod = 300)
    public String getSomethingToTuneInRuntime() {
        return somethingToTuneInRuntime;
    }
}

Spring Config

Now let’s configure Spring to autoregister our pojos to export and manage/monitor:
Create a jmx-context.xml file in your WEB-INF/* dir

Add: <context:mbean-export/>

Activates default exporting of MBeans by detecting standard MBeans in the Spring
context as well as @ManagedResource annotations on Spring-defined beans.
Rather than defining an MBeanExporter bean, just provide this single element. I could walk you through a simple, simpler and simplest config of spring jmx but with annotational config in the simplest requirements, this is all you need to do to get up and running. If you need object naming in multiple vm situations, you can easily do that and other things too but, that’s out of scope for this post ;)

Share/Save

JMX MBean Proxying for Export via Remoting to the Client

I wanted to expose mbeans related to application monitoring and services such as a JMS broker, users in session, etc and remote that data to the client which in my case was Flex.  First I checked out using annotations for automated registration and mbean creation but that wouldn’t help me export to a remoting format..without creating my own classes. Something for another post, but what I did was create a package with some classes, which I will explain here. You can certainly deliver this strategy more minimally but I’ve found that to come back and bite me later so I’ve added in some type safety to the proxy creation of the MBeans.

The Basic Components

There are a few important components you need to set this up as a framework

1. An MBeanConnection

2. A type-safe proxy generator

3. An applicable JMX service URL, Port, Hostname

To export mbeans to the client

1. ObjectName that can be connected to if they are services

2. Ports for those services where applicable (i.e. a broker)

3. Interfaces to proxy

I. The Goal: export mbean data by remoting to the client

I created a service which is mirrored as a Flex actionscript class for remoting which encapsulates the calls for various system monitoring data via jmx: This I registered for remoting in the framework of choice, in my case it was Flex. I have a custom annotation that handles this which I don’t include in this snippet.

public class ClientMBeanService implements IClientMBeanService { ..view the code… }

II. The Delegate

With the idea of each component to be exported create a Delegate class that implements the interface dictating the contract that it must implement a method to provide the port, the ObjectName and export the mbean:

public class BrokerDelegate implements IMBeanDelegate { ..view the code… }

All Delegates must implement

public interface IMBeanDelegate { ..view the code… }

III. For Remote and Local MBeanConnections

public class MBeanConnection { ..view the code… }

IV. The Cool Part: Get a Typesafe Proxy Intance of The MBean

public final class MBeanProxyInstance { ..view the code… }

V. Typesafing: I did not originally write this class, only modified it for Java 6, but the crux is:

public class TypeSafeMBeanProxy { ..view the code… }

Share/Save

Annotational MBean Export with Spring and JMX

If you just want to easily export mbeans to jconsole or an external profiling tool this is pretty sweet:

I. Create a Spring schema config file for JMX

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- This bean needs to be eagerly pre-instantiated in order for the exporting to occur: -->
<!-- ***************** Autodetects Annotated Beans JMX Exposure *********************** -->
<bean id="annotationalMbeanExporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="autodetect" value="true"/>
<property name="namingStrategy" ref="namingStrategy"/>
<property name="assembler" ref="assembler"/>
<!-- one of many optional config options -->
<property name="registrationBehaviorName" value="REGISTRATION_IGNORE_EXISTING"/>
</bean>
<bean id="attributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
<bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
<property name="attributeSource" ref="attributeSource"/>
</bean>
<bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
<property name="attributeSource" ref="attributeSource"/>
</bean>
</beans>

II. Annotate a component, bean, etc

@Service
@ManagedResource(objectName = "bean:name=applicationMonitor")
public class ClientMBeanService implements IClientMBeanService {

@ManagedOperation(description = "Monitor the JMS broker")
public BrokerStatistics exposeBroker() {
return new BrokerDelegate().exposeMBean();
}
}

@Component
@ManagedResource(objectName = "spring:name=broker", description = "ActimemMQ Broker")
public class BrokerStatistics {

private String brokerId;
private String brokerName;
private long totalMessages;
private long totalConsumers;
private long totalTopics;
private long totalQueues;
private List<Map> topics;
private List<Map> queues;

@ManagedAttribute
public String getBrokerId() {
return brokerId;
}
...etc
}

The annotational configuration in the mbean exporter allows automatic registration and mbean creation of beans. It’s that simple.

Share/Save