58 lines
1.8 KiB
Java
58 lines
1.8 KiB
Java
package com.cdzy.payment.task;
|
||
|
||
import com.cdzy.payment.config.WxPayConfig;
|
||
import jakarta.annotation.PostConstruct;
|
||
import jakarta.annotation.Resource;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||
import org.springframework.scheduling.support.CronTrigger;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
/**
|
||
* 微信支付定时作业调度器
|
||
*
|
||
* @author: yanglei
|
||
* @since: 2025-10-22 16:11
|
||
*/
|
||
@Slf4j
|
||
@Component
|
||
public class WxPayScheduledManager {
|
||
|
||
@Resource
|
||
private WxPayConfig wxPayConfig;
|
||
|
||
@Resource
|
||
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
|
||
|
||
@Resource
|
||
private WxPayTask wxPayTask;
|
||
|
||
@PostConstruct
|
||
public void init() {
|
||
String payCron = wxPayConfig.getPaySchedule();
|
||
String refundCron = wxPayConfig.getRefundSchedule();
|
||
|
||
// 注册支付状态检查任务
|
||
scheduleTask("CheckOrderStatus", wxPayTask::checkOrderStatus, payCron);
|
||
|
||
// 注册退款状态检查任务
|
||
scheduleTask("CheckRefundStatus", wxPayTask::checkRefundStatus, refundCron);
|
||
}
|
||
|
||
private void scheduleTask(String taskName, Runnable task, String cronExpression) {
|
||
if (cronExpression == null || cronExpression.trim().isEmpty()) {
|
||
log.warn("定时任务 [{}] 未配置Cron表达式,跳过注册", taskName);
|
||
return;
|
||
}
|
||
threadPoolTaskScheduler.schedule(() -> {
|
||
try {
|
||
log.debug("执行定时任务: {}", taskName);
|
||
task.run();
|
||
} catch (Exception e) {
|
||
log.error("定时任务执行异常: {}", taskName, e);
|
||
}
|
||
}, new CronTrigger(cronExpression));
|
||
|
||
log.info("已注册定时任务: {}, Cron表达式: {}", taskName, cronExpression);
|
||
}
|
||
} |