谷粒商城订单服务二(四十三)

293-299

代码提交到码云:https://gitee.com/dalianpai/gulimall

个人感觉这几章有点东西,还是第一次系统的接触到rabbitmq的这些应用。之前学的rabbitmq专题视频,正好是这些概念,等有时间去学完,还有几节没有看完,同时也在等尚硅古的rabbitmq视频。

代码具体如下:

订单服务:

/**
 * @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();
    }

    //绑定队列和交换机
    @Bean
    public Binding bingOrderReleaseOtherBinding(){
        return new Binding("stock.release.stock.queue",Binding.DestinationType.QUEUE,
                "order-event-exchange","order.release.other.#",null);
    }
}

监听服务:

/**
 * @author WGR
 * @create 2020/8/10 -- 19:53
 */
@RabbitListener(queues = "order.release.order.queue")
@Service
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    @SneakyThrows
    @RabbitHandler
    public void listener(OrderEntity entity, Channel channel, Message message){
        System.out.println("收到消息,准备关单"+entity.getOrderSn());

        try {
            orderService.closeOrder(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

}
    @Override
    public void closeOrder(OrderEntity entity) {
        OrderEntity orderEntity = this.getById(entity.getId());
        if(orderEntity.getStatus() == OrderStatusEnum.CREATE_NEW.getCode()){
            OrderEntity update = new  OrderEntity();
            update.setId(entity.getId());
            update.setStatus(OrderStatusEnum.CANCLED.getCode());
            this.updateById(update);

            OrderTo orderTo = new OrderTo();
            BeanUtils.copyProperties(orderEntity,orderTo);
            //立即发送消息
            rabbitTemplate.convertAndSend("order-event-exchange","order.release.other",orderTo);
        }

    }

image-20200810231524479

库存服务:

/**
 * @author WGR
 * @create 2020/8/6 -- 15:49
 */
@Configuration
public class MyRabbitConfig {


    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

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

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

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

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

    //绑定队列和交换机
    @Bean
    public Binding stockLockedBinding(@Qualifier("stockDelayQueue") Queue queue,
                                       @Qualifier("stockEventExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange)
                .with("stock.locked").noargs();
    }

    //绑定队列和交换机
    @Bean
    public Binding stockReleaseBing(@Qualifier("stockReleaseStockQueue") Queue queue,
                                              @Qualifier("stockEventExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange)
                .with("stock.release.#").noargs();
    }
}

监听类:

/**
 * @author WGR
 * @create 2020/8/6 -- 20:54
 */
@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class StockReleaseListener {

    @Autowired
    WareSkuService wareSkuService;

    @SneakyThrows
    @RabbitHandler
    public void handleStockLockedRelease(StockLockedTo to, Message message, Channel channel){
        System.out.println("收到解锁库存的消息");
        try{
            //当前消息是否被第二次及以后发送过来的
            message.getMessageProperties().getRedelivered()

            wareSkuService.unLockStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

    @SneakyThrows
    @RabbitHandler
    public void handleStockOrderRelease(OrderTo to, Message message, Channel channel){
        System.out.println("订单关闭准备解锁库存。。。。");
        try{
            wareSkuService.unLockStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}

image-20200810231817636

课件的截图:

image-20200810214301117 image-20200810214330651

消息的常见问题:

image-20200810222531358 image-20200810224704296 image-20200810225210232
posted @ 2020-08-10 23:23  天宇轩-王  阅读(446)  评论(0编辑  收藏  举报