Over a million developers have joined DZone.

Configuring MDP and Controlling It With and Without JMX

DZone's Guide to

Configuring MDP and Controlling It With and Without JMX

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

This tip demonstrates how to create and deploy an MDP, and how to control it, using both Spring’s inbuilt component management mechanisms, and JMX.


We have a number of JMS listeners in our application. Having ported our hardware to a grid of virtual machines, we want the feature to switch-on and switch-off the listeners on the VMs, and re-allocate the listeners to VMs across the grid on-demand, to make best use of the resources.


This tip just deals briefly with configuring the MDP, and switching it on and off.


- What is an MDP?
- MDP stands for Message Driven Pojo.

In the EJB world we have MDBs (Message Driven Beans). These are standard JEE components which are configured on a server and which act as a Message Listener.

Spring provides MDPs as an answer to MDBs.

Some helpful links are:

Creating a simple Spring MDP component, and controlling it using the Spring container provided “start” and “stop” methods

MDP Spring configuration

Extract from the Spring Config XML file spring_context_jms.xml


<!-- Destination configuration -->
<bean id="sample_queue"

<!-- Message Listener Container configuration -->
<bean id="sample_jmsContainer"
<property name="connectionFactory"
ref="activeMqQueueConnectionFactory" />
<property name="destination" ref=" sample_queue" />
<property name="messageListener"
ref="sample_messageListener" />

<!-- Message Driven POJO (MDP) configuration -->
<bean id="sample_messageListener"
<property name="listener" ref="sample_jmsContainer" />
The sample class we create (sample_messageListener of class “jmsexample.ExampleListener”) must extend javax.jms.MessageListener and implement the “onMessage” method.

As seen from the Spring configuration above, we have provided this as a ref to the “messageListener” property in the “DefaultMessageListenerContainer102” class.

Also as seen above, we have provided a ref to “sample_jmsContainer” in the bean “sample_messageListener”. (This is so that we are able to expose it using JMX – as discussed further on).

Configuring Spring components in a Web application in Tomcat

The MDPs are deployed in a Web application as JMSWebApplication.war.
Follows is an extract from the web.xml

<description>Context Loader Listener</description>

Getting a handle to the Spring’s “Listener Container” component and calling start and stop on it (Non-JMX)

The following is a relevant extract from the client jsp:

<%@page import="org.springframework.beans.factory.BeanFactory,org.springframework.context.ApplicationContext,org.springframework.context.support.ClassPathXmlApplicationContext,org.springframework.jms.listener.DefaultMessageListenerContainer102,org.springframework.web.context.WebApplicationContext,org.springframework.web.context.support.WebApplicationContextUtils"%>

ServletContext context = application
WebApplicationContext wac = WebApplicationContextUtils

DefaultMessageListenerContainer102 listener = (DefaultMessageListenerContainer102) wac


Exposing and Controlling the Listener component using Spring JMX

This involves configuring Spring JMX. Since this is deployed in Tomcat, we will enable JMX in Tomcat and connect to it using jconsole to control this component.

Configuring Spring JMX (server/container), and Exposing the component in JMX

<bean id="exporter"
<property name="beans">
<entry key="bean:name=sample_listener_jmx"
value-ref=" sample_messageListener" />
Arguments to Tomcat to enable JMX

Provide these (self-explanatory) arguments to the JVM while starting up Tomcat

-Dcom.sun.management.jmxremote.port=10088 \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false \
Code skeleton of jmsexample. ExampleListener

(Note the “startListener” and “stopListener” methods, and use of the “DefaultMessageListenerContainer102” class.)

package jmsexample;
< imports come here ... >

public class ExampleListener implements MessageListener {

private DefaultMessageListenerContainer102 listener;
< other variables here ... >

public void onMessage(Message message) {

< Business Logic here ... >


@ManagedAttribute(description = "Starts the Listener", currencyTimeLimit = 15)
public void startListener() {

System.out.println("Starting listener");
System.out.println("listener started " + getListener().isRunning());

@ManagedAttribute(description = "Stops the Listener", currencyTimeLimit = 15)
public void stopListener() {

System.out.println("Stopping listener");
System.out.println("listener stopped " + getListener().isRunning());

public DefaultMessageListenerContainer102 getListener() {
return listener;

public void setListener(DefaultMessageListenerContainer102 listener) {
this.listener = listener;

< other methods here ...>



View in JConsole of controlling the component

(Note the “startListener” and “stopListener” methods exposed)
The remote connection string here, for example, is service:jmx:rmi:///jndi/rmi://localhost:10088/jmxrmi

A simple java based rmi client using Tomcat JMX to control the components

Client code snippet and configuration

String address =
JMXServiceURL serviceURL = new JMXServiceURL(address);
Map<String, Object> environment = null;
JMXConnector connector = JMXConnectorFactory.connect(serviceURL,
MBeanServerConnection mBeanConnection = connector

ObjectName exampleServiceName = ObjectName

mBeanConnection.invoke(exampleServiceName, "startListener", null, null);
//mBeanConnection.invoke(exampleServiceName, "stopListener", null, null);

From http://viralpatel.net/blogs

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}