关于jav:异步调用

41次阅读

共计 1975 个字符,预计需要花费 5 分钟才能阅读完成。

/**
 * 核销揭示推送 定时
 */
@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<String> result = new AtomicReference<>(WechatJobResultEnum.SUCCESS.getCode());
    try {log.info( "主线程 name【" + Thread.currentThread().getName() + "】id【" + Thread.currentThread().getId() + "】开启异步线程执行核销推送");
        CompletableFuture<Void> 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");
    }

}

正文完
 0