|
|
@ -0,0 +1,45 @@ |
|
|
|
|
|
package com.heai.modules.task; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 定时任务调度示例(与业务解耦:只负责触发,逻辑在 service)。 |
|
|
|
|
|
* <p> |
|
|
|
|
|
*时间用 cron 配置(支持每天、每周等),见 {@code scheduler.demo.cron}。 |
|
|
|
|
|
* Spring 默认6 位:秒 分 时 日 月 周(年可选)。 |
|
|
|
|
|
* <ul> |
|
|
|
|
|
* <li>每天凌晨 2:00:{@code 0 0 2 * * ?}</li> |
|
|
|
|
|
* <li>每周一凌晨 2:00:{@code 0 0 2 ? * MON}</li> |
|
|
|
|
|
* <li>每周日 23:00:{@code 0 0 23 ? * SUN}</li> |
|
|
|
|
|
* </ul> |
|
|
|
|
|
*/ |
|
|
|
|
|
@Slf4j |
|
|
|
|
|
@Component |
|
|
|
|
|
public class DemoScheduler { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${scheduler.demo.enabled:false}") |
|
|
|
|
|
private boolean enabled; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* cron 未配置时使用:每天 02:00:00 |
|
|
|
|
|
*/ |
|
|
|
|
|
@Scheduled(cron = "${scheduler.demo.cron:0 0 2 * * ?}") |
|
|
|
|
|
// @Scheduled(fixedDelay = 5000)//启动项目后每5秒1次 |
|
|
|
|
|
public void scheduleDemoJob() { |
|
|
|
|
|
if (!enabled) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
log.info("=== DemoScheduler 开始 ==="); |
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("=== DemoScheduler 执行失败 ===", e); |
|
|
|
|
|
} |
|
|
|
|
|
log.info("=== DemoScheduler 结束 ==="); |
|
|
|
|
|
} |
|
|
|
|
|
} |