Scheduling a job with Spring programmatically (with fixedRate set dynamically)
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@EnableSchedulingpublic 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(); } } ); }}
You can also use Spring Expression Language (SpEL) for this.
Once this value is initialized, you won't be able to update this value.
@Scheduled(fixedRateString = "#{@applicationPropertyService.getApplicationProperty()}")public void getSchedule(){ System.out.println("in scheduled job");}@Servicepublic class ApplicationPropertyService { public String getApplicationProperty(){ //get your value here return "5000"; }}
Also you can use this simple approach:
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
is fully configurable at runtime and can be used with @Value
annotation.