Spring-boot2.X整合Apache ActiveMQ5.X

整合步骤

1. 添加依赖

   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 整合消息队列ActiveMQ -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

        <!-- 如果配置线程池则加入 -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
        </dependency>
    </dependencies>

2. 开启jms支持

在springboot启动类上添加@EnableJms

3. 修改配置文件

在application.properties配置文件中添加

#####################################
# 默认只支持点对点模型
#####################################
#整合jms测试,安装在别的机器,防火墙和端口号记得开放
spring.activemq.broker-url=tcp://192.168.2.160:61616

#集群配置
#spring.activemq.broker-url=failover:(tcp://localhost:61616,tcp://localhost:61617)

spring.activemq.user=admin
spring.activemq.password=admin
#下列配置要增加依赖
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=100

封装点对点和订阅发布模型的API

这里我只列出图片源代码在github上:https://github.com/woxbwo/is-springboot/tree/master/is-springboot-activemq

1. 接口:

2. 实现类:

  3. 将Topic和Queue交给Spring容器管理,添加配置

4. 添加测试触发条件controller

测试点对点模型

添加实时监听

使用postman发送请求:http://127.0.0.1:8010/api/v1/order?msg=HelloActiveMq

结果:

  查看ActiveMQ管理界面

  

订阅发布模式测试

注意点:

1. 默认消费者并不会消费订阅发布类型的消息,这是由于springboot默认采用的是p2p模式进行消息的监听。修改配置:spring.jms.pub-sub-domain=true

2. @JmsListener如果不指定独立的containerFactory的话是只能消费queue消息。修改订阅者container:containerFactory="jmsListenerContainerTopic"

 

3. 需要给topic定义独立的JmsListenerContainer。此时需要在配置文件里面,注释掉 #spring.jms.pub-sub-domain=true

添加实时监听订阅者:

模拟三个订阅这

使用postman发送请求:http://127.0.0.1:8010/api/v1/topic?msg=HelloActiveMqTopic

测试结果:

查看ActiveMQ管理界面

 源代码地址https://github.com/woxbwo/is-springboot/tree/master/is-springboot-activemq

   

 

 

 

转载于:https://www.cnblogs.com/woxbwo/p/11304018.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
详细说一下一下代码:package com.mcloud.market.mq; import com.mcloud.common.constant.Constants; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.beans.factory.annotation.Value; 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 org.springframework.jms.core.JmsMessagingTemplate; import javax.jms.ConnectionFactory; import javax.jms.Queue; @Configuration public class ActiveMQConfig { @Value("${spring.activemq.broker-url}") private String brokerUrl; @Value("${spring.activemq.user}") private String username; @Value("${spring.activemq.password}") private String password; @Bean public Queue queue() { return new ActiveMQQueue(Constants.PREFIX + ".amount"); } @Bean(name = "messageQueue") public Queue amountQueue() { return new ActiveMQQueue(Constants.PREFIX + ".message"); } // 在Queue模式中,对消息的监听需要对containerFactory进行配置 @Bean("queueListener") public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory) { SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory(); factory.setConnectionFactory(connectionFactory); factory.setPubSubDomain(false); return factory; } @Bean public ConnectionFactory connectionFactory() { return new ActiveMQConnectionFactory(username, password, brokerUrl); } @Bean public JmsMessagingTemplate jmsMessageTemplate() { return new JmsMessagingTemplate(connectionFactory()); } }
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值