一.SpringBoot 整合 ActiveMQ:
1、 如果要想在项目之中去使用 ActiveMQ 组件,则应该为项目添加依赖支持库,修改 pom.xml 配置文件:
org.springframework.boot
spring-boot-starter-activemq
org.apache.activemq
activemq-pool
5.8.0
2、 修改 application.yml 配置文件进行 activemq 的配置;
spring:
activemq:
user: admin
password: admin
broker-url: tcp://192.168.43.161:61616
pool:
enabled: true
max-connections: 10
3.ActiveMQ有两种方式 queue 和topic
queue:点对点 比如有两个消费者不能同时消费同一个消息
topic:是点对面的 多个消费者可以同时消费同一个消息
4-1写配置类 activemqconfig
package com.zhu.order.activemqconfig;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import javax.jms.Queue;
import javax.jms.Topic;
@Configuration
public class ActiveMQConfig {
//用户名
@Value("${spring.activemq.user}")
private String usrName;
//密码
@Value("${spring.activemq.password}")
private String password;
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
//连接工厂
@Bean
public ActiveMQConnectionFactory connectionFactory() {
return new ActiveMQConnectionFactory(usrName, password, brokerUrl);
}
//queue方式 点对点
@Bean
public JmsListenerContainerFactory> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(connectionFactory);
return bean;
}
//topic方式 点对面
@Bean
public JmsListenerContainerFactory> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
//设置为发布订阅方式, 默认情况下使用的生产消费者方式
bean.setPubSubDomain(true);
bean.setConnectionFactory(connectionFactory);
return bean;
}
}
4-2 .消息监听 消费
4-2-1:queue方式:
package com.zhu.order.listener;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
@Component
public class QueueListener {
//destination :消息目的地
//containerFactory 配置里面的queue方式
// @SendTo("out.queue") 把消息发送到指定目的地 让消息发送者监听
@JmsListener(destination = "publish.queue", containerFactory = "jmsListenerContainerQueue")
@SendTo("out.queue")
public String receive(String text){
System.out.println("QueueListener: consumer-a 收到一条信息: " + text);
return "consumer-a received : " + text;
}
}
4-2-1:topic方式:
package com.zhu.order.listener;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
@Component
public class TopicListener {
@JmsListener(destination = "publish.topic", containerFactory = "jmsListenerContainerTopic")
public String receive(String text){
System.out.println("QueueListener: consumer-b-1 收到一条信息: " + text);
return "consumer-a received : " + text;
}
}
5.定义消息发送者 producer 可能是不同的系统 所以jar包,yml,配置类都一样 。当然多了一个目的地 地址。
5-1:yml
queueName: publish.queue
topicName: publish.topic
5-2:配置类
package com.zhu.product.activemqconfig;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import javax.jms.Queue;
import javax.jms.Topic;
@Configuration
public class ActiveMQConfig {
@Value("${queueName}")
private String queueName;
@Value("${topicName}")
private String topicName;
@Value("${spring.activemq.user}")
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);
}
@Bean
public JmsListenerContainerFactory> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(connectionFactory);
return bean;
}
@Bean
public JmsListenerContainerFactory> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
//设置为发布订阅方式, 默认情况下使用的生产消费者方式
bean.setPubSubDomain(true);
bean.setConnectionFactory(connectionFactory);
return bean;
}
}
5-3:发送消息的controller类
package com.zhu.product.controller;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Queue;
import javax.jms.Topic;
@RestController
@RequestMapping("/producer")
public class ProducerController {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Autowired
private Topic topic;
//queue方式
@RequestMapping("/queue")
public String queue(){
for (int i = 0; i < 20 ; i++){
jmsMessagingTemplate.convertAndSend(queue, "queue"+i);
}
return "queue 发送成功";
}
//监听消费者是否已经消费
@JmsListener(destination = "out.queue")
public void consumerMsg(String msg){
System.out.println(msg);
}
//topic方式
@RequestMapping("/topic")
public String topic(){
for (int i = 0; i < 10 ; i++){
jmsMessagingTemplate.convertAndSend(topic, "topic"+i);
}
return "topic 发送成功";
}
}
6.如果是queue方式一个消费会把指定目的地址的消息消费完位置
如果是多个 则一个执行完了再执行下一个 消息交替执行。
如果是topic方式 则每个消费都会得到指定目的地全部的消息