Simple Example of ActiveMQ in JMS Message Queue: activemq in jms queue
Not to mention nonsense. 1. Add the required maven dependency
<!-- active mq begin --><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>4.3.8.RELEASE</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.14.5</version></dependency><!-- active mq end -->
2. Add activemq configurations to the Spring configuration file.1) message sending
<! -- Configure the JMS connection factory --> <bean id = "connectionFactory" class = "org. apache. activemq. activeMQConnectionFactory "> <property name =" brokerURL "value =" tcp: // localhost: 61616 "/> </bean> <! -- Define the Message destination of the Queue type --> <bean id = "queueDestination" class = "org. apache. activemq. command. ActiveMQQueue"> <! -- Set the Queue name --> <constructor-arg> <value> bounter. queue </value> </constructor-arg> </bean> <! -- Define the Message destination of the Topic type --> <bean id = "topicDestination" class = "org. apache. activemq. command. ActiveMQTopic"> <! -- Set the Topic name --> <constructor-arg> <value> bounter. topic </value> </constructor-arg> </bean> <! -- Defines JmsTemplate to simplify message sending --> <bean id = "jmsTemplate" class = "org. springframework. jms. core. jmsTemplate "> <property name =" connectionFactory "ref =" connectionFactory "/> <property name =" defaultDestination "ref =" queueDestination "/> <property name =" receiveTimeout "value = "10000"/> </bean>
2) Message receiving (there are multiple receiving methods. Here we use Spring's Message Driven POJO asynchronous listening method)
<! -- Define the listener container for receiving messages --> <bean id = "messageListenerContainer" class = "org. springframework. jms. listener. defaultMessageListenerContainer "> <property name =" connectionFactory "ref =" connectionFactory "/> <! -- Configure the message listening destination, which can be the Queue or Topic defined above --> <! -- <Property name = "destination" ref = "queueDestination"/> --> <property name = "destination" ref = "topicDestination"/> <! -- Configure the custom message listener class --> <! -- <Property name = "messageListener" ref = "jmsQueueListener"/> --> <property name = "messageListener" ref = "jmsTopicListener"/> </bean>
3. Define message senders and message ReceiversFor more information, see the sender and Cycler packages in the project source code.
4. Test message sending and receivingFor details, refer to the unit test class in the project source code.
ActiveMQTest Is it easy! Try it on your own! If you have any suggestions, please leave a message! Github Source Code address: (Go Back To update tonight, work first)