ebike-plus/ebike-payment/src/main/java/com/cdzy/payment/task/WxPayScheduledManager.java
2025-10-22 16:37:33 +08:00

58 lines
1.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}