13、SpringBoot集成ActiveMQ

1、本地安装ActiveMQ

  • 从网上下载ActiveMQ相关包,本次案例是安装windows版本的,版本号为5.15.3
  • 下载之后解压进入\apache-activemq-5.15.3\bin下面,根据自己操作系统进行选择是启动64位还是32位
  • 进入相应目录之后执行activemq.bat,启动成功之后,在浏览器中输入:http://127.0.0.1:8161/admin/,用户名密码为:admin/admin,进入界面如下:

 

2、引入相关jar包依赖

<!-- RocketMQ依赖添加 -->
<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-client</artifactId>
</dependency>
<!-- 连接池配置 -->
<!--
    1、在配置文件中有用到连接池相关的配置时需要引入连接池相关的包
 -->
<!--<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-poll</artifactId>
</dependency>-->

3、添加相关配置文件

  • 注意spring.activemq.poll.enabled的配置为false,如果为true的话还需要添加连接池相关的jar包
#ActiveMQ相关配置
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
# 如果此处设置为true,需要添加activemq-pool的依赖包,否则会自动配置失败,无法注入JmsMessagingTemplate
spring.activemq.pool.enabled=false
#设置消费模式为queue还是topic,默认是queue,当值为true时表示是topic模式
#这种配置方式只能在queue和topic消费模式中二选一,不太友好
#spring.jms.pub-sub-domain=true

4、创建配置类

@Configuration
public class ActiveMQConfig {

    /**
     * 主题命名
     */
    public static final String TOPIC_NAME = "activemq.topic";

    /**
     * 队列命名
     */
    public static final String QUEUE_NAME = "activemq.queue";

    @Bean
    public Topic topic(){
        return new ActiveMQTopic(TOPIC_NAME);
    }

    @Bean
    public Queue queue(){
        return new ActiveMQQueue(QUEUE_NAME);
    }

    /**
     * 由于JmsListener只能消费queue下的消息,如果要消费topic模式的消息需要设置containerFacory
     * @return
     */
    @Bean
    public JmsListenerContainerFactory topicListernerContainerFactory(ConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        // 这个配置和在配置文件中配置的spring.jms.pub-sub-domain的含义一致,这样的话就可以消费topic模式的消息了
        factory.setPubSubDomain(true);
        return factory;
    }
}

5、创建生产消息的service

  • 通过注入JmsMessagingTemplate类调用它的convertAndSend方法生产消息
@Service
public class AMQSendMsg {

    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 发送topic消息
     * @param topic
     * @param msg
     */
    public void sendTopic(Topic topic,String msg){
        jmsMessagingTemplate.convertAndSend(topic,msg);
    }

    /**
     * 发送队列消息
     * @param queue
     * @param msg
     */
    public void sendQueue(Queue queue,String msg){
        jmsMessagingTemplate.convertAndSend(queue,msg);
    }
}

6、创建消费消息的service

  • 通过@JmsListener注解可以消费对应模式下的消息,由于SpringBoot默认是队列的方式进行消费,所以如果要按照topic的模式进行消费的话需要直接在配置文件中配置 spring.activemq.poll.enabled ,但是如果这么配置的话就只能按照其中一种模式进行消费,比较好的方案是添加一个工厂,在工厂中定义topic模式消费,该段代码添加在上面的配置类中(ActiveMQConfig)代码如下:
/**
 * 由于JmsListener只能消费queue下的消息,如果要消费topic模式的消息需要设置containerFacory
 * @return
 */
@Bean
public JmsListenerContainerFactory topicListernerContainerFactory(ConnectionFactory connectionFactory){
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    // 这个配置和在配置文件中配置的spring.jms.pub-sub-domain的含义一致,这样的话就可以消费topic模式的消息了
    factory.setPubSubDomain(true);
    return factory;
}
  • 消息消费者service类
@Service
public class AMQConsumer {
    private static Logger log = LoggerFactory.getLogger(AMQConsumer.class);

    @JmsListener(destination = ActiveMQConfig.QUEUE_NAME)
    public void queueConsumer(String msg){
        log.info("队列消息为:{}",msg);
    }

    @JmsListener(destination = ActiveMQConfig.TOPIC_NAME,containerFactory = "topicListernerContainerFactory")
    public void topicConsumer(String msg){
        log.info("topic消费消息为:{}",msg);
    }
}

7、创建Controller类进行测试

@RestController("/activemq")
@Api("ActiceMQ测试")
public class ActiveMQController {
    private static Logger log = LoggerFactory.getLogger(ActiveMQController.class);

    @Autowired
    private AMQSendMsg amqSendMsg;

    @Resource
    private Queue queue;

    @Resource
    private Topic topic;

    /**
     * 测试queue模式
     * @param userEntry
     * @return
     */
    @RequestMapping(value = "/queue" ,method = RequestMethod.POST)
    @ApiOperation("ActiveMQ-Queue-Test")
    public JsonResult queueTest(@RequestBody UserEntry userEntry){
        log.info("ActiveMQ的队列测试");
        amqSendMsg.sendQueue(queue, JSONObject.toJSONString(userEntry));
        return new JsonResult();
    }

    /**
     * 测试topic模式
     * @param userEntry
     * @return
     */
    @RequestMapping(value = "/topic" , method = RequestMethod.POST)
    @ApiOperation("Active-Topic-Test")
    public JsonResult topic(@RequestBody UserEntry userEntry){
        log.info("ActiveMQ消息订阅测试");
        amqSendMsg.sendTopic(topic,JSONObject.toJSONString(userEntry));
        return new JsonResult();
    }
}

8、进行测试

  • 启动springboot工程,本示例集成了swagger所以直接进行测试,首先进行队列模式测试,浏览器输入:http://localhost:8080/activemq/queue

 

控制台打印信息如下:

  • 测试topic模式:同样在浏览器中输入:http://localhost:8080/activemq/topic

 

控制台信息打印:

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值