如何使用Spring动态添加新的计划作业 [英] How to add new schedule job dynamically with Spring
问题描述
我正在编写一个Spring Boot应用程序
I am writing a Spring Boot App
我的要求是-如果添加新的xml文件,则在资源(src/main/resources)文件夹中.我应该阅读这些文件,并从每个文件中获取一些url和其他特定设置.对于这些网址,我需要每天下载数据..因此,新的调度程序工作将从网址和一些设置开始
My requirements are - In the resources (src/main/resources) folder if I add new xml files.. I should read those files and get some url and other specific settings from each of it. and for those urls I need to download data everyday .. So a new scheduler job will start with url and some settings
新作业将在不同的计划时间内运行,这将使用xml文件中存在的cron表达式 此外,文件将在任何时间点动态添加 如何实现它.
The new jobs will run in different schedule time which will use cron expression present in the xml files Also files will be added dynamically at any point of time How to implenet it .
推荐答案
如果要动态安排任务,则可以使用 ScheduledThreadPoolExecutor
If you want to dynamically schedule tasks you can do it without spring by using ExecutorService in particular ScheduledThreadPoolExecutor
Runnable task = () -> doSomething();
ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
// Schedule a task that will be executed in 120 sec
executor.schedule(task, 120, TimeUnit.SECONDS);
// Schedule a task that will be first run in 120 sec and each 120sec
// If an exception occurs then it's task executions are canceled.
executor.scheduleAtFixedRate(task, 120, 120, TimeUnit.SECONDS);
// Schedule a task that will be first run in 120 sec and each 120sec after the last execution
// If an exception occurs then it's task executions are canceled.
executor.scheduleWithFixedDelay(task, 120, 120, TimeUnit.SECONDS);
With spring you can rely on the Task and Scheduling API
public class MyBean {
private final TaskScheduler executor;
@Autowired
public MyBean(TaskScheduler taskExecutor) {
this.executor = taskExecutor;
}
public void scheduling(final Runnable task) {
// Schedule a task to run once at the given date (here in 1minute)
executor.schedule(task, Date.from(LocalDateTime.now().plusMinutes(1)
.atZone(ZoneId.systemDefault()).toInstant()));
// Schedule a task that will run as soon as possible and every 1000ms
executor.scheduleAtFixedRate(task, 1000);
// Schedule a task that will first run at the given date and every 1000ms
executor.scheduleAtFixedRate(task, Date.from(LocalDateTime.now().plusMinutes(1)
.atZone(ZoneId.systemDefault()).toInstant()), 1000);
// Schedule a task that will run as soon as possible and every 1000ms after the previous completion
executor.scheduleWithFixedDelay(task, 1000);
// Schedule a task that will run as soon as possible and every 1000ms after the previous completion
executor.scheduleWithFixedDelay(task, Date.from(LocalDateTime.now().plusMinutes(1)
.atZone(ZoneId.systemDefault()).toInstant()), 1000);
// Schedule a task with the given cron expression
executor.schedule(task, new CronTrigger("*/5 * * * * MON-FRI"));
}
}
并且您可以通过实现请不要忘记在配置类上通过@EnableScheduling启用调度.
Don't forget to enable the scheduling by usin @EnableScheduling on configuration class.
关于收听目录内容,您可以使用 WatchService .像这样:
About listening to directory content you can use WatchService. Something like:
final Path myDir = Paths.get("my/directory/i/want/to/monitor");
final WatchService watchService = FileSystems.getDefault().newWatchService();
// listen to create event in the directory
myDir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
// Infinite loop don't forget to run this in a Thread
for(;;) {
final WatchKey key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent<Path> watchEvent = (WatchEvent<Path>) event;
Path newFilePath = myDir.resolve(watchEvent.context());
//do something with the newFilePath
}
// To keep receiving event
key.reset();
}
请看一下本文:监视目录中的更改有关更多详细信息.
Take a look at this article: Watching a Directory for Changes for more details.
这篇关于如何使用Spring动态添加新的计划作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!