一、什么是消息中间件
发送者将消息发送给消息服务器,消息服务器将消息存放在队列中,在合适的时候再讲消息转发给接受者。
在这种模式下,发送和接收是异步的,发送者无需进行等待,而且二者的生命周期不一定相同,发送消息的时候接收者不一定正在运行,而接收消息的时候发送者也不一定运行,但是消息中间件服务必须运行。如图:
消费者与消息中间件之间采用长连接方式通讯,在消费者监听到MQ中有消息存在时可以及时获取到消息。
二、ActiveMQ介绍
1.1ActiveMQ是个啥?
ActiveMQ是一个消息队列应用服务器。支持JMS规范。
1.2.ActiveMQ的消息模型
- Point-to-Point (P2P) 点对点消息模式
- Publish/Subscribe(Pub/Sub) 发布订阅
1.2.1.点对点
概念:
发送者(producer):发送消息的生产者;
消费者(consumer):接收消息的消费者;
消息队列(queue):用来存储发送者发送消息的中间件;
特点:
1.每一个消息都被发送到一个特定的队列,接收者从队列中获取消息,队列中保留着消息,直到消息被消费或者超时。
2.每一个消息只能被一个消费者消费,被消费后的消息将会从队列中移除。
3.发送者和接收者之间在时间上没有依赖性,即就是当发送者发送消息后,不管接收者是否运行,都不会影响到消息被发送到队列
4.接收者在成功接收到消息后需要向队列应该成功。
点对点模式适合A与B用户发消息,接收消息后,消息即不存在于队列。
1.2.2.发布订阅
概念:
发布者(Publisher):发布消息的生产者;
订阅者(Subscriber):接收消息的消费者;
主题(topic):用来存储发布者发布的消息;
特点:
1.每一个消息可以有多个消费者。
2.对于某一个主题的订阅者,他必须创建了一个订阅之后,才能消费发布者的消息,而且为了消费消息,订阅者需要保持运行状态,所以发布者和订阅者有时间上的依赖性。
三、ActiveMQ的安装
3.1.Windows下安装ActiveMQ
1.下载Windows版本的ActiveMQ压缩包,ActiveMQ官网地址。
2.将下载的安装包解压到然后进去bin目录
3.在bin目录中根据自己电脑的位数,进入不同文件夹,双击 activemq.bat 的批处理文件,运行ActiveMQ。
4.启动图如下图,默认端口为6161
5.登录,验证MQ是否启动成功,默认账户名,密码都是 admin
四、SpringBoot整合ActiveMQ
4.1.点对点模式下整合
1.引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot Activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
2.引入application.yml配置
server:
port: 8080
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
#自定义队列名称
queue: gothic-queue
3.创建QueueConfig,注意引包
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
@Component
public class ConfigQueue {
@Value("${queue}")
private String queueName;
//将队列注入到Springboot容器
@Bean
public Queue queue(){
return new ActiveMQQueue(queueName);
}
}
4.创建生产者
@Component
public class QueueProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
private int count = 1;
//设置五秒钟往队列发送一次消息
@Scheduled(fixedDelay = 5000)
public void send() {
String msg = "消息队列: 第 " + count + " 次发送消息";
//将消息转化并发送到消息队列
jmsMessagingTemplate.convertAndSend(queue, msg);
System.out.println("发送到队列中的消息为:" + msg);
count++;
}
}
5.创建消费者
@Component
public class QueueConsumer {
@JmsListener(destination = "${queue}")
public void receive(String msg) {
System.out.println("消费者接收到消息为:" + msg);
}
}
6.启动服务
@EnableScheduling
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
7.控制台打印结果以及ActiveMQ客户端查看
在ActiveMQ的管理平台可以看到生产的消息被消息,当把生产者和消费者部署在不通服务下时,会发现之间互不影响。
4.2.发布订阅
4.2.1.创建订阅者(消费者)
1.引入依赖,依赖于点对点相同
2.引入application.yml配置
server:
port: 8082
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
#### 开启发布订阅
jms:
pub-sub-domain: true
##自定义主题名称
topic: gothic-topic
3.创建订阅者
@Component
public class TopicConsumer {
@JmsListener(destination = "${topic}")
public void receiveTopic(String msg) {
System.out.println("消费者消费的消息为: " + msg);
}
}
4.创建启动类
@SpringBootApplication
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}
5.启动订阅者
4.2.2.创建发布者(生产者)
1.引入依赖,依赖于点对点相同
2.引入application.yml配置
server:
port: 8081
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
##自定义主题名称
topic: gothic-topic
3.创建TopicConfig
@Component
public class TopicConfig {
//获取主题名称
@Value("${topic}")
private String topicName;
//将topic注入到Springboot的容器中
@Bean
private Topic topic(){
return new ActiveMQTopic(topicName);
}
}
4.创建生产者
@Component
public class TopicProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Topic topic;
private int count = 1;
//每五秒发布一次
@Scheduled(fixedDelay = 5000)
public void sendTopic() {
String msg = "发布订阅: 第" + count + "次发布消息";
jmsMessagingTemplate.convertAndSend(topic, msg);
System.out.println(msg);
count++;
}
}
5.创建生产者的启动类
@EnableScheduling
@SpringBootApplication
public class ProducerApp {
public static void main(String[] args) {
SpringApplication.run(ProducerApp.class, args);
}
}
6.启动发布者
4.2.3.测试查看控制台
订阅者控制台打印
生产者控制台
至此Springboot集成ActiveMQ完成。