291-292

下面主要是视频中课件的截图,之前学rabbitmq的时候,这些还没有学,后面学了在做一个笔记

谷粒商城模拟关单(四十二)_封装谷粒商城模拟关单(四十二)_测试方法_02谷粒商城模拟关单(四十二)_测试方法_03谷粒商城模拟关单(四十二)_测试方法_04谷粒商城模拟关单(四十二)_测试方法_05谷粒商城模拟关单(四十二)_测试方法_06谷粒商城模拟关单(四十二)_测试方法_07

代码:

/**
 * @author WGR
 * @create 2020/8/6 -- 14:46
 */
@Configuration
public class MyMQConfig {

    @SneakyThrows
    @RabbitListener(queues = "order.release.order.queue")
    public void listener(OrderEntity entity, Channel channel,Message message){
        System.out.println("收到消息"+entity.getOrderSn());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //声明队列
    @Bean("orderDelayQueue")
    public Queue orderDelayQueue(){
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        arguments.put("x-dead-letter-routing-key","order.release.order");
        arguments.put("x-message-ttl",60000);

        //谷粒视频中是new出来的,这个相当于封装了一下,用起来更方便
        return QueueBuilder.durable("order.delay.queue").withArguments(arguments).build();
    }

    @Bean("orderReleaseOrderQueue")
    public Queue orderReleaseOrderQueue(){
        return QueueBuilder.durable("order.release.order.queue").build();
    }

    @Bean("orderEventExchange")
    public Exchange orderEventExchange(){
        return ExchangeBuilder.topicExchange("order-event-exchange").durable(true).build();
    }

    //绑定队列和交换机
    @Bean
    public Binding bingOrderDelayQueue(@Qualifier("orderDelayQueue") Queue queue,
                                     @Qualifier("orderEventExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange)
                .with("order.create.order").noargs();
    }

    //绑定队列和交换机
    @Bean
    public Binding bingOrderReleaseOrderQueue(@Qualifier("orderReleaseOrderQueue") Queue queue,
                                     @Qualifier("orderEventExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange)
                .with("order.release.order").noargs();
    }


}


//测试方法

    @ResponseBody
    @GetMapping("/test/createOrder")
    public String createOrderTest(){
        OrderEntity entity = new OrderEntity();
        entity.setOrderSn(UUID.randomUUID().toString());
        entity.setModifyTime(new Date());
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",entity);
        return "ok";
    }

演示的效果:

谷粒商城模拟关单(四十二)_测试方法_08谷粒商城模拟关单(四十二)_其他_09