如何使用 Spring 动态添加新的计划作业



>我正在编写一个 Spring 启动应用程序

我的要求是 - 如果我添加新的 xml 文件,请在资源 ( src/main/resources ) 文件夹中...我应该阅读这些文件并从每个文件中获取一些 url 和其他特定设置。对于这些URL,我需要每天下载数据...因此,新的调度程序作业将从 url 和一些设置开始

新作业将在不同的计划时间内运行,这将使用 xml 文件中存在的 cron 表达式。此外,文件将在任何时间点动态添加。

如何实施?

如果你想

动态调度任务,你可以在没有 spring 的情况下完成,特别是 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);

使用 spring,您可以依靠任务和调度 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"));
    }
}

您可以通过实现触发器来提供自己的触发器

不要忘记在配置类上启用 usin @EnableScheduling 调度。

关于收听目录内容,您可以使用监视服务。像这样:

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

请查看这篇文章:监视目录以获取更改以获取更多详细信息。

试试这个库与外部动态参数配置,实时监控:

https://github.com/tyrion9/mtask

mtasks.yml中的配置参数

-   code: complex
    scheduled:
        period: 1000
    name: Autowired Param MTask
    className: sample.sample2.ComplexMTask
    params:
        name: HoaiPN
    autoStart: true

动态参数配置:

curl -X GET http://localhost:8080/api
curl -X POST http://localhost:8080/api/helloworld/stop
curl -X POST http://localhost:8080/api/helloworld/start

您可以在 Spring 注释中执行此操作:

@Scheduled(fixedRate = 360000)
public void parseXmlFile() {
    // logic for parsing the XML file.
}

请注意,该方法必须无效。此外,在主类中,您必须启用调度:

@SpringBootApplication
@EnableScheduling
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

请在此处查看完整参考:https://spring.io/guides/gs/scheduling-tasks/

最新更新