简体   繁体   English

动态作业调度石英

[英]Dynamic Job Schedule Quartz

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. 是否有可能由使用我的门户的最终用户动态地(在运行时)添加/删除/修改在Quartz + Spring Boot中调度的Job。 Since schedule start() couldnt be accessed from outside I do not know is there any way. 由于无法从外部访问日程表start() ,所以我不知道有什么办法。 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? Im建筑将被大量用户使用的Portal是实现此目标的正确解决方案?

Otherwise can I use cron like below 否则我可以像下面那样使用cron

@Scheduled(cron = "0 5 * * * *")

to scan jobs every 5 mns to achieve this. 每5百万扫描一次作业以实现此目的。

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). 您只需自动调度程序,然后每当您的用户想要更改任务时,就可以将Scheduler#scheduleJobScheduler.deleteJobMethodInvokingJobDetailFactoryBean实例(代表作业)一起使用。 To create cron job triggers use Trigger class. 要创建cron作业触发器,请使用Trigger类。

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.): 无法从我的代码中摘录(EtlManager是具有我要运行的方法的专有类,etlTask​​是包含有关运行频率的持久性信息的对象,等等):

@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 同样,这里也有类似的质量检查,不确定100%是否重复: Java示例:使用Quartz的动态作业调度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM