消息队列之ActiveMQ
一、安装ActiveMQ
http://activemq.apache.org/components/classic/download/
二,如果我们是32位的机器,就双击win32目录下的activemq.bat,如果是64位机器,则双击win64目录下的activemq.bat,运行结果如下:
三,启动成功!成功之后在浏览器输入http://127.0.0.1:8161/地址,可以看到ActiveMQ的管理页面,用户名和密码默认都是admin,如下:
四,pom文件依赖如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-cloud</groupId>
<artifactId>activeMQ</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>activeMQ</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
五,修改application.yaml配置
server:
port: 9080
spring:
activemq:
broker-url: tcp://localhost:61616
in-memory: true
user: admin
password: admin
pool:
enabled: true
max-connections: 50
expiry-timeout: 10000
idle-timeout: 30000
jms:
pub-sub-domain: false
#默认情况下activemq提供的是queue模式(队列模式位false),若要使用topic模式需要配置pub-sub-domain为true(订阅模式)
六,新建Producer
@Service("producer")
public class Producer {
@Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
private JmsMessagingTemplate jmsTemplate;
// 发送消息,destination是发送到的队列,message是待发送的消息
public void sendMessage(Destination destination, final String message){
jmsTemplate.convertAndSend(destination, message);
}
七,新建Consumer
@Component
public class Consumer {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "mytest")
public void receiveQueue(String text) {
System.out.println("Consumer收到的报文为:"+text);
}
}
八,新建测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.jms.Destination;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActiveMqTest {
@Autowired
private Producer producer;
@Test
public void contextLoads() throws InterruptedException {
Destination destination = new ActiveMQQueue("mytest");
for(int i=0; i<10; i++){
producer.sendMessage(destination, "你好啊"+i);
}
}
}
启动测试类如下图:
经过上面的几个步骤,spring boot和Jms就基本上整合完成了,是不是使用起来很方便了
九、实现双向队列
1 修改producer
@JmsListener(destination="test")
public void consumerMessage(String text){
System.out.println("从out.queue队列收到的回复报文为:"+text);
}
2 新建consumer2
@Component
public class Consumer2 {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "mytest")
@SendTo("test") //该注解的意思是将return回的值,再发送队列中
public String receiveQueue(String text) {
System.out.println("Consumer2收到的报文为:"+text);
return "return message"+text;
}
}
再次运行如下图: