springboot 集成activeMQ实现消息队列和双向队列

本文详细介绍如何在Spring Boot项目中整合ActiveMQ消息中间件,包括配置依赖、配置文件设置、消息生产者与消费者的实现方式等。通过示例代码演示如何发送与接收消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、引入依赖配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
</dependency>

二、application.properties配置文件中新增配置

# ActiveMQ--------------------------------------------------------------------------------------------------------------
# Specify if the default broker URL should be in memory. Ignored if an explicit broker has been specified.
# 默认为true表示使用内存的activeMQ,不需要安装activeMQ server
#spring.activemq.in-memory=false
# URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`
spring.activemq.broker-url=tcp://localhost:61616
# Login user of the broker.
spring.activemq.user=admin
# Login password of the broker.
spring.activemq.password=admin
# Trust all packages.
#spring.activemq.packages.trust-all=false
# Comma-separated list of specific packages to trust (when not trusting all packages).
#spring.activemq.packages.trusted=
# See PooledConnectionFactory.
#spring.activemq.pool.configuration.*=
# Whether a PooledConnectionFactory should be created instead of a regular ConnectionFactory.
spring.activemq.pool.enabled=true
# Maximum number of pooled connections.
spring.activemq.pool.max-connections=50
# Connection expiration timeout in milliseconds.
spring.activemq.pool.expiry-timeout=10000
# Connection idle timeout in milliseconds.
spring.activemq.pool.idle-timeout=30000
# 如果为True,则是Topic;如果是false或者默认,则是queue。
spring.jms.pub-sub-domain=false

三、新建消息生产者类

@Service("producer")
public class Producer {

    @Resource
    private JmsTemplate jmsTemplate;

    /**
     * 发送消息
     *
     * @param destination 发送到的队列
     * @param message     待发送的消息
     */
    public void convertAndSend(Destination destination, final String message) {
        jmsTemplate.convertAndSend(destination, message);
    }
}

四、新建消息消费者ABC

@Component
public class ConsumerA {

    /**
     * 使用JmsListener配置消费者监听的队列
     *
     * @param text 接收到的消息
     */
    @JmsListener(destination = "suimh_queue")
    public void receiveQueue(String text) {
        System.out.println("Consumer-A : 收到的报文为:" + text);
    }
}
@Component
public class ConsumerB {

    /**
     * 使用JmsListener配置消费者监听的队列
     *
     * @param text 接收到的消息
     */
    @JmsListener(destination = "suimh_queue")
    @SendTo("out.queue")
    public String receiveQueue(String text) {
        System.out.println("Consumer-B : 收到的报文为:" + text);
        return text;
    }
}
@Component
public class ConsumerC {

    /**
     * 使用JmsListener配置消费者监听的队列
     *
     * @param text 接收到的消息
     */
    @JmsListener(destination = "out.queue")
    public void consumerMessage(String text) {
        System.out.println("Consumer-C : 从out.queue队列收到的回复报文为:" + text);
    }
}

五、新建接口用来测试

/**
     * activeMQ测试方法(queue队列模式)
     * 如果想用Topic发布订阅模式,需要新建ActiveMQTopic实例,并修改配置文件spring.jms.pub-sub-domain=true
     *
     * @return String
     */
    @GetMapping(value = "/activeMqSendMes")
    @ResponseBody
    public String activeMqSendMes() {
        int num = 10;
        try {
            Destination destinationQueue = new ActiveMQQueue("suimh_queue");
            for (int i = 1; i <= num; i++) {
                producer.convertAndSend(destinationQueue, "这是queueProducer发送的第" + i + "个消息!");
            }
            return "activeMQ生产成功!";
        } catch (Exception e) {
            return "activeMQ生产失败!";
        }
    }

 

转载于:https://my.oschina.net/MinghanSui/blog/1615997

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值