订单入库后异步推送

 

    @Autowired//注入ConfigurableApplicationContext 
    private ConfigurableApplicationContext context;


private void push(Order order){
            insertOrder(order);
            //消息通知新增订单
            context.publishEvent(new OrderInsertEvent(order));          
}
                  
/**
 * 订单插入事件
 */
public class OrderInsertEvent extends ApplicationEvent {
    public OrderInsertEvent(Order order) {
        super(order);
    }
}
@Log4j2
@Component
public class OrderInsertListener {

    @Autowired
    @Lazy
    private OrderNoticeService orderNoticeService;


    @EventListener
    public void onApplicationEvent(OrderInsertEvent event) {
        Order order = (OrderBasicRequestEntity) event.getSource();
        //消息推送订单入库通知
        orderNoticeService.notice(order);
    }
}
public interface OrderNoticeService {
    void notice(Order order);
}    
@Service
@Log4j2
public class OrderNoticeServiceImpl implements OrderNoticeService {

    @Async//开启异步
    @Override
    public void notice(Order order) {
        log.info("通知其他服务 :" + order.getId());
     try {
        //执行通知代码,try住异常,不让异常影响其他通知执行
          } catch (Exception e) {
            log.error("质控部分添加出错:", e);
          }
    }
}

转载于:https://www.cnblogs.com/huanghuanghui/p/11430837.html

你可能感兴趣的:(订单入库后异步推送)