pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
Copy the code

Modify the application.properties configuration file

## URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`
# failover:(tcp://localhost:61616,tcp://localhost:61617)
# tcp://localhost:61616
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.user=admin
spring.activemq.password=admin
If the value is set to true, add the following dependency package. Otherwise, the JmsMessagingTemplate injection fails
spring.activemq.pool.enabled=false
Copy the code
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-pool</artifactId>
  <! - < version > 5.7.0 < / version > -- >
</dependency>
Copy the code

Message producer

package com.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;


/** * Message producer */
@Service("producer")
public class Producer {
    @Autowired JmsTemplate can also be injected. The JmsMessagingTemplate encapsulates the JmsTemplate
    private JmsMessagingTemplate jmsTemplate;
    // To send a message, destination is the queue to which it is sent, and message is the message to be sent
    public void sendMessage(Destination destination, final String message){ jmsTemplate.convertAndSend(destination,message); }}Copy the code

Message consumer

package com.mq;


import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;


/** * message consumer */
@Component
public class Consumer {
    // Use JmsListener to configure the queue on which the consumer listens, where text is the received message
    @JmsListener(destination = "Consumer")
    public void receiveQueue(String text) {
        System.out.println("Consumer received the following message :"+text); }}Copy the code

The code for consumer 2 is the same as above. Note that the message consumer class must have @Component, or @Service, so that the message consumer class is delegated to the Listener class. Principle similar to use SessionAwareMessageListener and implemented the MessageListenerAdapter message-driven POJO

test

package com.mq;

import com.mq.service.IMessageProducerService;
import org.apache.activemq.command.ActiveMQQueue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.jms.Destination;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ActiveMqApplicationTests {

	@Autowired
	private Producer producer;

	@Test
	public void contextLoads(a) throws InterruptedException {
		Destination destination = new ActiveMQQueue("Consumer");

		for(int i=0; i<100; i++){
			producer.sendMessage(destination, "News"+i); }}}Copy the code

The test results

. The message received by a Consumer is as follows: message 93 The message received by a Consumer is as follows: message 94 The message received by a Consumer is as follows: message 95 The message received by a Consumer is as follows: message 97 The message received by a Consumer is as follows: message 98 Consumer received messages as follows: 99 2019-01-31 17:24:02. 11336-690 the INFO/Thread - 4 O.S.W.C.S.G enericWebApplicationContext: Closing org.springframework.web.context.support.GenericWebApplicationContext@49e5f737: startup date [Thu Jan 31 17:23:56 CST 2019]; The root of the context hierarchy 17:24:02 2019-01-31. 11336-694 the INFO/Thread - 4 O.S.C.S upport. DefaultLifecycleProcessor: Stopping beansin phase 2147483647

Process finished with exit code 0
Copy the code

After the above steps, Spring Boot and Jms are basically integrated, is it very convenient to use!

Implement bidirectional queue

1. Let’s start with Consumer2, the code is as follows:

package com.mq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

@Component
public class Consumer2 {

    @JmsListener(destination = "Consumer")
    @SendTo("Consumer2")
    public String receiveQueue(String text) {
        System.out.println("The Consumer received the following message :"+text);
        return "return message"+text; }}Copy the code

ReceiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue: receiveQueue We found that the “Consumer2 “queue already has the following contents:

Enter the Browse interface to view:

Finally, take a look at the specific message received:

We find that the message in the queue is the value we return!

Reform Producer

Using the above example, we now modify Producer so that it can both produce packets and consume packets in the queue as follows:

package com.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;


/** * Message producer */
@Service("producer")
public class Producer {
    @Autowired JmsTemplate can also be injected. The JmsMessagingTemplate encapsulates the JmsTemplate
    private JmsMessagingTemplate jmsTemplate;
    // To send a message, destination is the queue to which it is sent, and message is the message to be sent
    public void sendMessage(Destination destination, final String message){
        jmsTemplate.convertAndSend(destination,message);
    }

    @JmsListener(destination="Consumer2")
    public void consumerMessage(String text){
        System.out.println("The reply message received from the Consumer2 queue is :"+text); }}Copy the code

The test results are as follows:

The Consumer received the following message: Message 93 Received the following reply message from the Consumer2 queue:returnMessage 93 The Consumer receives the following message: Message 94 The Consumer receives the following message: Message 95 The Consumer receives the following reply message from queue Consumer2:returnMessage 95 The Consumer receives the following message: Message 96 The Consumer receives the following message 97 The Consumer receives the following reply message from queue Consumer2:returnMessage 97 The Consumer receives the following message: Message 98 The Consumer receives the following message 99 The Consumer receives the following reply message from queue Consumer2:returnMessage message 99 17:39:14 2019-01-31. 7440-095 the INFO/Thread - 4 O.S.W.C.S.G enericWebApplicationContext: Closing org.springframework.web.context.support.GenericWebApplicationContext@c86b9e3: startup date [Thu Jan 31 17:39:07 CST 2019]; The root of the context hierarchy 17:39:14 2019-01-31. 7440-096 the INFO/Thread - 4 O.S.C.S upport. DefaultLifecycleProcessor: Stopping beansin phase 2147483647

Process finished with exit code 0
Copy the code

The original address: blog.csdn.net/liuchuanhon…