ActiveMQ基本操作
一、SpringBoot方式实现ActiveMQ的 Request-Response 请求-响应模式
上一篇实现的生产者负责发送消息,另一方负责处理,现在实现双方都能给对方发消息,请求-应答这种方式很普遍。
生产者端配置类:
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";
}