异步调用

/**
 * 核销提醒推送 定时
 */
@Scheduled(cron = "${schedule.mktCouponsUpdateStatusTime}")
public void updateStatusAndCancelVerificationPushService(){
    log.info("MktCouponsUpdateStatusTask mktCouponsUpdateStatusTime start...");
    LocalDateTime beginAt = LocalDateTime.now();
    WechatJob wechatJob = wechatJobService.selectWechatJobBySign(MktCouponsUpdateStatusTask.class.getSimpleName());
    if (!wechatJobService.verifyResult(wechatJob)) return;
    AtomicReference result = new AtomicReference<>(WechatJobResultEnum.SUCCESS.getCode());
    try {
        log.info( "主线程name【" + Thread.currentThread().getName() + "】id【" + Thread.currentThread().getId() + "】开启异步线程执行核销推送");
        CompletableFuture future = CompletableFuture.runAsync(() -> {
            log.info("异步线程name【" + Thread.currentThread().getName() + "】id【" + Thread.currentThread().getId() + "】开始执行核销推送任务");
            cancelVerificationPushService.cancelVerificationPush();
            log.info("异步线程name【" + Thread.currentThread().getName() + "】id【" + Thread.currentThread().getId() + "】结束执行核销推送任务");
        });
        future.whenComplete((res, throwable) -> {
            log.info("异步线程name【" + Thread.currentThread().getName() + "】id【" + Thread.currentThread().getId() + "】开始执行结束回调");
            if (throwable != null) {
                result.set(WechatJobResultEnum.FAIL.getCode());
                log.error("cancelVerificationPushService Exception: " + CommonUtil.getExceptionInfo(new Exception(throwable)));
            }
            LocalDateTime endAt = LocalDateTime.now();
            wechatJobService.insertWechatLogJob(wechatJob.getJobId(), beginAt, endAt, result.get(), "");
            log.info("cancelVerificationPushService push job end");
            log.info("异步线程name【" + Thread.currentThread().getName() + "】id【" + Thread.currentThread().getId() + "】结束回调执行完成");
        });
        log.info("主线程name【" + Thread.currentThread().getName() + "】id【" + Thread.currentThread().getId() + "】代码执行完成");
    } catch (Exception e) {
        result.set(WechatJobResultEnum.FAIL.getCode());
        log.error("cancelVerificationPushService Exception: " + CommonUtil.getExceptionInfo(e));
        LocalDateTime endAt = LocalDateTime.now();
        wechatJobService.insertWechatLogJob(wechatJob.getJobId(), beginAt, endAt, result.get(), "");
        log.info("cancelVerificationPushService push job end");
    }

}

你可能感兴趣的:(jav)