Tag Archives: Annotations

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

Creating Custom Annotations

Here is a very simplistic example on creating custom annotations in java.


package com.foo.infrastructure.annotations;
import java.lang.annotation.*;
@Documented
@Retention (RetentionPolicy.SOURCE)
@Inherited
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR,ElementType.ANNOTATION_TYPE})
public @interface Unfinished {

public enum Priority { LOW, MEDIUM, HIGH }
String value();
String[] owners() default “”;
Priority priority() default Priority.MEDIUM;
}

}

And here is an equally simplistic example of using it

package com.foo.jms.infrastructure.delegates;

import com.foo.jms.infrastructure.async.JMSConsumerImpl;
import com.foo.infrastructure.annotations.Unfinished;
import org.apache.log4j.Logger;
import org.springframework.jms.core.SessionCallback;
import org.springframework.jms.core.support.JmsGatewaySupport;
import org.springframework.jms.support.JmsUtils;
import javax.jms.*;
/**
*JMSMessageDelegateImpl delegates JMS input to the appropriate handler.
* Delegates asynchronous and synchronous reception
* @author Holly Edelson
*/
@Unfinished(value=”Class scope”, priority=Unfinished.Priority.LOW, owners=”Holly”)
public class JMSMessageDelegateImpl extends JmsGatewaySupport implements JMSMessageDelegate {

private static final Logger logger = Logger.getLogger(JMSMessageDelegateImpl.class);
….
public void handleMessage(Map message) {

Map receivedMessage = (Map) getJmsTemplate().receiveAndConvert(destination);
logger.debug(“nn” + receivedMessage);

}

}

Share/Save