允许最终用户使用Spring Boot计划任务(Allow the end user to schedule task with Spring Boot)

我正在使用Spring Boot,我想让最终用户按照自己的意愿安排任务。

我有一个带有Angular前端的Spring Boot REST后端。 用户应该能够(使用crontab样式语法)一个任务(即类的方法后端方面),并选择一些参数。

用户还应该能够从前端查看,编辑,删除计划任务

我知道我可以使用@Scheduled注释,但我不知道最终用户如何使用它来安排任务。 我也看一下Quartz,但是我没有看到如何在方法调用中设置用户的参数。

我应该使用Spring Batch吗?

I'm using Spring Boot and I want to allow the end user to schedule task as he wants.

I have a Spring Boot REST backend with a Angular frontend. The user should be able to scedule (with a crontab style syntaxe) a task (i.e. a class' method backend side), and choose some arguments.

The user should alse be able to view, edit, delete scheduled task from the front end

I know I can use @Scheduled annotation, but I don't see how the end user can schedule task with it. I also take a look at Quartz, but I don't see how to set the user's argument in my method call.

Should I use Spring Batch?

最满意答案

要以编程方式安排作业,您有以下选择:

对于简单的情况,您可以查看ScheduledExecutorService ,它可以调度命令在给定的延迟后运行,或者定期执行。 它在java.util.concurrent包java.util.concurrent ,易于使用。

要动态安排Crontab作业,您可以使用quartz和这里的官方示例 ,基本上您将要做的是:

创建实例Scheduler ,可以通过Spring定义为java bean和autowire ,示例代码: 创建JobDetail 创建CronTrigger 用cron安排job

官方示例代码(计划每20秒运行一次):

SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); JobDetail job = newJob(SimpleJob.class) .withIdentity("job1", "group1") .build(); CronTrigger trigger = newTrigger() .withIdentity("trigger1", "group1") .withSchedule(cronSchedule("0/20 * * * * ?")) .build(); sched.scheduleJob(job, trigger);

To schedule job programatically, you have the following options:

For simple cases you can have a look at ScheduledExecutorService, which can schedule commands to run after a given delay, or to execute periodically. It's in package java.util.concurrent and easy to use.

To schedule Crontab job dynamically, you can use quartz and here's official examples, basically what you'll do is:

Create instance Scheduler, which can be defined as a java bean and autowire by Spring, example code: Create JobDetail Create CronTrigger Schedule the job with cron

Official example code(scheduled to run every 20 seconds):

SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); JobDetail job = newJob(SimpleJob.class) .withIdentity("job1", "group1") .build(); CronTrigger trigger = newTrigger() .withIdentity("trigger1", "group1") .withSchedule(cronSchedule("0/20 * * * * ?")) .build(); sched.scheduleJob(job, trigger);

更多推荐