1.安装activemq,我是本地安装了虚拟机装的mq,具体安装可参考https://www.cnblogs.com/shamo89/p/7645651.html
安装成功后访问页面,192.192.192:8161/admin 默认账号密码admin,admin
2.添加mq的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId> --queue方式可以不添加
</dependency>
3.配置文件添加mq配置
使用queue(点对点)方式是,pool.enable要设置为false,默认使用的是queue方式,使用topic(订阅)方式是设置为true,同时要添加
spring.jms.pub-sub-domain=true。
4.创建生产者
package com.yypawn.lean.springbootactivemq.controller;
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;
/**
* @Author: yang
* @Description: 生产者
* @Date: 2018-12-28
* @Version: 1.0
*/
@Service("producer")
public class JMSProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
public void sendMessage(Destination destination,final String msg){
jmsMessagingTemplate.convertAndSend(destination,msg);
}
@JmsListener(destination = "out.queue")
public void consumerMessage(String text){
System.out.println("从out.queue队列收到回复为:" + text);
}
}
5.创建消费者
package com.yypawn.lean.springbootactivemq.controller;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
/**
* @Author: yang
* @Description: 消费者1
* @Date: 2018-12-28
* @Version: 1.0
*/
@Component
public class JMSConsumerOne {
@JmsListener(destination = "mytest.queue")
public void reveiveQueue(TextMessage msg, Session session){
try {
System.out.println("test-queue 消息:" + msg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.yypawn.lean.springbootactivemq.controller;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
/**
* @Author: yang
* @Description: 消费者2
* @Date: 2018-12-28
* @Version: 1.0
*/
@Component
public class JMSConsumerTwo {
@JmsListener(destination = "mytest.queue")
@SendTo("out.queue")//发送到out.queue队列,消息回馈
public String receiveQueue(String text){
System.out.println("Consumer2收到信息为:" + text);
return "return message-" + text;
}
}
5添加测试类
package com.yypawn.lean.springbootactivemq.controller;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Destination;
/**
* @Author: yang
* @Description: 测试
* @Date: 2018-12-28
* @Version: 1.0
*/
@RestController
@RequestMapping("test")
public class TestController {
@Autowired
private JMSProducer producer;
@GetMapping("queue")
public String queueSend(){
Destination destination = new ActiveMQQueue("mytest.queue");
for (int i = 0; i < 5; i++) {
producer.sendMessage(destination,"this is a queue test");
}
return "ok";
}
}
6.控制台打印
两个消费者 共收到5条消息,消费者1收到2条,消费者2收到3条,消费者2收到的消息之后,反馈到out.queue队列
查看队列消息
简单的点对点消息测试完成了,关于订阅类型的测试还在尝试中!