SpringBoot2.0以上纯注解搭建消息队列环境,以及实现订阅模式

@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值,含义遵循订阅模式。

第三个参数为内容。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值