101

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...

M. Justin
  • 14,487
  • 7
  • 91
  • 130
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • You say "without redeploying the app". Changing a property reference can be done with an app restart without a redeploy (e.g. through updating a system property and then restarting). Is that sufficient, or do you want to be able to change it without a redeploy or a restart? – M. Justin Dec 22 '20 at 06:51

8 Answers8

138

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();
                    }
                }
        );
    }
}
ach
  • 6,164
  • 1
  • 25
  • 28
  • 4
    Looking at your code - are you not running into NullPointerException when the following line is executed `nextExecutionTime.setTime(triggerContext.lastActualExecutionTime());`. The triggerContext will return `null` when the application starts. – jsf Mar 06 '13 at 17:00
  • 3
    And is there a way to interrupt the current Trigger and change it's value while it's sleeping. – jsf Mar 06 '13 at 18:34
  • 3
    I only made sure this would compile, I never ran it. – ach Mar 06 '13 at 18:51
  • 3
    I used it in my project, works perfectly. Fast Fix: lastActualExecutionTime != null ? lastActualExecutionTime : new Date() – Alexander Aug 22 '14 at 13:02
  • 2
    @AlexanderSchwarz: thanks, incorporated the fix in edit – ach Aug 22 '14 at 14:13
  • Is it possible to remove task after exectuion from registry in this solution ? – kxyz Dec 17 '14 at 08:18
  • @kxyz You can return `null` from `nextExecutionTime()` to prevent the trigger from continuing to fire, but I'm not sure if that destroys it completely. Alternatively you can schedule using `taskRegistrar.getScheduler().schedule(Runnable, Trigger)` and use the returned `ScheduledFuture` to cancel. That might be effectively the same as just returning `null` in the above, though. – ach Dec 24 '14 at 01:56
  • Runnable is never getting executed when I try the above code. the nextexecutiontime returned in my sample code is 5secs after the current time. Does anyone know the possible reason? – Arijeet Saha Apr 06 '15 at 14:23
  • Thanks for the code, but i modified it, to have it started exactly upon startup: if (lastActualExecutionTime == null) { nextExecutionTime.setTime(new Date()); } else { // Already executed, should be plus'ed nextExecutionTime.setTime(lastActualExecutionTime); nextExecutionTime.add(Calendar.MILLISECOND, rate); } – Alec Jul 29 '15 at 15:01
  • @Ach, this works for me, only thing is the previous also works. so after trigger both previous one and new one getting triggered at their respective intervals. – karan_s438 Dec 13 '16 at 12:34
  • I found different solution. You can see my answer: https://stackoverflow.com/a/51333059/2590960 – grep Jul 13 '18 at 23:37
  • 3
    What is MyBean class here? – Aman Nagarkoti Jan 16 '19 at 10:24
  • @AmanNagarkoti It's been awhile but IIRC it's some service class or similar that is responsible for retrieving the schedule from some source, i.e. property, database, etc. – ach Jan 16 '19 at 14:12
  • Thanks for the response @ach, So how to configure that source.. form where .. means I need to implement this and how I'll change the schedule at run time... – Aman Nagarkoti Jan 21 '19 at 06:06
  • @ach I want to implement this for one specific schedule, though I have multiple schedules running in my application. – Aman Nagarkoti Jan 21 '19 at 12:56
  • @AmanNagarkoti That is really outside of the scope of this question. You could implement some sort of a cache or registry with an invalidation mechanism. – ach Jan 22 '19 at 14:51
  • @AmanNagarkoti Have you understood what the `MyBean` class is about? – parsecer Jan 10 '21 at 17:53
27

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");
}

@Service
public class ApplicationPropertyService {

    public String getApplicationProperty(){
        //get your value here
        return "5000";
    }
}
Sagar Ahuja
  • 637
  • 10
  • 10
  • 1
    I prefer this than the selected one because of less code and clean. – Pemassi Dec 15 '19 at 09:30
  • 2
    You can still change the value by making the bean scope as RefreshScope and also by implementing RefreshScopeRefreshedEvent. Sample app here https://github.com/winster/SpringSchedulerDynamic – Winster Jul 14 '20 at 07:56
  • My question is, if we override the AppProp.Service class and give two different delay timing, the scheduler will run two times? – mfaisalhyder Oct 06 '20 at 08:31
  • No it will not, it will use the overridden property, if you want your scheduler to run multiple times use CRON – Sagar Ahuja Oct 06 '20 at 08:37
  • How it will work if we need to change cron value dynamically. this will not help. – Kushwaha Apr 07 '21 at 13:20
18

To create and manage multiple dynamically scheduled tasks,

Scheduler configuration and bean:

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@Configuration
@EnableScheduling
public class SchedulingConfigs implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                // Do not put @Scheduled annotation above this method, we don't need it anymore.
                System.out.println("Running Scheduler..." + Calendar.getInstance().getTime());
            }
        }, 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, getNewExecutionTime());
                return nextExecutionTime.getTime();
            }
        });
    }

    private int getNewExecutionTime() {
        //Load Your execution time from database or property file
        return 1000;
    }

    @Bean
    public TaskScheduler poolScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
        scheduler.setPoolSize(1);
        scheduler.initialize();
        return scheduler;
    }
}

Scheduler service code:

package io.loadium.resource.service;

import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;

@Service
public class ScheduleTaskService {

    // Task Scheduler
    TaskScheduler scheduler;

    // A map for keeping scheduled tasks
    Map<Integer, ScheduledFuture<?>> jobsMap = new HashMap<>();

    public ScheduleTaskService(TaskScheduler scheduler) {
        this.scheduler = scheduler;
    }


    // Schedule Task to be executed every night at 00 or 12 am
    public void addTaskToScheduler(int id, Runnable task, Date runningDate) {
        ScheduledFuture<?> scheduledTask = scheduler.schedule(task, runningDate);
        jobsMap.put(id, scheduledTask);
    }

    // Remove scheduled task
    public void removeTaskFromScheduler(int id) {
        ScheduledFuture<?> scheduledTask = jobsMap.get(id);
        if (scheduledTask != null) {
            scheduledTask.cancel(true);
            jobsMap.put(id, null);
        }
    }

    // A context refresh event listener
    @EventListener({ContextRefreshedEvent.class})
    void contextRefreshedEvent() {
        // Get all tasks from DB and reschedule them in case of context restarted
    }
}

Sample usage:

// Add a new task with runtime after 10 seconds
scheduleTaskService.addTaskToScheduler(1, () -> System.out.println("my task is running -> 1"), , Date.from(LocalDateTime.now().plusSeconds(10).atZone(ZoneId.systemDefault()).toInstant()));
// Remove scheduled task
scheduleTaskService.removeTaskFromScheduler(1);
Bilal Demir
  • 587
  • 6
  • 17
  • In short: Just autowire 'TaskScheduler scheduler' and use it. – Lubo May 16 '23 at 07:58
  • IS it possible to explain this, i tried to use a randomizer in the getNewExecutionTime method but it seems it doesnt affect the time when the task is launched. – Maxrunner Aug 10 '23 at 09:43
10

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.

Praytic
  • 1,771
  • 4
  • 21
  • 41
  • 12
    Not really helpful, introduces too much of overhead – Rade_303 May 12 '17 at 11:56
  • 3
    Not so much actually – Praytic May 12 '17 at 19:47
  • 2
    This works if, like me, what you want to do is dynamically adjust up and down (in intervals of `some.rate`) how often the `// some code` in your scheduled task actually runs. But it doesn't actually answer the question which is about dynamically setting the value of `fixedRateString`. This way the Task is still triggered every `some.rate` interval, but `the business code` in the Task only runs when the Tick Count refreshes. The question and the other answers are about adjusting when the task is triggered to directly control when `the business code` in the Task runs. Tradeoffs. – geneSummons Jun 28 '19 at 00:08
9

you can manage restarting scheduling using TaskScheduler and 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();
    }

}

interface for start stop :

public interface SchedulerObjectInterface {    
    void start();
    void stop();
}

now you can stop and start again (restarting) Scheduling using @Autowired SchedulerObjectInterface

Sandip Solanki
  • 704
  • 1
  • 8
  • 15
2

Simple Spring Boot example restricted to second, minute, and hourly intervals. Intent of this example is to demonstrate conditional handling of two properties, TimeUnit and interval.

Properties:

snapshot.time-unit=SECONDS
snapshot.interval=5

Scheduled method:

@Scheduled(cron = "*/1 * * * * *")
protected void customSnapshotScheduler()
{
    LocalDateTime now = LocalDateTime.now();
    TimeUnit timeUnit = TimeUnit.valueOf(snapshotProperties.getSnapshot().getTimeUnit());
    int interval = snapshotProperties.getSnapshot().getInterval();

    if (TimeUnit.SECONDS == timeUnit
            && now.getSecond() % interval == 0)
    {
        this.camService.writeSnapshot(webcam.getImage());
    }

    if (TimeUnit.MINUTES == timeUnit
            && now.getMinute() % interval == 0)
    {
        this.camService.writeSnapshot(webcam.getImage());
    }

    if (TimeUnit.HOURS == timeUnit
            && now.getHour() % interval == 0)
    {
        this.camService.writeSnapshot(webcam.getImage());
    }
}
mmamane
  • 353
  • 4
  • 11
1

See How we are calling "#{@getIntervalTime}" in MySchedularService Class and taking the time interval for next scheduled call from @Bean annotate class

Main Class

package com;

import java.util.Calendar;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SbootSchedularApplication {

    public static void main(String[] args) {
        SpringApplication.run(SbootSchedularApplication.class, args);
    }
    
    @Value("${schedular3Timing}")
    String schedular3Timing;
    
    @Bean
    public String getIntervalTime() 
    {
        long startSchedulerAfterMiliSec = setSchedule(schedular3Timing);

        return ""+startSchedulerAfterMiliSec;
    }
    
    public long setSchedule(String key) 
    {
        int hour = Integer.parseInt(key.substring(0, key.indexOf(":")));
        int min = Integer.parseInt(key.substring(key.indexOf(":") + 1));

        Calendar schedulerCal = Calendar.getInstance();
        schedulerCal.set(Calendar.HOUR, hour);
        schedulerCal.set(Calendar.MINUTE, min);
        schedulerCal.set(Calendar.SECOND, 0);
        
        Calendar localCal = Calendar.getInstance();
        Long currentTimeInMilliSec = localCal.getTime().getTime();
        String currentDayTime = localCal.getTime().toString();

        if (schedulerCal.getTime().getTime() < currentTimeInMilliSec) {         // Means calculating time reference from time 00:00, if current time is 1000 mili-sec and scheduled time is 800 mili-sec -> then that time is already happened, so better add one more day in that same timing.
            schedulerCal.add(Calendar.DATE, 1);         // add 1 day more in the Schedular, if scheduled-MiliSec is less than the current-MiliSec.
        }

        long scheduledTimeInMilliSec = schedulerCal.getTime().getTime();
        String scheduledTime = schedulerCal.getTime().toString();
        System.out.println("** Scheduled start time for the task    : " + scheduledTime + " *** " + scheduledTimeInMilliSec);
        System.out.println("** Current time of the day      : " + currentDayTime + " *** " + currentTimeInMilliSec);

        long startScheduler = scheduledTimeInMilliSec - currentTimeInMilliSec;      // eg: scheduledTime(5pm) - currentTime(3pm) = (2hr)startSchedulerAfter
        return startScheduler;

    }

}


MySchedularService Class : See the JOB-3

package com.service;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class MySchedularService {

    private static final Logger logger = LoggerFactory.getLogger(MySchedularService.class);

//  @Scheduled(fixedRate = 2000, initialDelay = 5000L)
    @Scheduled(fixedRateString = "${schedular1.fixedRateInMS}", initialDelay = 1000L)
    public void job() {
        logger.info("Job1 Run Time : " + new Date());
    }
    
//  @Scheduled(fixedRateString = "${schedular2.fixedRateInMS}", initialDelay = 5000L)
//  public void job2() {
//      logger.info("Job2 Run Time : " + new Date());
//  }

    @Scheduled(fixedRate = 10000 , initialDelayString = "#{@getIntervalTime}")      // we can change the fixedRate = 86400000L miliseconds (i.e, one day interval)    
    public void job3() {
        logger.info("**Job2 Run Time : " + new Date());
    }
    
    

}


Application.properties File

spring.task.scheduling.pool.size=10
schedular1.fixedRateInMS=3000
schedular2.fixedRateInMS=10000
schedular3Timing=01:07

0

i created dynamic tasks using ThreadPoolTaskScheduler from org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler and scheduleWithFixedDelay method. i also added a redisson lock inorder to prevent duplicated jobs in distributed environment here is my code:

public class TaskRunnerService {

    private final ThreadPoolTaskScheduler taskScheduler;
    private final RedissonClient redissonClient;


    public TaskRunnerService(ThreadPoolTaskScheduler taskScheduler, RedissonClient redissonClient) {
        this.taskScheduler = taskScheduler;
        this.redissonClient = redissonClient;
    }

    @PostConstruct
    public void runTasks() {
        List<TaskDTO> taskDTOS = TaskHolder.createTasks();
        for (TaskDTO taskDTO : taskDTOS) {
            RLock lock = this.redissonClient.getFairLock("LoadAndRunScheduleService-" + taskDTO.getId());

            if (lock.tryLock()) {
                try {
                    this.taskScheduler.scheduleWithFixedDelay(() -> {
                        System.out.println(" running task " + taskDTO.getId() + " with delay " + taskDTO.getDelay() + " at " + new Date());
                    }, taskDTO.getDelay() * 1000L);
                }finally {
                    lock.unlock();
                }
            }
        }
    }

}

i created a TaskDTO class to be able to get delay at runtime:

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

    @AllArgsConstructor
    @NoArgsConstructor
    @Getter
    @Setter
    public class TaskDTO {
    
        private int id;
        private int delay;
    }

and configuration class is:

    @Configuration
    public class AppConfig {
    
        @Bean
        ThreadPoolTaskScheduler taskScheduler(){
            ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
            scheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
            scheduler.setPoolSize(2);
            scheduler.initialize();
            return scheduler;
        }
    
    }
saba
  • 332
  • 2
  • 14