<!-- Destination configuration -->
<bean id="sample_queue"
class="org.apache.activemq.command.ActiveMQQueue"
autowire="constructor">
<constructor-arg>
<value>SAMPLE.QUEUE</value>
</constructor-arg>
</bean>
<!-- Message Listener Container configuration -->
<bean id="sample_jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer102">
<property name="connectionFactory"
ref="activeMqQueueConnectionFactory" />
<property name="destination" ref=" sample_queue" />
<property name="messageListener"
ref="sample_messageListener" />
</bean>
<!-- Message Driven POJO (MDP) configuration -->
<bean id="sample_messageListener"
class="jmsexample.ExampleListener">
<property name="listener" ref="sample_jmsContainer" />
</bean>
Code language: HTML, XML (xml)
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). <display-name>JMSWebApplication</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_context.xml</param-value>
</context-param>
<listener>
<description>Context Loader Listener</description>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Code language: HTML, XML (xml)
<%@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
.getContext("/JMSWebApplication");
WebApplicationContext wac = WebApplicationContextUtils
.getRequiredWebApplicationContext(context);
DefaultMessageListenerContainer102 listener = (DefaultMessageListenerContainer102) wac
.getBean("sample_jmsContainer");
listener.stop();
//listener.start();
Code language: Java (java)
<bean id="exporter"
class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="bean:name=sample_listener_jmx"
value-ref=" sample_messageListener" />
</map>
</property>
</bean>
Code language: HTML, XML (xml)
Code language: HTML, XML (xml)-Dcom.sun.management.jmxremote.port=10088 \ -Dcom.sun.management.jmxremote.ssl=false \ -Dcom.sun.management.jmxremote.authenticate=false \
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");
getListener().start();
System.out.println("listener started " + getListener().isRunning());
}
@ManagedAttribute(description = "Stops the Listener", currencyTimeLimit = 15)
public void stopListener() {
System.out.println("Stopping listener");
getListener().stop();
System.out.println("listener stopped " + getListener().isRunning());
}
public DefaultMessageListenerContainer102 getListener() {
return listener;
}
public void setListener(DefaultMessageListenerContainer102 listener) {
this.listener = listener;
}
< other methods here ...>
}
Code language: Java (java)
String address =
"service:jmx:rmi:///jndi/rmi://localhost:10088/jmxrmi";
JMXServiceURL serviceURL = new JMXServiceURL(address);
Map<String, Object> environment = null;
JMXConnector connector = JMXConnectorFactory.connect(serviceURL,
environment);
MBeanServerConnection mBeanConnection = connector
.getMBeanServerConnection();
ObjectName exampleServiceName = ObjectName
.getInstance("bean:name=sample_listener_jmx");
mBeanConnection.invoke(exampleServiceName, "startListener", null, null);
//mBeanConnection.invoke(exampleServiceName, "stopListener", null, null);
Code language: Java (java)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Viral,
Could you please attache the total source code of this tutorial please.
Thanks,
Naveen
This is a blatant plagarization of this site: http://java.dzone.com/articles/configuring-mdp-and
Give credit where credit is due!
Hi, The article is from the same author :) He published the article first on this site and then on dzone. The citation is done on DZone. Check the end of the article on dzone. ^_^