rabbitmq-Direct交换器
Posted liaohongbin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rabbitmq-Direct交换器相关的知识,希望对你有一定的参考价值。
编写Direct模式
application.yml
mq:
config:
#交换器名称
exchange: log.direct
queue:
error:
#队列名称
name: log.error
routing:
key: log.error.routiang.key
info:
name: log.info
routing:
#路由键值
key: log.info.routiang.key
spring:
application:
name: liao
rabbitmq:
host: localhost
password: 123456
port: 5672
username: 123456
配置文件都用相同的就好 jar包还是之前的
1.消息接收:
InfoReceiver.java
@Component //@RabbitListener(queues = {"hello-queue1","hello-queue2"}) //@RabbitListener(queues = "hello-queue") //绑定队列 //QueueBinding value:绑定队列的名称 //@Queue value:配置队列名称 // autoDelete:是否是一个颗删除的队列 //exchange 配置交换器 //@Exchange value:交换器名称 // type:指定具体的交换器类型 ExchangeTypes //key 路由键 @RabbitListener( bindings [email protected](value = @Queue(value = "${mq.config.queue.info.name}",autoDelete ="true"), exchange [email protected](value ="${mq.config.exchange}",type = ExchangeTypes.DIRECT), key = "${mq.config.queue.info.routing.key}" ) ) public class InfoReceiver { @Autowired private AmqpTemplate amqpTemplate; //表示这个方法支持监听消息 如果为true,则指定这是有效负载类型的默认回退方法 @RabbitHandler public void process( String msg){ System.out.println(); // amqpTemplate.receive(msg); System.out.println("队列info接受到了消息:"+msg); } }
2.消息发送
@Component public class Send { @Autowired private AmqpTemplate amqpTemplate; @Value("${mq.config.exchange}") private String exchange; @Value("${mq.config.queue.info.routing.key}") private String routingkey; public void send(User msg){ //交换器 // 路游键 //消息 amqpTemplate.convertAndSend(exchange,routingkey,msg.toString()); } }
test
@RunWith(SpringRunner.class) @SpringBootTest(classes =SpringcloudMqApplication.class ) public class SpringcloudMqApplicationTests { @Autowired private Send send; @Test public void contextLoads() { //while (true){ int a=1; User user=new User(); user.setId(Long.valueOf(a++)); user.setUsername("yjakly"); send.send(user); //} } }
3.配置json格式的消息队列
发送配置
@Configuration public class RabbitMqConfig { @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setMessageConverter(new Jackson2JsonMessageConverter()); return template; } //发送消息设置 @Bean public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactoryPlus( SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory, Jackson2JsonMessageConverter jackson2JsonMessageConverter) { rabbitListenerContainerFactory.setMessageConverter(jackson2JsonMessageConverter); return rabbitListenerContainerFactory; } //json格式转换 @Bean public Jackson2JsonMessageConverter jackson2JsonMessageConverter(ObjectMapper xssObjectMapper) { return new Jackson2JsonMessageConverter(xssObjectMapper); } }
接收消息配置
@Configuration public class RabbitMqConfig { @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setMessageConverter(new Jackson2JsonMessageConverter()); return template; } @Bean public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); factory.setConnectionFactory(connectionFactory); factory.setMessageConverter(new Jackson2JsonMessageConverter()); return factory; } }
注意!! 发送消息实体类与接收消息实体类需要在一个包下 不然会报错找不到实体
以上是关于rabbitmq-Direct交换器的主要内容,如果未能解决你的问题,请参考以下文章