Spring integrates activemq to send MQ messages in [Topic mode] instances and activemqmq
Topic mode message sending instance
1. Introduction of pom
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.11.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.4.RELEASE</version> </dependency>
2. Producer Configuration
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- Configure the JMS connection factory --> <bean id = "providerConnectionFactory" class = "org. apache. activemq. activeMQConnectionFactory "> <property name =" brokerURL "value =" failover :( tcp: // 192.168.147.131: 61616) "/> <property name =" useAsyncSend "value =" true "/> <property name =" clientID "value =" providerClienctConnect "/> </bean> <! -- Define Message Destination --> <bean id = "topicDestination" class = "org. apache. activemq. command. activeMQTopic "> <constructor-arg value =" testSpringTopic "/> </bean> <! -- Message sender client --> <bean id = "providerJmsTemplate" class = "org. springframework. jms. core. jmsTemplate "> <property name =" connectionFactory "ref =" providerConnectionFactory "/> <property name =" defaultDestination "ref =" topicDestination "/> <! -- Enable subscription mode --> <property name = "pubSubDomain" value = "true"/> <property name = "receiveTimeout" value = "10000"/> <! -- The deliveryMode, priority, and timeToLive switch must be set to true. The default value is false --> <property name = "explicitQosEnabled" value = "true"/> <! -- Sending mode: DeliveryMode. NON_PERSISTENT = 1: non-persistent; DeliveryMode. PERSISTENT = 2: PERSISTENT --> <property name = "deliveryMode" value = "1"/> </bean> </beans>
Producer Program
Package com. mq. spring. topic; import org. junit. test; import org. junit. runner. runWith; import org. springframework. jms. core. jmsTemplate; import org. springframework. jms. core. messageCreator; import org. springframework. test. context. contextConfiguration; import org. springframework. test. context. junit4.SpringJUnit4ClassRunner; import javax. annotation. resource; import javax. jms. *;/*** created on 2015/6/4 * @ author Dennisit@163.com * @ version 1.0 */@ RunWith (SpringJUnit4ClassRunner. class) @ ContextConfiguration (locations = {"classpath: spring-topic.xml"}) public class TopicSender {@ Resource (name = "providerJmsTemplate") private JmsTemplate jmsTemplate; @ Test public void send () {sendMqMessage (null, "spring activemq topic type message [with listener]! ");}/*** Note: if the specified destination is not displayed during sending. destination * @ param message */public void sendMqMessage (Destination destination, final String message) {if (null = destination) configured in spring xml) {destination = jmsTemplate. getdefadestindestination ();} jmsTemplate. send (destination, new MessageCreator () {public Message createMessage (Session session) throws JMSException {return session. createTextMessage (message) ;}}); System. out. println ("spring send text message... ");} public void setJmsTemplate (JmsTemplate jmsTemplate) {this. jmsTemplate = jmsTemplate ;}}
3. Consumer Configuration
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- Configure the JMS connection factory --> <bean id = "consumerConnectionFactory" class = "org. apache. activemq. activeMQConnectionFactory "> <property name =" brokerURL "value =" failover :( tcp: // 192.168.147.131: 61616) "/> <property name =" useAsyncSend "value =" true "/> <property name =" clientID "value =" consumerClienctConnect "/> </bean> <! -- Define Message Destination --> <bean id = "topic1Destination" class = "org. apache. activemq. command. activeMQTopic "> <constructor-arg value =" testSpringTopic1 "/> </bean> <! -- Configure the message consumer listener --> <bean id = "consumerMessageListener" class = "com. mq. spring. topic. ConsumerMessageListener"/> <! -- Message subscription client 1 --> <bean id = "consumerListenerClient1" class = "org. springframework. jms. listener. defaultMessageListenerContainer "> <property name =" connectionFactory "ref =" consumerConnectionFactory "/> <! -- Enable subscription mode --> <property name = "pubSubDomain" value = "true"/> <property name = "destination" ref = "topic1Destination"/> <property name = "subscriptionDurable "value =" true "/> <! --- Here is the ID of the receiving client. when the client is not online, the message is stored in the database, until it is consumed by the client of this ID --> <property name = "clientId" value = "consumerClient1"/> <property name = "messageListener" ref = "consumerMessageListener"/> <! -- Message response mode Session. AUTO_ACKNOWLEDGE automatically signs for the Session. the CLIENT_ACKNOWLEDGE client calls the acknowledge method to manually sign for the Session. DUPS_ OK _ACKNOWLEDGE does not have to be signed for. messages may be sent repeatedly --> <property name = "sessionAcknowledgeMode" value = "1"/> </bean> <! -- Message subscription client 2 --> <bean id = "consumerListenerClient2" class = "org. springframework. jms. listener. defaultMessageListenerContainer "> <property name =" connectionFactory "ref =" consumerConnectionFactory "/> <! -- Enable subscription mode --> <property name = "pubSubDomain" value = "true"/> <property name = "destination" ref = "topicDestination"/> <property name = "subscriptionDurable "value =" true "/> <! --- Here is the ID of the receiving client. when the client is not online, the message is stored in the database, until it is consumed by the client of this ID --> <property name = "clientId" value = "consumerClient2"/> <property name = "messageListener" ref = "consumerMessageListener"/> <! -- Message response mode Session. AUTO_ACKNOWLEDGE automatically signs for the Session. the CLIENT_ACKNOWLEDGE client calls the acknowledge method to manually sign for the Session. DUPS_ OK _ACKNOWLEDGE does not have to be signed for. messages may be sent repeatedly --> <property name = "sessionAcknowledgeMode" value = "1"/> </bean> </beans>
Consumer listening code
Package com. mq. spring. topic; import org. apache. commons. lang. builder. toStringBuilder; import javax. jms. JMSException; import javax. jms. message; import javax. jms. messageListener; import javax. jms. textMessage;/*** created on 2015/6/4 * @ author dennisit@163.com * @ version 1.0 */public class ConsumerMessageListener implements MessageListener {@ Override public void onMessage (Message message) {TextMessage tm = (TextMessage) message; try {System. out. println ("--------- message consumption ---------"); System. out. println ("message content: \ t" + tm. getText (); System. out. println ("Message ID: \ t" + tm. getJMSMessageID (); System. out. println ("Message Destination: \ t" + tm. getJMSDestination (); System. out. println ("--------- more information ---------"); System. out. println (ToStringBuilder. reflectionToString (tm); System. out. println ("-------------------------");} catch (JMSException e) {e. printStackTrace ();}}}
Running result:
Note: It is a learning course. The online materials are based on your personal understanding and incorrect understanding. We look forward to your guidance and suggestions to learn together.
For more information, see [http://www.cnblogs.com/dennisit/p/4552686.html].