In my last post, I had shown how you can post a JMS message to a ActiveMQ queue. In this post I will show the reverse process. I will not try to consume this message using a Spring Message Driven POJO. I assume your familiarity with Spring MDP. If not you can look here - http://static.springframework.org/spring/docs/2.5.x/reference/jms.html
The operation that I intend to carry out is depicted in Figure 1.
Figure 1 - Consuming JMS message using Spring MDP
Step 1 - Create the message listener.
Listing 1 - EventMessageConsumer.java
/**
*
*/
package org.opengarage.pingscape.mq.consumer;
import javax.jms.Message;
import javax.jms.MessageListener;
/**
* @author dhrubo
*
*/
public class EventMessageConsumer implements MessageListener {
/* (non-Javadoc)
* @see javax.jms.MessageListener#onMessage(javax.jms.Message)
*/
public void onMessage(Message message) {
System.out.println("Message Consumed -"+message);
//TODO : Take some action.... invoke service method
//NOTE : Service object is injected by Spring framework
}
}
Step 2 - Wire up the bean in Spring configuration
Listing 2 - spring-activemq-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>
<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue.opengarage</value>
</constructor-arg>
</bean>
<!-- lets wrap in a pool to avoid creating a connection per send -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory">
<ref local="jmsFactory" />
</property>
</bean>
<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!-- a sample POJO message producer which uses a Spring JmsTemplate -->
<bean id="messageProducer"
class="org.opengarage.pingscape.mq.producer.EventInvitationMessageProducer">
<property name="jmsTemplate" ref="jmsTemplate" />
<property name="queue" ref="queue" />
<property name="messageCreator">
<bean class="org.opengarage.pingscape.mq.producer.EventMessageCreator" />
</property>
</bean>
<!-- Consumer part -->
<!-- this is the Message Driven POJO (MDP) -->
<bean id="messageListener"
class="org.opengarage.pingscape.mq.consumer.EventMessageConsumer" />
<!-- and this is the message listener container -->
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queue" />
<property name="messageListener" ref="messageListener" />
</bean>
</beans>
Step 3 - Test
Finally you can see the results of message produced and consumed in the Admin web console as shown in Figure 2.
Figure 2 - Queue statistics
0 comments:
Post a Comment