Step 1 - You will need same Jar files which we discussed in Producer Example
Spring Core
Spring Context
Spring JMS
activemq-all
commons-logging
slf4j
Step 2: Create applicationContext.xml in classpath
</beans>
Spring Core
Spring Context
Spring JMS
activemq-all
commons-logging
slf4j
Step 2: Create applicationContext.xml in classpath
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
<context:component-scan base-package="com.itech" />
<context:annotation-config/>
<!-- JMS ConnectionFactory to use, configuring the embedded broker using XML -->
<bean id="activemqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- brokerURL, You may have different IP or port -->
<constructor-arg name="brokerURL" value="vm://localhost:61616" />
</bean>
<!-- Pooled Spring connection factory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="activemqConnectionFactory" />
</bean>
<!-- Queue Destination -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- name of the queue -->
<constructor-arg index="0" value="THIS.IS.TEST.QUEUE" />
</bean>
<!-- Listeners -->
<jms:listener-container connection-factory="connectionFactory">
<jms:listener destination="THIS.IS.TEST.QUEUE" ref="messageListenerTest" />
</jms:listener-container>
</beans>
Step 3:
You need a Consumer Class which implements MessageListener interface:
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;
@Component("messageListenerTest")
public class MessageListenerTest implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println(textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
That's it, Now try sending a message with Producer example, and message will be listened by this MessageListener implementation.
No comments:
Post a Comment