生产者
配置生产者
/**
* @author BNTang
*/
public class RoutingDirectConfig {
/**
* 声明交换机
*
* @return 交换机
*/
public DirectExchange directExchange() {
return new DirectExchange("directs");
}
/**
* 声明队列1 绑定info 和 warm
*
* @return 队列1
*/
public Queue queue1() {
return new Queue("queue1");
}
/**
* 声明队列2
*
* @return 队列2
*/
public Queue queue2() {
return new Queue("queue2");
}
/**
* 把队列1绑定到交换机里面指定info的路由key
*
* @return 交换机
*/
public Binding binding1() {
return BindingBuilder.bind(queue1()).to(directExchange()).with("info");
}
/**
* 把队列2绑定到交换机里面指定error的路由key
*
* @return 交换机
*/
public Binding binding2() {
return BindingBuilder.bind(queue2()).to(directExchange()).with("error");
}
}
发送消息
public void testRoutingDirect() {
this.rabbitTemplate.convertAndSend("directs", "info", "info 的日志信息");
this.rabbitTemplate.convertAndSend("directs", "warm", "warm 的日志信息");
this.rabbitTemplate.convertAndSend("directs", "debug", "debug 的日志信息");
this.rabbitTemplate.convertAndSend("directs", "error", "error 的日志信息");
System.out.println("消息发送成功");
}
消费者
消费消息
/**
* @author BNTang
*/
public class RoutingDirectConsumer {
(bindings = {
(
value = ("queue1"),
key = {"info", "warm"},
exchange = (name = "directs", type = ExchangeTypes.DIRECT)
)
})
public void receive1(String message) {
System.out.println("消费者【1】接收到消息:" + message);
}
(bindings = {
(
value = ("queue2"),
key = {"debug", "error"},
exchange = (name = "directs", type = ExchangeTypes.DIRECT)
)
})
public void receive2(String message) {
System.out.println("消费者【2】接收到消息:" + message);
}
}
测试方式同之前章节的一样。