一:安装ActiveMQ
官网下载,解压
二:创建springboot项目,引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--消息队列连接池-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.0</version>
</dependency>
三:yml配置
spring:
activemq:
broker-url: tcp://localhost:61616
in-memory: true
pool:
enabled: false
四:生产者发送消息
package com.lv.learning.jms;
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;
/**
* Producer
*
* @author lvjijun
* @version 1.0
* @Date 2020-10-19
*/
@Service("producer")
public class Producer {
/**
* 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
*/
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
/***
* description: 发送消息,destination是发送到的队列,message是待发送的消息
* @param destination
* @param message
* @return void
* @author lvjijun
* @time 2020-10-19 10:52
*/
public void sendMessage(Destination destination, final String message) {
jmsMessagingTemplate.convertAndSend(destination, message);
}
}
五:消费者接收消息
package com.lv.learning.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* Consumer
*
* @author lvjijun
* @version 1.0
* @Date 2020-10-19
*/
@Component
public class Consumer {
/***
* description: 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
* @param text
* @return
* @author lvjijun
* @time 2020-10-19 10:53
*/
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text) {
System.out.println("Consumer收到的报文为:"+text);
}
}
六:消费者接收并发送消息
package com.lv.learning.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
/**
* Consumer2
*
* @author lvjijun
* @version 1.0
* @Date 2020-10-19
*/
@Component
public class Consumer2 {
@JmsListener(destination = "mytest.queue")
@SendTo("out.queue")
public String receiveQueue(String text) {
System.out.println("Consumer2收到的报文为:"+text);
return "return message"+text;
}
}
在receiveQueue方法上面加上注解@SendTo("out.queue"),意思是将return回的值,再发送的"out.queue"队列中
七:生产者发送并接收消息
package com.lv.learning.jms;
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;
/**
* Producer
*
* @author lvjijun
* @version 1.0
* @Date 2020-10-19
*/
@Service("producer")
public class Producer {
/**
* 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
*/
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
/***
* description: 发送消息,destination是发送到的队列,message是待发送的消息
* @param destination
* @param message
* @return void
* @author lvjijun
* @time 2020-10-19 10:52
*/
public void sendMessage(Destination destination, final String message) {
jmsMessagingTemplate.convertAndSend(destination, message);
}
/***
* description: 监听队列接收消息
* @param text
* @return void
* @author lvjijun
* @time 2020-10-19 16:06
*/
@JmsListener(destination = "out.queue")
public void consumerMessage(String text) {
System.out.println("从out.queue队列收到的回复报文为:" + text);
}
}