springboot集成activemq,并配置多个mq

maven依赖

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

配置项

package com.demo;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;

/**
 * <p>
 * <code>ActiveMqConfig</code>
 * </p>
 * Description:
 */
@EnableJms
@Configuration
public class ActiveMqConfig {

    @Value("${mq1.brokerURL}")
    private String mqOneBrokerUrl;
    @Value("${mq1.userName}")
    private String mqOneUserName;
    @Value("${mq1.password}")
    private String mqOnePassword;

    @Value("${mq2.brokerURL}")
    private String mqTwoBrokerUrl;
    @Value("${mq2.userName}")
    private String mqTwoUserName;
    @Value("${mq2.password}")
    private String mqTwoPassword;

    @Bean(name = "oneConnectionFactory")
    public ConnectionFactory oneConnectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(mqOneBrokerUrl);
        connectionFactory.setUserName(mqOneUserName);
        connectionFactory.setPassword(mqOnePassword);
        return connectionFactory;
    }

    @Bean(name = "oneJmsTemplate")
    public JmsTemplate oneJmsTemplate(@Qualifier("oneConnectionFactory") ConnectionFactory oneConnectionFactory) {
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(oneConnectionFactory);
        return template;
    }

    @Bean(name = "oneMessageListenerContainer")
    public DefaultJmsListenerContainerFactory oneMessageListenerContainer(
            @Qualifier("oneConnectionFactory") ConnectionFactory oneConnectionFactory) {
        DefaultJmsListenerContainerFactory listenerContainer = new DefaultJmsListenerContainerFactory();
        listenerContainer.setConnectionFactory(oneConnectionFactory);
        return listenerContainer;
    }

    @Bean(name = "twoConnectionFactory")
    public ConnectionFactory twoConnectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(mqTwoBrokerUrl);
        connectionFactory.setUserName(mqTwoUserName);
        connectionFactory.setPassword(mqTwoPassword);
        return connectionFactory;
    }

    @Bean(name = "twoJmsTemplate")
    public JmsTemplate twoJmsTemplate(@Qualifier("twoConnectionFactory") ConnectionFactory twoConnectionFactory) {
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(twoConnectionFactory);
        return template;
    }

    @Bean(name = "twoMessageListenerContainer")
    public DefaultJmsListenerContainerFactory twoMessageListenerContainer(
            @Qualifier("twoConnectionFactory") ConnectionFactory twoConnectionFactory) {
        DefaultJmsListenerContainerFactory listenerContainer = new DefaultJmsListenerContainerFactory();
        listenerContainer.setConnectionFactory(twoConnectionFactory);
        return listenerContainer;
    }
}

发送端

    @Autowired
    @Qualifier("oneJmsTemplate")
    private JmsTemplate oneJmsTemplate;

    @Autowired
    @Qualifier("twoJmsTemplate")
    private JmsTemplate twoJmsTemplate;

    public void sendMessageToMqOne(String destinationName, String message) {
        oneJmsTemplate.send(destinationName, session -> session.createTextMessage(message));
    }
    public void sendMessageToMqTwo(String destinationName, String message) {
        twoJmsTemplate.send(destinationName, session -> session.createTextMessage(message));
    }

消费端

package com.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * <p>
 * <code>ConsumerListener </code>
 * </p>
 * Description: mq监听
 *
 */
@Slf4j
@Component
public class ConsumerListener {

    /**
     * 监听消费MQ队列中的消息
     */
    @JmsListener(containerFactory = "oneMessageListenerContainer", destination = "oneDestination")
    public void oneListenQueue(String msgText) {
	// 进行业务处理
	}

	/**
     * 监听消费MQ队列中的消息
     */
    @JmsListener(containerFactory = "twoMessageListenerContainer", destination = "twoDestination")
    public void twoListenQueue(String msgText) {
	// 进行业务处理
	}
}

配置项示例

mq1.brokerURL = failover:(tcp://xxx:61616,tcp://xxx:61617)?jms.prefetchPolicy.queuePrefetch=50&jms.redeliveryPolicy.maximumRedeliveries=1&randomize=false&initialReconnectDelay=1000&maxReconnectDelay=30000
mq1.userName = xxx
mq1.password = xxx
mq2.brokerURL = failover:(tcp://xxx:61616,tcp://xxx:61617)?jms.prefetchPolicy.queuePrefetch=50&jms.redeliveryPolicy.maximumRedeliveries=1&randomize=false&initialReconnectDelay=1000&maxReconnectDelay=30000
mq2.userName = xxx
mq2.password = xxx
相关推荐
阿华的代码王国2 分钟前
【Spring】——SpringBoot项目创建
java·spring boot·后端·启动类·target文件
转世成为计算机大神10 分钟前
网关 Spring Cloud Gateway
java·网络·spring boot·1024程序员节
paopaokaka_luck17 分钟前
基于Spring Boot+Vue的助农销售平台(协同过滤算法、限流算法、支付宝沙盒支付、实时聊天、图形化分析)
java·spring boot·小程序·毕业设计·mybatis·1024程序员节
lexusv8ls600h21 分钟前
微服务设计模式 - 网关路由模式(Gateway Routing Pattern)
spring boot·微服务·设计模式
咕哧普拉啦24 分钟前
乐尚代驾十订单支付seata、rabbitmq异步消息、redisson延迟队列
java·spring boot·mysql·spring·maven·乐尚代驾·java最新项目
cooldream20091 小时前
Spring Boot中集成MyBatis操作数据库详细教程
java·数据库·spring boot·mybatis
Java白菜治2 小时前
SpringBoot基础系列学习(五):JdbcTemplate 访问数据库
数据库·spring boot·jdbc·jdbctemplate
java知路2 小时前
springboot 基于google 缓存,实现防重复提交
spring boot·后端·缓存
张保瑞3 小时前
十五:java web(7)-- Spring Boot
java·spring boot
bytebeats3 小时前
我用 Spring AI 集成 OpenAI ChatGPT API 创建了一个 Spring Boot 小程序
spring boot·chatgpt·openai