51 lines
1.8 KiB
Plaintext
51 lines
1.8 KiB
Plaintext
|
|
package com.securityControl.task.schedule.task;
|
|||
|
|
|
|||
|
|
import com.securityControl.task.service.impl.CatchPictureService;
|
|||
|
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
|
import org.jetbrains.annotations.NotNull;
|
|||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
|
import org.springframework.beans.factory.annotation.Value;
|
|||
|
|
import org.springframework.scheduling.Trigger;
|
|||
|
|
import org.springframework.scheduling.TriggerContext;
|
|||
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|||
|
|
import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
|||
|
|
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|||
|
|
import org.springframework.scheduling.support.CronTrigger;
|
|||
|
|
import org.springframework.stereotype.Component;
|
|||
|
|
|
|||
|
|
import java.time.LocalDateTime;
|
|||
|
|
import java.util.Date;
|
|||
|
|
|
|||
|
|
@Slf4j
|
|||
|
|
//@Component
|
|||
|
|
//@EnableScheduling
|
|||
|
|
public class DownloadPictureTask implements SchedulingConfigurer {
|
|||
|
|
|
|||
|
|
@Value("${jobs.cron.downloadPicture}")
|
|||
|
|
private String cron;
|
|||
|
|
|
|||
|
|
@Autowired
|
|||
|
|
private CatchPictureService catchPictureService;
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
|
|||
|
|
|
|||
|
|
// 动态使用cron表达式设置循环间隔
|
|||
|
|
taskRegistrar.addTriggerTask(new Runnable() {
|
|||
|
|
@Override
|
|||
|
|
public void run() {
|
|||
|
|
log.info("=======DownloadPicture Current time: {}", LocalDateTime.now());
|
|||
|
|
|
|||
|
|
log.info("=======DownloadPicture End time: {}", LocalDateTime.now());
|
|||
|
|
}
|
|||
|
|
}, new Trigger() {
|
|||
|
|
@Override
|
|||
|
|
public Date nextExecutionTime(@NotNull TriggerContext triggerContext) {
|
|||
|
|
// 使用CronTrigger触发器,可动态修改cron表达式来操作循环规则
|
|||
|
|
CronTrigger cronTrigger = new CronTrigger(cron);
|
|||
|
|
return cronTrigger.nextExecutionTime(triggerContext);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|