technology-note/springboot系列/消息队列/springboot整合activeMQ(1).md
2018-11-10 17:10:02 +08:00

117 lines
4.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[id]:2018-09-05
[type]:javaee
[tag]:java,spring,springboot,activemq
**说明acitveMQ版本为5.9.1springboot版本为2.0.3**<br/>
## 一. 下载安装windows
&emsp;&emsp;官方下载地址:[点我跳转](http://activemq.apache.org/download-archives.html),选择windows安装包下载,然后解压解压后运行bin目录下的**activemq.bat**启动服务,无报错即可启动成功。默认管理地址为:[localhost:8161/admin](localhost:8161/admin),默认管理员账号密码为**admin**/**admin**。
## 二. springboot整合
### &emsp;1. 创建springboot项目
&emsp;&emsp;创建springboot web项目加入spring-boot-starter-activemq依赖。
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
```
&emsp;&emsp;然后编辑配合文件加上一个配置61616为activeMQ的默认端口暂时不做其他配置使用默认值。
```yml
spring:
activemq:
broker-url: tcp://localhost:61616
```
### &emsp;2. 创建生产者消费者
&emsp;&emsp;springboot中activeMQ的默认配置为**生产-消费者模式**,还有一种模式为**发布-订阅模式**后面再讲。项目目录如下:
![项目目录](./picFolder/pic1.png)
&emsp;&emsp;首先编写配置类Config.java代码如下
```java
@Configuration
public class Config {
@Bean(name = "queue2")
public Queue queue2(){
return new ActiveMQQueue("active.queue2");
}
@Bean(name = "queue1")
public Queue queue1(){
return new ActiveMQQueue("active.queue1");
}
}
```
上面的代码建立了两个消息队列queue1queue2,分别由queue1和queue2这两个Bean注入到Spring容器中。程序运行后会在activeMQ的管理页面->queue中看到如下
![队列](./picFolder/pic2.png)
&emsp;&emsp;生产者Producer.java代码如下
```java
@RestController
public class Producer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired()
@Qualifier("queue2")
private Queue queue2;
@Autowired()
@Qualifier("queue1")
private Queue queue1;
@GetMapping("/queue2")
public void sendMessage1(String message){
jmsMessagingTemplate.convertAndSend(queue2,"I'm from queue2:"+message);
}
@GetMapping("/queue1")
public void sendMessage2(String message){
jmsMessagingTemplate.convertAndSend(queue1,"I'm from queue1:"+message);
}
}
```
上面的类创建了两个GET接口访问这两个接口分别向queue1和queue2中发送消息。
消费者Comsumer.java代码如下
```java
@Component //将该类注解到Spring 容器中
public class Comsumer {
//接受消息队列1消息
@JmsListener(destination = "active.queue1") //监听active.queue1消息队列
public void readActiveQueue11(String message){
System.out.println(1+message);
}
//接受消息队列1消息
@JmsListener(destination = "active.queue1")
public void readActiveQueue12(String message){
System.out.println(2+message);
}
//接受消息队列2消息
@JmsListener(destination = "active.queue2")
public void readActiveQueue21(String message){
System.out.println(1+message);
}
//接受消息队列2消息
@JmsListener(destination = "active.queue2")
public void readActiveQueue22(String message){
System.out.println(2+message);
}
}
```
上面的代码定义了4个消费者每两个消费一个消息队列。
## &emsp;3. 运行
&emsp;&emsp;启动项目后分别向/queue1?message=niihao,/queue2?message=nihaoa发送http请求然后我们可以在控制台中看到如下输出
```
2I'm from queue2:nihaoa
1I'm from queue2:nihaoa
2I'm from queue1:nihao
1I'm from queue1:nihao
```
消息都成功被消费者消费,从打印结果也可看出生产者消费者的一个特点:一个消息只会被一个消费者消费。同时在管理页面中可以看到:
![运行结果](./picFolder/pic3.png)
每个消息队列有两个消费者队列进入了三个消息出了三个消息说明消息都被消费掉了如果注释掉消费者代码再次运行然后发送消息就会发现MessagesEnqueued数量大于MessagesDequeued然后再让消费者上线会立即消费掉队列中的消息。