当前位置:   article > 正文

Spring Boot配置多个RabbitMQ_多个mq配置怎么默认指定一个containerfactory

多个mq配置怎么默认指定一个containerfactory
1.application.yml
 
spring:
    rabbitmq:
      hospSync:
        host: localhost
        port: 5672
        username: test
        password: test
        virtual-host: test
      pay:
        host: localhost
        port: 5672
        username: demo
        password: demo
        virtual-host: demo
2.配置mq
package com.medrd.hpay.config;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class RabbitMqConfiguration{

    @Bean(name="hospSyncConnectionFactory")
    @Primary
    public ConnectionFactory hospSyncConnectionFactory(
            @Value("${spring.rabbitmq.hospSync.host}") String host,
            @Value("${spring.rabbitmq.hospSync.port}") int port,
            @Value("${spring.rabbitmq.hospSync.username}") String username,
            @Value("${spring.rabbitmq.hospSync.password}") String password,
            @Value("${spring.rabbitmq.hospSync.virtual-host}") String virtualHost){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);

        return connectionFactory;
    }

    @Bean(name="hPayConnectionFactory")
    public ConnectionFactory hPayConnectionFactory(
            @Value("${spring.rabbitmq.pay.host}") String host,
            @Value("${spring.rabbitmq.pay.port}") int port,
            @Value("${spring.rabbitmq.pay.username}") String username,
            @Value("${spring.rabbitmq.pay.password}") String password,
            @Value("${spring.rabbitmq.pay.virtual-host}") String virtualHost){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }

    @Bean(name="hospSyncRabbitTemplate")
//    @Primary
    public RabbitTemplate firstRabbitTemplate(
            @Qualifier("hospSyncConnectionFactory") ConnectionFactory connectionFactory
    ){
        RabbitTemplate hospSyncRabbitTemplate = new RabbitTemplate(connectionFactory);
        //使用外部事物
        //ydtRabbitTemplate.setChannelTransacted(true);
        return hospSyncRabbitTemplate;
    }

    @Bean(name="hPayRabbitTemplate")
    @Primary
    public RabbitTemplate secondRabbitTemplate(
            @Qualifier("hPayConnectionFactory") ConnectionFactory connectionFactory
    ){
        RabbitTemplate hPayRabbitTemplate = new RabbitTemplate(connectionFactory);
        //使用外部事物
        //lpzRabbitTemplate.setChannelTransacted(true);
        return hPayRabbitTemplate;
    }

        @Bean(name="hospSyncContainerFactory")
    public SimpleRabbitListenerContainerFactory hospSyncFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("hospSyncConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }


    @Bean(name="hPayContainerFactory")
    public SimpleRabbitListenerContainerFactory hPayFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("hPayConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

}


3.监听(消费方)

package com.medrd.hpay.domain;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

@Component
public class RabbitMqMessage {

    private static final Logger logger = LoggerFactory.getLogger(RabbitMqMessage.class);

    @RabbitListener(queues = {"test.hospital.add","test.hospital.update","test.hospital.delete"}, containerFactory="hospSyncContainerFactory")
    public void hospitalAdd(@Header("amqp_receivedRoutingKey") String routeKey, Object data) {
        logger.debug("Reciver routeKey: {}, data: {}", data);
    }

    @RabbitListener(queues = {"demo.add","demo.update"}, containerFactory="hPayContainerFactory")
    public void hPayAdd(@Header("amqp_receivedRoutingKey") String routeKey, String data) {
        System.out.println(data);
        System.out.println();
    }


}
 

4.生产者(消息生产方

  1. package com.paas.springboot.demo01;
  2. import java.util.Date;
  3. import javax.annotation.Resource;
  4. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  5. import org.springframework.stereotype.Component;
  6. @Component
  7. public class Sender {
  8. @Resource(name="hospSyncRabbitTemplate")
  9. private RabbitTemplate hospSyncRabbitTemplate;
  10. public void send1() {
  11. String context = "hello1 " + new Date();
  12. System.out.println("Sender : " + context);
  13. this.firstRabbitTemplate.convertAndSend("test.hospital.add", context);
  14. }
  15. public void send2() {
  16. String context = "hello2 " + new Date();
  17. System.out.println("Sender : " + context);
  18. this.firstRabbitTemplate.convertAndSend("test.hospital.add", context);
  19. }
  20. }


5.配置文件

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



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/617041
推荐阅读
相关标签
  

闽ICP备14008679号