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
相关推荐
CoderJia程序员甲1 小时前
重学SpringBoot3-如何发送 Email
java·spring boot·后端·email
初晴~1 小时前
【spring】参数校验Validation
java·c++·spring boot·后端·python·spring·validation
chudaxiakkk1 小时前
记录spring-boot 3.X版本整合RocketMq
java·spring boot·rocketmq
wqq_9922502771 小时前
springboot基于微信小程序的农产品交易平台
spring boot·后端·微信小程序
forestsea2 小时前
Spring Boot 与 Java 决策树:构建智能分类系统
java·人工智能·spring boot·深度学习·决策树·机器学习·数据挖掘
LRcoding2 小时前
【Spring Boot】# 使用@Scheduled注解无法执行定时任务
java·spring boot·后端
夜色呦3 小时前
实验室管理自动化:Spring Boot技术的应用
spring boot·自动化·php
《源码好优多》4 小时前
基于Java Springboot出租车管理网站
java·开发语言·spring boot
求积分不加C8 小时前
Spring Boot中使用AOP和反射机制设计一个的幂等注解(两种持久化模式),简单易懂教程
java·spring boot·后端
夜色呦8 小时前
Spring Boot实验室管理系统:高效科研管理解决方案
数据库·spring boot·php