SpringBoot整合ActiveMQ

依赖

	 <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.2.5.RELEASE</version>
    </parent>

    <dependencies>
        <!--web启动器-->
        <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>
        <!--springBoot单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <exclusions>
                <exclusion>
                    <!--移出junit4-->
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

application.yml

server:
  port: 5000

spring:
  activemq:
    broker-url: tcp://139.224.82.98:61616 # 连接地址
    user: admin # 连接用户名
    password: admin # 连接密码
  jms:
    pub-sub-domain: false # 默认false false为队列 true主题

active-queue-name: springbootqueue # 定义目的地名称

active-topic-name: springboottopic

目的地(队列,主题)配置

  1. queue

    package site.wanjiahao.config;
    
    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;
    
    @Configuration
    public class JmsQueueConfig {
    
        @Value("${active-queue-name}")
        private String destinationName;
    
        // 配置队列
        @Bean
        public ActiveMQQueue queue() {
            return new ActiveMQQueue(destinationName);
        }
    }
    
  2. topic

    package site.wanjiahao.config;
    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;
    
    @Configuration
    public class JmsTopicConfig {
    
        @Value("${active-topic-name}")
        private String destinationName;
    
        // 配置队列
        @Bean
        public ActiveMQTopic topic() {
            return new ActiveMQTopic(destinationName);
        }
    }
    

    这两步也就想当于创建了queue和topic对象,并且赋予了其名称,以便日后好用,类似于xml中的<bean></bean>

producer

package site.wanjiahao.producer;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MQQueueProducer {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private ActiveMQQueue queue;

    @Autowired
    private ActiveMQTopic topic;

    // queue 发送
    public void sendQueueMessage() {
        jmsMessagingTemplate.convertAndSend(queue, "springBoot-Queue发送消息");
        System.out.println("发送成功");
    }

    // topic 发送
    public void sendTopicMessage() {
        jmsMessagingTemplate.convertAndSend(topic, "springBoot-Topic发送消息");
        System.out.println("发送成功");
    }
    
}

生产者即提供了点对点(queue)的实现,也提供了发布订阅(topic)的实现

consumer

  1. 同步接收

    package site.wanjiahao.consumer;
    
    import org.apache.activemq.command.ActiveMQQueue;
    import org.apache.activemq.command.ActiveMQTopic;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MQQueueConsumer {
    
        // 和JmsTemplate, 不过封装的方法更加好用
        @Autowired
        private JmsMessagingTemplate jmsMessagingTemplate;
    
        @Autowired
        private ActiveMQQueue queue;
    
        @Autowired
        private ActiveMQTopic topic;
    
        // queue 接收
        public void reviveQueue() {
            String receive = jmsMessagingTemplate.receiveAndConvert(queue, String.class);
            System.out.println(receive);
        }
    
        // topic 接收
        public void reviveTopic() {
            String receive = jmsMessagingTemplate.receiveAndConvert(topic, String.class);
            System.out.println(receive);
        }
    }
    

    消费者即提供了点对点(queue)的实现,也提供了发布订阅(topic)的实现

  2. 异步接收

    package site.wanjiahao.listener;
    
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.stereotype.Component;
    
    import javax.jms.JMSException;
    import javax.jms.TextMessage;
    
    @Component
    public class MessageListener {
    
        // 异步接收消息,可以接受queue topic,取决于application.yml spring.jms.pub-sub-domain的取值
        @JmsListener(destination = "${active-topic-name}")
        public void receive(TextMessage textMessage) throws JMSException {
            System.out.println("监听器" + textMessage.getText());
        }
    }
    

测试用例

package site.wanjiahao.test;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import site.wanjiahao.consumer.MQQueueConsumer;
import site.wanjiahao.producer.MQQueueProducer;

@SpringBootTest
public class SpringBootTest1 {

    @Autowired
    private MQQueueProducer mqQueueProducer;

    @Autowired
    private MQQueueConsumer mqQueueConsumer;

    // 发送取决于application.yml: spring.jms.pub-sub-domain的取值
    // spring.jms.pub-sub-domain=false
    @Test
    void sendQueue() {
        mqQueueProducer.sendQueueMessage();
    }

    @Test
    void reviveQueue() {
        mqQueueConsumer.reviveQueue();
    }

    // 发送取决于application.yml spring.jms.pub-sub-domain的取值
    // spring.jms.pub-sub-domain=true
    @Test
    void sendTopic() {
        mqQueueProducer.sendTopicMessage();
    }

    @Test
    void reviveTopic() {
        mqQueueConsumer.reviveTopic();
    }
}

定时发送

  1. 启动类上添加

    @EnableScheduling # 开启定时任务支持
    
  2. 生产者类编写

    // 三秒钟执行一次定时任务
    @Scheduled(fixedDelay = 3000)
    public void ScheduleSendMessage() {
        // 执行topic方法
        sendTopicMessage();
    }
    

需要注意的是:点对点模式与发布订阅模式的切换,一定需要修改配置文件中的 spring.jms.pub-sub-domain

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值