Is it possible to add/remove/modify Job which is scheduled in Quartz + Spring Boot dynamically (during run time) by the end user who are using my portal. Since schedule start()
couldnt be accessed from outside I do not know is there any way. Basically I need to store all schedule information into Database and access them. Portal which Im building will be used by large number of users what would be the right solution to achieve this?
Otherwise can I use cron like below
@Scheduled(cron = "0 5 * * * *")
to scan jobs every 5 mns to achieve this.
It's definitely possible.
You just autowire the scheduler and whenever your user want to change task, you'd use Scheduler#scheduleJob
and Scheduler.deleteJob
with MethodInvokingJobDetailFactoryBean
instance (which represents the job). To create cron job triggers use Trigger
class.
Not runable excerpt from my code(EtlManager is proprietary class with method I want to run, etlTask is object containing persisted info on how often to run etc.):
@Autowired
private Scheduler scheduler;
in method reacting on user adding new task:
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
EtlManager em = this.appContext.getBean(etlTask.getTask().getExeClass(), EtlManager.class);
jobDetail.setTargetObject(em);
jobDetail.setTargetMethod("run");
jobDetail.setName(etlTask.getAppTaskCode());
jobDetail.setGroup(this.appCode);
jobDetail.setConcurrent(false);
jobDetail.afterPropertiesSet();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("CRON" + etlTask.getTiming().getId(), this.appCode)
.withSchedule(CronScheduleBuilder.cronSchedule(etlTask.getTiming().getCronExpression())
.withMisfireHandlingInstructionDoNothing())
.startAt(etlTask.getTiming().getStartAfter()) //
.build();
scheduler.scheduleJob((JobDetail) jobDetail.getObject(), trigger);
Also there is this quite similar QA here, not sure if 100% duplicate tough: Java Example: Dynamic Job Scheduling with Quartz
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.