一、安装ActiveMQ
到Apache官方网站下载最新的ActiveMQ的安装包,并解压到本地目录下,下载链接如下:http://activemq.apache.org/download.html
解压后运行bin/win64目录下的activemq.bat。
成功之后在浏览器输入http://127.0.0.1:8161/地址,可以看到ActiveMQ的管理页面,用户名和密码默认都是admin
二、使用ActionMQ
1.依赖
<!-- ActionMQ 消息队列 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2.application.properties配置
#activeMQ地址
spring.activemq.broker-url=tcp://localhost:61616
#是否启用内存模式(也就是不安装MQ,项目启动时同时也启动一个MQ实例)
spring.activemq.in-memory=true
#是否替换默认的connectionFactory
spring.activemq.pool.enabled=false
# 开启发布订阅 (点对面)
#spring.jms.pub-sub-domain=true
spring.jms.pub-sub-domain 默认为false 为点对点
如果要点对面 需要 spring.jms.pub-sub-domain=true
3.
需要在StringBootApplication类加上
@EnableJms
@ComponentScan
4.创建生产者
package com.example.controller;
import javax.jms.Destination;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ActionMQController {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
/**
* 点对点
*
* @throws InterruptedException
*/
@GetMapping("/action")
public void contextLoads() throws InterruptedException {
String msg = "my name is topic";
Destination destination = new ActiveMQQueue("mytest.queue");
for (int i = 0; i < 5; i++) {
jmsMessagingTemplate.convertAndSend(destination, msg);
}
}
@JmsListener(destination = "out.queue")
public void consumerMessage(String text) {
System.out.println("从out.queue队列收到的回复报文为:" + text);
}
/**
* 点对面
*/
@GetMapping("/topic")
public void sendMessage() {
String msg = "my name is topic";
ActiveMQTopic destination = new ActiveMQTopic("topic-my");
jmsMessagingTemplate.convertAndSend(destination, msg);
System.out.println("发布消息:" + msg);
}
}
5.创建消费者
点对点的消费者
package com.example.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text) {
System.out.println("Consumer收到的报文为:" + text);
}
@JmsListener(destination = "mytest.queue")
@SendTo("out.queue")
public String receiveQueue2(String text) {
System.out.println("Consumer2收到的报文为:" + text);
return "return message" + text;
}
}
点对面的消费者
package com.example.topic;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* 订阅模式消费者
*/
@Component
public class TopicCustomer {
/**
* 创建2个消费者
*
* @param text
*/
@JmsListener(destination = "topic-my")
public void subscriber(String text) {
System.out.println("消费者1:" + text);
}
@JmsListener(destination = "topic-my")
public void subscriber1(String text) {
System.out.println("消费者2:" + text);
}
}