1,https://activemq.apache.org/ 进入官网
找到对应的目录点击即可启动
我修改了一下apache-activemq-5.16.0-bin\apache-activemq-5.16.0\conf的activemq.xml文件
将111行左右的代码修改成下图即可
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://127.0.0.1:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://127.0.0.1:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://127.0.0.1:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://127.0.0.1:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://127.0.0.1:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
1,添加依赖
<!-- activemq-->
<!--ActiveMq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<!--消息队列连接池-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.0</version>
</dependency>
2.配置application.properties
# 应用服务 WEB 访问端口
server.port=8080
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/bbs_test?serverTimezone=UTC&characterEncoding=utf-8
spring.datasource.password=root
spring.datasource.username=root
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.packages.trust-all=true
# 在考虑结束之前等待的时间
spring.activemq.close-timeout= 15s
# 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。
spring.activemq.in-memory=true
#是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
spring.activemq.non-blocking-redelivery=false
# 等待消息发送响应的时间。设置为0等待永远
spring.activemq.send-timeout=0
#指定启动时创建的topic,多个的话逗号分隔,默认: []
spring.artemis.embedded.topics=publish.topics
#指定启动时创建的队列,多个用逗号分隔,默认: []
spring.artemis.embedded.queues=publish.queue
3,写一个ActiveMQConfig
package com.example.demo.config;
import org.springframework.beans.factory.annotation.Value;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;
@Configuration
public class ActiveMQConfig {
@Value("${spring.artemis.embedded.queues}")
private String queueName;
@Value("${spring.artemis.embedded.topics}")
private String topicName;
@Value("${spring.activemq.userl")
private String usrName;
@Value("${spring.activemq.password}")
private String password;
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
@Bean
public Queue queue(){
return new ActiveMQQueue(queueName);
}
@Bean
public Topic topic(){
return new ActiveMQTopic(topicName);
}
@Bean
public ActiveMQConnectionFactory connectionFactory() {
return new ActiveMQConnectionFactory(usrName, password, brokerUrl);
}
// 在Queue模式中,对消息的监听需要对containerFactory进行配置
@Bean("queueListener")
public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(false);
return factory;
}
//在Topic模式中,对消息的监听需要对containerFactory进行配置
@Bean("topicListener")
public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
// Set this to "true" to register a durable subscription,
//持久化消息
factory.setSubscriptionDurable(true);
factory.setClientId("A");
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(true);
return factory;
}
}
4,写一个生产者发送消息的接口
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;
@RestController
@RequestMapping("/send")
public class SendController {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Autowired
private Topic topic;
/**
* 生产者 queue
* @param str
* @return
*/
@PostMapping("/queue")
public String queue(@RequestParam(value = "s") String str){
sendMessage(this.queue, str);
System.out.println("success,queue队列发送成功"+str);
return "success,queue队列发送成功"+str;
}
/**
* 生产者 topic
* @param str
* @return
*/
@PostMapping("/topic")
public String topic(@RequestParam(value = "sss") String str){
sendMessage(this.topic, str);
return "success,topic发送成功"+str;
}
/**
* 发送消息,destination是发送到的队列,message是待发送的消息
* @param destination
* @param message
*/
private void sendMessage(Destination destination, final String message){
jmsMessagingTemplate.convertAndSend(destination, message);
}
}
5,queue模式的消费者
package com.example.demo.controller;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class QueueConsumerListener {
/**
* queue模式的消费者
* @param message
*/
@JmsListener(destination="${spring.artemis.embedded.queues}", containerFactory="queueListener")
public void readActiveQueue(String message) {
System.out.println("queue消费者接受到:" + message);
}
}
6,topic的消费者模式
package com.example.demo.controller;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class TopicConsumerListener {
//topic模式的消费者
@JmsListener(destination="${spring.artemis.embedded.topics}", containerFactory="topicListener")
public void readActiveQueue(String message) {
System.out.println("topic消费者接受到:" + message);
}
}