ActiveMQ configuration in Spring web application
Point to Point (using Queue)
applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:jms="http://www.springframework.org/schema/jms" 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"> <context:component-scan base-package="jms" /> <context:annotation-config /> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL"> <value>tcp://localhost:61616</value> </property> </bean> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="testQueue"/> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="defaultDestination" ref="destination"/> </bean> <jms:listener-container connection-factory="connectionFactory"> <jms:listener destination="testQueue" ref="messageReceiver" method="receive"/> </jms:listener-container> </beans>
MessageReceiver.java @Component public class MessageReceiver { public void receive(Map message) throws Exception { String msg = (String) message.get("message"); } }
MessageSender.java @Controller @RequestMapping("/sendMsg") public class MessageSender { @Autowired private JmsTemplate jmsTemplate; @RequestMapping(method=RequestMethod.GET) public String send() { System.out.println("Sending..."); sendMessage("how u doin!!"); return ""; } public void sendMessage(final String msg) { jmsTemplate.send( new MessageCreator() { public Message createMessage(Session session) throws JMSException { MapMessage mapMessage = session.createMapMessage(); mapMessage.setString("message", msg); return mapMessage; } } ); System.out.println("Message sent> " + msg); } }
Publisher – Subscriber (using Topic)
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>
<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="testTopic"/>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestination" ref="destination"/>
</bean>
<bean id="smsNotifListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="destination"/>
<property name="messageListener" ref="smsSubscriber"/>
</bean>
<bean id="emailNotifListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="destination"/>
<property name="messageListener" ref="emailSubscriber"/>
</bean>Required Jars:
1. activemq-all-5.5.0 jar
2. log4j-1.2.16
3. slf4j-api-1.5.8
4. slf4j-log4j12-1.5.8
5. jcl-over-slf4j-1.5.8