How to Schedule a job run at multiple time intervals

3k views Asked by At

We've a simple requirement to download files from different end point servers. We'll be scheduling these file downloads to happen at various time in the day (customizable from the GUI). We've been using Spring Scheduling & its working for a single job to download from a single server.

Now we would like to extend this operation of downloading the files to more than one server & different time. The operation of file downloading is same, but the time at which it will be running, the end point server, the location to download everything will change per task. Basically we'll be parameterizing these attributes & want to reuse the same job to download the files. Is that possible ?

How to achieve this using Spring Scheduling ?

2

There are 2 answers

3
Viraj Nalawade On BEST ANSWER

The schedule(TimerTask task,long delay,long period) method is used to schedule the specified task for repeated fixed-delay execution, beginning after the specified delay.

public class TimerClassExample {
   public static void scheduleTaskOfDownload(Date SchedulerStartTime,Long SchedulerInterval) {
   // creating timer task, timer
   TimerTask tasknew = new MyFileDownloader (serverName);
   Timer timer = new Timer();

   // scheduling the task at interval
   timer.schedule(tasknew,SchedulerStartTime, SchedulerInterval);      
   }

}

You can have a timerTask like below from which you can call download logic:-

public class MyFileDownloader extends TimerTask {
String serverName;
MyFileDownloader(String serverName){this.serverName= serverName}
    @Override
    public void run() {
        System.out.println("Timer task started at:"+new Date());
        downloadFiles(serverName);//your code which downloads file from server
        System.out.println("Timer task finished at:"+new Date());
    }
}

Now when your application starts you can use this code to make a call to our scheduler method scheduleTaskOfDownload

@Component
public class StartupHousekeeper implements ApplicationListener<ContextRefreshedEvent> {

  @Override
  public void onApplicationEvent(final ContextRefreshedEvent event) {

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 5);
    cal.set(Calendar.MINUTE, 30);
           //I am passing same time in both calls but you can pass different dates 
    TimerClassExample.scheduleTaskOfDownload(cal.getTime(),TimeUnit.MILLISECONDS.convert(3, TimeUnit.HOURS));
        TimerClassExample.scheduleTaskOfDownload(cal.getTime(),TimeUnit.MILLISECONDS.convert(2, TimeUnit.HOURS)); 

   //also keeping interval as 2 and 3 hours respectively but you choose different interval like 24 hrs in that place
      }
    }
2
charybr On

I would suggest integrate spring with Quartz job scheduler. Where you can either decide to:

- use single cron job that runs once in every scheduled time. Each invocation picks up parameters provided from another spring bean or database table.

OR

- create separate job that triggeres at scheduled time with given parameters.