Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduling a job with Spring programmatically (with fixedRate set dynamically)

Currently I have this :

@Scheduled(fixedRate=5000) public void getSchedule(){    System.out.println("in scheduled job"); } 

I could change this to use a reference to a property

@Scheduled(fixedRateString="${myRate}") public void getSchedule(){    System.out.println("in scheduled job"); } 

However I need to use a value obtained programmatically so the schedule can be changed without redeploying the app. What is the best way? I realize using annotations may not be possible...

like image 546
NimChimpsky Avatar asked Jan 31 '13 16:01

NimChimpsky


People also ask

How do you dynamically schedule a spring batch job?

When you want to schedule a job in Spring Batch, you annotate it with @Scheduled(cron = "your-cron-expression") . This is "static" since you can't put a variable for the cron expression : it is assigned before runtime. Here, you can put a variable in CronTrigger.

Is it possible to call a Spring scheduled method manually?

There is no limitation to call a scheduled method manually.

How do I schedule a cron job in Spring boot?

Java Cron ExpressionThe @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. The @Scheduled annotation is used to trigger the scheduler for a specific time period.


1 Answers

Using a Trigger you can calculate the next execution time on the fly.

Something like this should do the trick (adapted from the Javadoc for @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();                     }                 }         );     } } 
like image 112
ach Avatar answered Sep 22 '22 22:09

ach