Using JMS with Spring: Message Driven POJO
The previous post described how to implement a JMS messaging client using Spring JMS. This post will describe how to implement the Message listener as a spring Message driven POJO. Follow these steps to implement the Message driven POJO
1. Create the Message Driven POJO: the Message-Driven POJO (MDP) acts as a receiver for JMS messages. The one restriction on an MDP is that it must implement the javax.jms.MessageListener interface. Please also be aware that in the case where your POJO will be receiving messages on multiple threads, it is important to ensure that your implementation is thread-safe. The following listing shows the code for the MDP
SpringMDP.java
public class SpringMDP implements MessageListener {
public void onMessage(Message message) {
try {
System.out.println(((TextMessage) message).getText());
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
}
}
2. Once you've implemented your MessageListener, it's time to create a message listener container.
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<!-- this is the Message Driven POJO (MDP) -->
<bean id="messageListener" class="jms.SpringMDP"/>
<!-- and this is the message listener container -->
<bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="concurrentConsumers" value="5" />
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queue" />
<property name="messageListener" ref="messageListener" />
</bean>
Find above an example of how to define and configure one of the message listener containers that ships with Spring (in this case the DefaultMessageListenerContainer)
Reference: JMS on Spring
2 comments:
Hi !!! Good job!
Wuzzap?
I am glad to be a visitant of this pure site ! , regards for this rare information! .
Post a Comment