@Component //组件
@EnableRabbit //开启交换机
public class RabbitMQTest {
@RabbitListener(bindings = {@QueueBinding(value = @Queue("computer"), exchange = @Exchange(value = "topic.myOrder",type = "topic"), key = "computer.#")})
public void test1(String text) {
System.out.println("1111" + text);
}
@RabbitListener(bindings = {@QueueBinding(value = @Queue("fruit"), exchange = @Exchange(value = "topic.myOrder",type = "topic"), key = "#.fruit")})
public void test2(String text) {
System.out.println("22222" + text);
}
}
@RabbitListener详细解释:
bindings参数指定要绑定的绑定事件,@QueueBinding指定详细参数,其中包含匹配交换机,创建队列。
value参数设置队列,在没有当前队列时,会自动创建一个,exchange参数指定交换机,没有时自动创建,除direct模式之外,其余模式都需要指定路由,type=“topic”,同时需要设置交换机value值,即“topic.( )”,用于指定交换机的路由。
key参数指定发送消息的路由key,会在发送消息的方法中设置,具体含义自行百度。
@Test
public void test5() {
rabbitTemplate.convertAndSend("topic.myOrder", "computer.fruit", "test");
}
第一个参数:指定交换机:需要和交换机value值相同。
第二个参数:指定routingKey:这是详细路由参数设置,也是@QueueBinding注解中的key值,含义遵循订阅模式。
第三个参数为内容。