Introduction to ActiveMQ (two)-Request-Response request response mode

ActiveMQ basic operation

One, SpringBoot way to achieve ActiveMQ Request-Response request-response mode

The producer implemented in the previous article is responsible for sending messages, and the other party is responsible for processing. Now both parties can send messages to each other. The request-response approach is common.

Producer configuration class:

import javax.jms.Destination;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

@Configuration
@EnableJms
public class ActiveMQConfig {
	
	private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
	private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
	private static final String BROKERURL = "failover://tcp://192.168.244.2:61616";
	private static final String TOPIC_NAME = "activemq-topic";
	private static final String QUEUE_NAME = "activemq-queue";
	
	@Bean
	public ActiveMQConnectionFactory activeMQConnectionFactory() {
		ActiveMQConnectionFactory factory = 
                 new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKERURL);
		return factory;
	}
	
	@Autowired
	private ActiveMQConnectionFactory factory;
	
	@Bean("activeMQTopic")
	public Destination ActiveMQTopic() {
		Destination queue = new ActiveMQTopic(TOPIC_NAME);
		return queue;
	}
	@Bean("activeMQQueue")
	public Destination ActiveMQQueue() {
		Destination queue = new ActiveMQQueue(QUEUE_NAME);
		return queue;
	}
	
	//生产者配置监听,实现请求应答模式,
	@Bean
	public JmsListenerContainerFactory<?> JmsListenerContainerFactoryTopic(){
		DefaultJmsListenerContainerFactory listener = 
            new DefaultJmsListenerContainerFactory();
		listener.setConnectionFactory(factory);
		//监听发布订阅模式TOPIC
		listener.setPubSubDomain(true);
		return listener;
	}
	
	@Bean
	public JmsListenerContainerFactory<?> JmsListenerContainerFactoryQueue(){
		DefaultJmsListenerContainerFactory listener = 
           new DefaultJmsListenerContainerFactory();
		listener.setConnectionFactory(factory);
		listener.setReplyPubSubDomain(false);
		//监听发布订阅模式FALSE
		listener.setPubSubDomain(false);
		return listener;
	}

Producer-side business code:

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/activemq")
public class ProducterController {
	
	@Autowired
	private JmsTemplate jmsTemplate;
	@Autowired
	@Qualifier("activeMQTopic")
	private Destination destinationTopic;
	@Autowired
	@Qualifier("activeMQQueue")
	private Destination destinationQueue;
	@Autowired
	private JmsMessagingTemplate template;
	
	@RequestMapping("sendTopic")
	public void sendTopicMessage() {
		String message = "TOPIC消息内容:"+System.currentTimeMillis();
		template.convertAndSend(destinationTopic,message);
	}
	
	@RequestMapping("sendQueue")
	public void sendQueueMessage(Message msg) {
		String message = "QUEUE消息内容:"+System.currentTimeMillis();
		jmsTemplate.convertAndSend(destinationTopic, message);
	}
	
	 @JmsListener(destination = 
         "topic",containerFactory="JmsListenerContainerFactoryTopic")
	 @JmsListener(destination = 
         "queue",containerFactory="JmsListenerContainerFactoryQueue")
	 public void receiveQueue(String text){
	     System.out.println("收到响应回来的消息:"+text);
	 }

Consumer configuration class:

import javax.jms.Destination;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

@Configuration
@EnableJms
public class ConsumerConfig {
	
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
	private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
	private static final String BROKERURL = "failover://tcp://192.168.244.2:61616";
	private static final String TOPIC_NAME = "activemq-topic";
	private static final String QUEUE_NAME = "activemq-queue";
	
	@Autowired
	private ActiveMQConnectionFactory factory;
	
	@Bean
	public ActiveMQConnectionFactory activeMQConnectionFactory() {
		ActiveMQConnectionFactory factory =
                new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
		return factory;
	}
	
	@Bean
	public JmsListenerContainerFactory<?> JmsListenerContainerFactoryTopic(){
		DefaultJmsListenerContainerFactory listener = 
               new DefaultJmsListenerContainerFactory();
		listener.setConnectionFactory(factory);
		//监听发布订阅模式TOPIC
		listener.setPubSubDomain(true);
		return listener;
	}
	
	@Bean
	public JmsListenerContainerFactory<?> JmsListenerContainerFactoryQueue(){
		DefaultJmsListenerContainerFactory listener = 
             new DefaultJmsListenerContainerFactory();
		listener.setConnectionFactory(factory);
		//监听点对点模式QUEUE
		listener.setPubSubDomain(false);
		return listener;
	}
	
	@Bean("activeMQTopic")
	public Destination ActiveMQTopic() {
		Destination queue = new ActiveMQTopic(TOPIC_NAME);
		return queue;
	}
	@Bean("activeMQQueue")
	public Destination ActiveMQQueue() {
		Destination queue = new ActiveMQQueue(QUEUE_NAME);
		return queue;
	}

Consumer business code:

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

@Component
public class ConsumerService {
	
	private static final String TOPIC_NAME = "activemq-topic";
	private static final String QUEUE_NAME = "activemq-queue";
	@Autowired
	private JmsTemplate jmsTemplate;
	
 @JmsListener(destination=TOPIC_NAME,containerFactory="JmsListenerContainerFactoryTopic")
	@SendTo("topic")
	public String reviceTopic(Message productMessage) throws JMSException {
		String msg = "接收消息:"+(((TextMessage)productMessage)).getText();
		return msg;
		
	}
	
@JmsListener(destination=QUEUE_NAME,containerFactory="JmsListenerContainerFactoryQueue")
	@SendTo("queue")
	public String reviceQueue(Message productMessage)throws JMSException {
		String msg = "接收消息:"+(((TextMessage)productMessage)).getText();
		System.out.println(msg);
		return msg;
	}

If you want to add ID to the content of the message to identify the request and response, you can use the following method

Producer side:

     @RequestMapping("sendQueue1")
	 public void sendQueueMessage1() {
		 jmsTemplate.send(destinationQueue,new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				TextMessage message = session.createTextMessage();
				message.setText("生产者发送消息");
				String uid = System.currentTimeMillis()+"";
				message.setJMSCorrelationID(uid);
				return message;
			}
		});
	 }
	 
	 @JmsListener(destination="callback")
     public void consumerMessage(String text) {
    	 System.out.print("收到队列的回复报文:"+text);
     }

Consumer end:

 @JmsListener(destination =QUEUE_NAME,containerFactory="JmsListenerContainerFactoryQueue")
	    @SendTo("callback")
	    public String receiveQueue(Message message) throws JMSException{
	    	System.out.println(((TextMessage)message).getText());
	    	System.out.println(message.getJMSMessageID());
	        return "I am consumer,I get your message";
	    }

 

 

Guess you like

Origin blog.csdn.net/csdnbeyoung/article/details/91045593