springboot如何整合activemq呢?
下文笔者讲述SpringBoot整合activemq的方法及示例分享,如下所示
SpringBoot整个activemq的实现思路
1.编写配置信息 2.引入相应的依赖 3.编写activemqConfig 4.编写消费者和生产者相关代码例:SpringBoot整个activemq的示例
1、配置连接信息 spring: activemq: broker-url: tcp://192.168.8.8: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("888888");
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。