SchedulingConfigurer/CronTrigger 正则表达式未按预期工作
SchedulingConfigurer/CronTrigger regex is not working as expected
我正在使用 Spring SchedulingConfigurer 和 CronTrigger 每 5 分钟触发一次作业,但代码没有按预期工作。
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
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;
@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {
@Bean(destroyMethod = "shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
System.out.println("Scheduler task is running: " + java.time.LocalDateTime.now());
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
CronTrigger trigger = new CronTrigger("* */5 * * * *");
return trigger.nextExecutionTime(triggerContext);
}
});
}
我遵循 spring 文档中提到的以下正则表达式规则。
"0 0 * * * " = 每天每个小时的顶部。
"/10 * * * * *" = 每十秒一次。
"0 0 8-10 * * *" = 每天的 8、9 和 10 点。
"0 0 6,19 * * *" = 6:00 AM 和 7:00 PM 每天。
"0 0/30 8-10 * * *" = 8:00、8:30、9:00、9:30、10:00 和 10:30 每天.
“0 0 9-17 * * MON-FRI”= 工作日朝九晚五整点
“0 0 0 25 12 ?” = 每个圣诞节午夜
我在下面尝试过,但没有成功。
Create a regular expression for Cron statement
Create a regular expression for Cron statement
终于在CronMaker的帮助下找到了解决方案。
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
System.out.println("Scheduler task is running: " + java.time.LocalDateTime.now());
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
CronTrigger trigger = new CronTrigger("0 0/5 * 1/1 * ?");
return trigger.nextExecutionTime(triggerContext);
}
});
我正在使用 Spring SchedulingConfigurer 和 CronTrigger 每 5 分钟触发一次作业,但代码没有按预期工作。
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
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;
@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {
@Bean(destroyMethod = "shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
System.out.println("Scheduler task is running: " + java.time.LocalDateTime.now());
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
CronTrigger trigger = new CronTrigger("* */5 * * * *");
return trigger.nextExecutionTime(triggerContext);
}
});
}
我遵循 spring 文档中提到的以下正则表达式规则。
"0 0 * * * " = 每天每个小时的顶部。
"/10 * * * * *" = 每十秒一次。
"0 0 8-10 * * *" = 每天的 8、9 和 10 点。
"0 0 6,19 * * *" = 6:00 AM 和 7:00 PM 每天。
"0 0/30 8-10 * * *" = 8:00、8:30、9:00、9:30、10:00 和 10:30 每天.
“0 0 9-17 * * MON-FRI”= 工作日朝九晚五整点
“0 0 0 25 12 ?” = 每个圣诞节午夜
我在下面尝试过,但没有成功。
Create a regular expression for Cron statement
Create a regular expression for Cron statement
终于在CronMaker的帮助下找到了解决方案。
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
System.out.println("Scheduler task is running: " + java.time.LocalDateTime.now());
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
CronTrigger trigger = new CronTrigger("0 0/5 * 1/1 * ?");
return trigger.nextExecutionTime(triggerContext);
}
});