目前,我有这样的:
@Scheduled(fixedRate=5000)
public void getSchedule(){
System.out.println("in scheduled job");
}
我可以改变这种使用引用到属性
@Scheduled(fixedRate=${myRate})
public void getSchedule(){
System.out.println("in scheduled job");
}
不过,我需要用编程方式获得的值,因此调度可以不用重新部署应用程序进行更改。 什么是最好的方法? 我意识到使用注释可能无法...
使用Trigger
可以实时计算的下一个执行时间。
这样的事情应该做的伎俩(改编自Javadoc文档@EnableScheduling
):
@Configuration
@EnableScheduling
public class MyAppConfig implements SchedulingConfigurer {
@Autowired
Environment env;
@Bean
public MyBean myBean() {
return new MyBean();
}
@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() {
myBean().getSchedule();
}
},
new Trigger() {
@Override public Date nextExecutionTime(TriggerContext triggerContext) {
Calendar nextExecutionTime = new GregorianCalendar();
Date lastActualExecutionTime = triggerContext.lastActualExecutionTime();
nextExecutionTime.setTime(lastActualExecutionTime != null ? lastActualExecutionTime : new Date());
nextExecutionTime.add(Calendar.MILLISECOND, env.getProperty("myRate", Integer.class)); //you can get the value from wherever you want
return nextExecutionTime.getTime();
}
}
);
}
}
你也可以用这个简单的方法:
private int refreshTickNumber = 10;
private int tickNumber = 0;
@Scheduled(fixedDelayString = "${some.rate}")
public void nextStep() {
if (tickNumber < refreshTickNumber) {
tickNumber++;
return;
}
else {
tickNumber = 0;
}
// some code
}
refreshTickNumber
在运行时完全可配置,可与使用@Value
注解。
您可以管理使用重启和的TaskScheduler调度ScheduledFuture:
@Configuration
@EnableScheduling
@Component
public class CronConfig implements SchedulingConfigurer , SchedulerObjectInterface{
@Autowired
private ScheduledFuture<?> future;
@Autowired
private TaskScheduler scheduler;
@Bean
public SchedulerController schedulerBean() {
return new SchedulerController();
}
@Bean(destroyMethod = "shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}
@Override
public void start() {
future = scheduler.schedule(new Runnable() {
@Override
public void run() {
//System.out.println(JOB + " Hello World! " + new Date());
schedulerBean().schedulerJob();
}
}, new Trigger() {
@Override public Date nextExecutionTime(TriggerContext triggerContext) {
Calendar nextExecutionTime = new GregorianCalendar();
Date lastActualExecutionTime = triggerContext.lastActualExecutionTime();
nextExecutionTime.setTime(convertExpresssiontoDate());//you can get the value from wherever you want
return nextExecutionTime.getTime();
}
});
}
@Override
public void stop() {
future.cancel(true);
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// TODO Auto-generated method stub
start();
}
}
接口启停:
public interface SchedulerObjectInterface {
void start();
void stop();
}
现在你可以停止,使用@Autowired SchedulerObjectInterface再次启动(重启)调度
文章来源: Scheduling a job with Spring programmatically (with fixedRate set dynamically)