springboot 整合activemq
Windows系统安装ActiveMQ参考:https://www.cnblogs.com/pxblog/p/14580404.html
1、配置连接信息
spring:
activemq:
broker-url: tcp://192.168.1.28:61616
user: admin
password: 123456
packages:
trust-all: true
jms:
pub-sub-domain: true
引入maven信息
<!-- 整合消息队列ActiveMQ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <!-- 如果配置线程池则加入 --> <dependency> <groupId>org.messaginghub</groupId> <artifactId>pooled-jms</artifactId> </dependency> <!--消息队列连接池--> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.15.0</version> </dependency>
2、增加自定义配置
ActiveMqConfig.java
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 javax.jms.Queue; import javax.jms.Topic; @Configuration @EnableJms public class ActiveMqConfig{ @Value("${queue}") private String queueName; @Value("${topic}") private String topicName; @Bean public Queue queue(){ return new ActiveMQQueue(queueName); } @Bean public Topic topic(){ return new ActiveMQTopic(topicName); } }
生产者
ProducterService.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; import javax.jms.Queue; import javax.jms.Topic; import java.util.List; /** * 生产者 */ @Component public class ProducterService{ @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; @Autowired private Topic topic; public void send1(String msg){ jmsMessagingTemplate.convertAndSend(queue, msg); } public void sendQuene(Weixin object){ jmsMessagingTemplate.convertAndSend(queue, object); } public void sendTopic(Weixin object){ jmsMessagingTemplate.convertAndSend(topic, object); } }
消费者
ConsumerService.java
import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import java.util.List; /** * 消费者 */ @Component public class ConsumerService{ @JmsListener(destination = "${queue}") public void receive12(String str) { System.out.println(str); System.out.println(11121); } @JmsListener(destination = "${topic}") public void receive2(Object object) { System.out.println(object); System.out.println(1112); } @JmsListener(destination = "${topic}") public void receive3(List list) { System.out.println(list.size()); System.out.println(11144); } }
调用方式:
/** * 注入 */ @Autowired private ProducterService producterService; /** * 调用 */ producterService.sendQuene("11");
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)