一、Linux下ActiveMQ安装
1.下载并解压
wget https://mirrors.tuna.tsinghua.edu.cn/apache//activemq/5.15.9/apache-activemq-5.15.9-bin.tar.gz tar zxvf apache-activemq-5.15.9-bin.tar.gz
2.运行
cd bin/ ./activemq start
3.进入管理界面
浏览器访问192.168.0.1:8161/admin/,默认用户名和密码为:admin/admin,控制台截图如下:
列表中信息含义如下:
Number Of Pending Messages:等待消费的消息 这个是当前未出队列的数量。
Number Of Consumers:消费者 这个是消费者端的消费者数量
Messages Enqueued:进入队列的消息 进入队列的总数量,包括出队列的。
Messages Dequeued:出了队列的消息 可以理解为是消费这消费掉的数量。
二、发送队列消息
队列模式特点:
客户端包括生产者和消费者。
队列中的一个消息只能被一个消费者使用。
消费者可以随时取消息。
application.properties配置如下:
#连接地址 spring.activemq.broker-url=tcp://192.168.0.1:61616 #如果是点对点(queue),那么此处默认应该是false,如果发布订阅,那么一定设置为true spring.jms.pub-sub-domain=false
ActivemqConfig.java配置:
/** * 点对点 */ @Bean public Queue queue() { return new ActiveMQQueue("active.queue"); }
消息生产者SendController.java发送代码如下:
/* * 发送 队列消息 */ @RequestMapping("/sendQueue") public String sendQueue() { String message = UUID.randomUUID().toString(); // 指定消息发送的目的地及内容 this.jmsMessagingTemplate.convertAndSend(this.queue, message); return "消息发送成功!message=" + message; }
消息消费者QueueCustomerController.java发送代码如下:
@RestController public class QueueCustomerController { /* * 监听和接收 队列消息 */ @JmsListener(destination="active.queue") public void readActiveQueue(String message) { System.out.println("接受到:" + message); } }
浏览器连续访问:http://localhost:8080/sendQueue,消息发送成功,消费者接收消息后打印的日志如下:
接受到:51d85d31-002d-4c9b-87df-a5ea64e8d6da 接受到:1c9dab0c-1d47-4556-95dc-601c8add44fe 接受到:d199ff29-d6ff-41d2-ada0-921d636f7ed1 接受到:4d50ba07-a48a-42b6-a67e-805cdeea662c 接受到:31fc16a9-8aec-4ee6-bbb3-a0ca22c19686
三、发送主题消息
主题模式特点:
客户端包括发布者和订阅者。
主题中的消息可以被所有订阅者消费。
消费者不能消费订阅之前发送的消息。
application.properties中修改属性:
spring.jms.pub-sub-domain=true
ActivemqConfig.java配置:
/** * 发布/订阅 */ @Bean public Topic topic() { return new ActiveMQTopic("active.topic"); }
消息生产者SendController.java发送代码如下:
/* * 发送 主题消息 */ @RequestMapping("/sendTopic") public String sendTopic() { String message = UUID.randomUUID().toString(); // 指定消息发送的目的地及内容 this.jmsMessagingTemplate.convertAndSend(this.topic, message); return "消息发送成功!message=" + message; }
添加两个消息消费者,TopicCustomerController.java代码如下:
/* * 监听和接收 主题消息1 */ @JmsListener(destination = "active.topic") public void readActiveTopic1(String message) { System.out.println("Customer1接受到:" + message); } /* * 监听和接收 主题消息2 */ @JmsListener(destination = "active.topic") public void readActiveTopic2(String message) { System.out.println("Customer2接受到:" + message); }
浏览器连续访问:http://localhost:8080/sendTopic,消息发送成功,两个消费者接收消息后打印的日志如下:
Customer1接受到:96c674b7-a310-487b-8088-2c5d049cfabf Customer2接受到:96c674b7-a310-487b-8088-2c5d049cfabf Customer1接受到:fee4fde8-6cbe-4d08-b179-e9462f6f1e74 Customer2接受到:fee4fde8-6cbe-4d08-b179-e9462f6f1e74 Customer1接受到:05d79335-ff33-41ec-ba13-a6f5c9d5bba0 Customer2接受到:05d79335-ff33-41ec-ba13-a6f5c9d5bba0 Customer1接受到:f8244df3-5504-478b-b86e-77823ab34dac Customer2接受到:f8244df3-5504<span class="hljs-number" style="margin: 0px; padding: 0px; font-size: inherit; line-height: inherit; color: rgb(174, 135, 250); word-wrap: inherit
发表评论 取消回复