@Configuration
@EnableScheduling
public class TimingHandle implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
try {
System.out.println("定时器启动");
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
// 半小时 1-2 30 * * * ? *
String conString = "0/2 * * * * ?";
CronTrigger trigger = new CronTrigger(conString);
Date nextExecDate = trigger.nextExecutionTime(triggerContext);
return nextExecDate;
}
});
}
}
Java定时任务(直接启动任务不用配置)
最新推荐文章于 2024-09-07 17:57:51 发布