rabbitMQ+Springboot使用

引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

 创建队列,交换机,并绑定关系

@Configuration
public class RabbitMQSMSConfig {
    public static final String SMS_EXCHANGE="sms-exchange";
    public static final String SMS_QUEUE="sms-queue";
    @Bean(SMS_EXCHANGE)
    public Exchange exchange(){
        return ExchangeBuilder.topicExchange(SMS_EXCHANGE).durable(true).build();
    }
    @Bean(SMS_QUEUE)
    public Queue queue(){
//        return new Queue(SMS_QUEUE);
        return QueueBuilder.durable(SMS_QUEUE).build();
    }
    @Bean
    public Binding smsBinding(@Qualifier(SMS_EXCHANGE) Exchange exchange, @Qualifier(SMS_QUEUE)Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("imooc.sms.#").noargs();
    }
}

发送消息

        rabbitTemplate.convertAndSend(RabbitMQSMSConfig.SMS_EXCHANGE,
"imooc.sms.send.login", GsonUtils.object2String(mqcontext));

监听消息

@Component
public class RabbitMQSMSConsumer {
    @RabbitListener(queues = {RabbitMQSMSConfig.SMS_QUEUE})
    public void watchQueue(String pyload, Message message){
//        pyload就是发送的信息内容
//        获取routingkey
        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
        SMSContentQO smsContentQO = GsonUtils.stringToBean(pyload, SMSContentQO.class);

    }
}

死信队列的实现

@Bean("queueB")
public Queue queueB(){
    Map<String, Object> arguments  = new HashMap<>();
    //设置死信交换机
    arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_XCHANGE);
    //设置死信RoutingKey
    arguments.put("x-dead-letter-routing-key","YD");
    //设置ttl
    arguments.put("x-message-ttl",40000);

    return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值