Thread.sleep 延时查询或延时查询前更新es缓存数据

2024-10-09 10:47:57 浏览数 (4)

Thread.sleep 延时查询或延时查询前更新es缓存数据

MQ消息的顺序性,或发送MQ的发送端未严格事务处理,可能存在数据未落库的情况,而导致接收端处理MQ消息的时候,查询为空。

代码语言:javascript复制
//demo1 订单
            Order Order = OrderMapper.getOrder(orderNo);
            if (Order == null) {
                //延时3秒
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    log.error("查询Order延时3秒异常:orderNo=[{}]",e,orderNo);
                }
                Order = OrderMapper.getOrder(orderNo);
                if (Order == null) {
                    throw new RuntimeException("Order is null , orderNo = "   orderNo);
                }
            }

        //demo2 查询es
        Order bo = orderSearchService.detail(orderNo);
        if(bo == null){
            //es尝试刷新一次
            List<String> orderNoList = Arrays.asList(orderNo);
            orderSearchService.refresh(orderNoList);
            Thread.sleep(3000);
            //重新查询
            bo = orderSearchService.detail(orderNo);
            if(bo == null){
                logger.info("es查询订单信息Order is null , orderNo = "   orderNo);
                return content;
            }
        }

1 人点赞