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


    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
  • 相关阅读:
    objective c 中基本类型的操作
    [转载]Server.MapPath和Request.MapPath()的用法
    [转载]mstsc远程报:这可能是由于CredSSP 加密Oracle修正的两种完美解决方法
    [转载]忘记token怎么加入k8s集群
    ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded; 的解决办法
    [转载]AutoMapper 9.0的改造
    [转载]k8s注册节点提示Docker SystemdCheck]: detected cgroupfs" as the Docker cgroup dr iver. The r ecommended dr fiver is" systemd"
    [转载]Linux的Vi命令详解
    [转载]查看虚拟机里的Centos7的IP
    [转载]centos关闭swap分区
  • 原文地址:https://www.cnblogs.com/dalianpai/p/13472942.html
Copyright © 2020-2023  润新知