ActiveMQの概要(2)-要求-応答要求応答モード

ActiveMQの基本操作

1つは、 ActiveMQ要求/応答要求/応答モードを実現するSpringBootの方法です。

前の記事で実装したプロデューサーがメッセージの送信を担当し、もう一方の当事者が処理を担当します。これで、両方の当事者が互いにメッセージを送信できるようになります。要求と応答のアプローチは非常に一般的です。

プロデューサー構成クラス:

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;
	}

プロデューサービジネスコード:

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);
	 }

コンシューマー構成クラス:

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;
	}

消費者ビジネスコード:

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;
	}

メッセージの内容にIDを追加して要求と応答を識別する場合は、次の方法を使用できます。

プロデューサー側:

     @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);
     }

消費者側:

 @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";
	    }

 

 

おすすめ

転載: blog.csdn.net/csdnbeyoung/article/details/91045593