如何在多个时间间隔安排作业 运行
How to Schedule a job run at multiple time intervals
我们有一个简单的要求,即从不同的端点服务器下载文件。我们将安排这些文件下载在一天中的不同时间进行(可从 GUI 自定义)。我们一直在使用 Spring 计划及其工作,以便从单个服务器下载单个作业。
现在我们想将这种下载文件的操作扩展到多个服务器和不同的时间。下载文件的操作是一样的,但是下载的时间 运行,端点服务器,下载所有内容的位置都会因任务而异。基本上我们将对这些属性进行参数化并希望重复使用相同的作业来下载文件。这可能吗?
如何使用 Spring 调度实现此目的?
我建议将 spring 与 Quartz 作业调度程序集成。您可以在哪里决定:
- 使用在每个预定时间运行一次的单个 cron 作业。每次调用都从另一个 spring bean 或数据库 table.
中获取参数
或
- 创建单独的作业,在预定时间使用给定参数触发。
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);
}
}
您可以有一个如下所示的 timerTask,您可以从中调用下载逻辑:-
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());
}
}
现在,当您的应用程序启动时,您可以使用此代码调用我们的调度程序方法 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
}
}
我们有一个简单的要求,即从不同的端点服务器下载文件。我们将安排这些文件下载在一天中的不同时间进行(可从 GUI 自定义)。我们一直在使用 Spring 计划及其工作,以便从单个服务器下载单个作业。
现在我们想将这种下载文件的操作扩展到多个服务器和不同的时间。下载文件的操作是一样的,但是下载的时间 运行,端点服务器,下载所有内容的位置都会因任务而异。基本上我们将对这些属性进行参数化并希望重复使用相同的作业来下载文件。这可能吗?
如何使用 Spring 调度实现此目的?
我建议将 spring 与 Quartz 作业调度程序集成。您可以在哪里决定:
- 使用在每个预定时间运行一次的单个 cron 作业。每次调用都从另一个 spring bean 或数据库 table.
中获取参数
或
- 创建单独的作业,在预定时间使用给定参数触发。
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);
}
}
您可以有一个如下所示的 timerTask,您可以从中调用下载逻辑:-
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());
}
}
现在,当您的应用程序启动时,您可以使用此代码调用我们的调度程序方法 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
}
}