I. introduce dependency
<!-- xbean as<amq:connectionFactory /> --> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>3.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <!-- activemq --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.7.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.12.1</version> </dependency>
II. Spring mq.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:amq="http://activemq.apache.org/schema/core" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.12.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd"> <!-- ActiveMQ Connection factory --> <amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://localhost:61616" userName="admin" password="admin" /> <!-- Improve efficiency, configure JMS Connection factory --> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <constructor-arg ref="amqConnectionFactory" /> <property name="sessionCacheSize" value="100" /> </bean> <!--Defining message queues( Queue)--> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="order.queue"/> </bean> <!--Defining themes(Topic)--> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="order.topic"/> </bean> <!-- To configure JMS Template ( Queue),Spring Provided JMS Tool class, using it to send and receive messages. --> <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="queueDestination" /> <property name="receiveTimeout" value="10000" /> <!-- true yes topic,false yes queue,The default is false --> <property name="pubSubDomain" value="false" /> </bean> <!-- To configure JMS Template ( topic),Spring Provided JMS Tool class, using it to send and receive messages. --> <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="topicDestination" /> <property name="receiveTimeout" value="10000" /> <!-- true yes topic,false yes queue,The default is false --> <property name="pubSubDomain" value="true" /> </bean> <!-- Configure message queuing listeners( Queue or Topic) --> <bean id="messageListener1" class="com.mq.QueueMessageListener" /> <bean id="messageListener2" class="com.mq.QueueMessageListener2" /> <bean id="messageListener3" class="com.mq.QueueMessageListener3" /> <bean id="messageListener4" class="com.mq.QueueMessageListener4" /> <!--queue Monitor--> <jms:listener-container destination-type="queue" container-type="default" connection-factory="connectionFactory" acknowledge="auto"> <jms:listener destination="order.queue" ref="messageListener1" /> <jms:listener destination="order.queue" ref="messageListener2" /> </jms:listener-container> <!-- Topic Monitor --> <!--<jms:listener-container destination-type="topic" --> <!--container-type="default" connection-factory="connectionFactory"--> <!--acknowledge="auto">--> <!--It is found in the test that the message is repeatedly received in this way--> <!--<jms:listener destination="order.topic" ref="messageListener3" />--> <!--<jms:listener destination="order.topic" ref="messageListener4" />--> <!--</jms:listener-container>--> <!--DefaultMessageListenerContainer Realization topic Monitor--> <bean id="container" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="messageListener" ref="messageListener3" /> <property name="destination" ref="topicDestination" /> <!--Solve topic Message received repeatedly--> <property name="pubSubNoLocal" value="true"></property> </bean> <!--DefaultMessageListenerContainer Realization topic Monitor--> <bean id="container2" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="messageListener" ref="messageListener4" /> <property name="destination" ref="topicDestination" /> <!--Solve topic Message received repeatedly--> <property name="pubSubNoLocal" value="true"></property> </bean> </beans>
III. message producers
Peer to peer message producers @Service public class QueueProductService { @Autowired private JmsTemplate jmsQueueTemplate; //Use default destination public void sendMessageDefault(final String msg) { Destination destination = jmsQueueTemplate.getDefaultDestination(); System.out.println("To the queue: " + destination + " Successfully sent a message"); jmsQueueTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(msg); } }); } //Destination can be specified public void sendMessage(Destination destination, final String msg) { jmsQueueTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(msg); } }); } } //Publish / subscribe message producers @Service public class TopicProductService { @Autowired private JmsTemplate jmsTopicTemplate; //Use default destination public void sendMessageDefault(final String msg) { Destination destination = jmsTopicTemplate.getDefaultDestination(); System.out.println("towards topic: " + destination + " Successfully sent a message"); jmsTopicTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(msg); } }); } //Destination can be specified public void sendMessage(Destination destination, final String msg) { System.out.println(Thread.currentThread().getName() + " towards" + destination.toString() + "Send subject:" + msg); jmsTopicTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(msg); } }); } }
IV. message listener
public class QueueMessageListener implements MessageListener{ @Override public void onMessage(Message message) { TextMessage tm = (TextMessage) message; try { System.out.println("Message received by listener 1-----"+tm.getText()); } catch (JMSException e) { System.out.println("QueueMessageListener abnormal"); e.printStackTrace(); } } } QueueMessageListener2,QueueMessageListener3,QueueMessageListener4 Same as above
Five, test
@RequestMapping("/send") @ResponseBody public String sendMeg(){ System.out.println("send queue message"); Destination destination=new ActiveMQQueue("order.queue"); queueProductService.sendMessage(destination,"hello.queue"); return "ok"; } @RequestMapping("/send2") @ResponseBody public String sendMeg2(){ System.out.println("send topic message"); Destination destination=new ActiveMQTopic("order.topic"); topicProductService.sendMessage(destination,"hello.topic.123"); return "ok"; } Note how to instantiate Destination: Destination destination=new ActiveMQQueue("order.queue"); Destination destination=new ActiveMQTopic("order.topic"); Observe console output: Send topic message http-nio-8080-exec-5 send topic to topic://order.topic: hello.topic.123 Message received by listener 3 ------ hello.topic.123 Message received by listener 4 ------ hello.topic.123 Send queue message Message received by listener 2 ------ hello.queue Send queue message Message received by listener 1 ------ hello.queue It can be seen that: The queue message will only be consumed randomly once, and the topic will be consumed twice