赞
踩
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.生产者(消息生产方
- package com.paas.springboot.demo01;
-
- import java.util.Date;
- import javax.annotation.Resource;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.stereotype.Component;
-
- @Component
- public class Sender {
-
- @Resource(name="hospSyncRabbitTemplate")
- private RabbitTemplate hospSyncRabbitTemplate;
-
- public void send1() {
- String context = "hello1 " + new Date();
- System.out.println("Sender : " + context);
- this.firstRabbitTemplate.convertAndSend("test.hospital.add", context);
- }
-
- public void send2() {
- String context = "hello2 " + new Date();
- System.out.println("Sender : " + context);
- this.firstRabbitTemplate.convertAndSend("test.hospital.add", context);
- }
-
- }
5.配置文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。