java quartz scheduler立即开始一项新工作 [英] java quartz scheduler fire a new job immediately
问题描述
是否可以创建一个立即触发的作业?
当我想让这个工作成为现在的时候我用现在的日期和时间来存储一个cron表达式字符串 - 我觉得它太复杂了,还有另一种方法可以立即触发这个工作吗?
Is it possible to crate a job that will trigger immediately ? when i want the job to be triggres now i builed a cron expression string with the current date and time - i think it's too complicated, is there another way to trigger the job immediately ?
感谢提前。
推荐答案
在Quartz中注册的所有工作调度程序由 JobKey 由名称和组组成。您可以立即解雇具有给定 JobKey 的作业致电 triggerJob(JobKey) job 计划程序实例。
All the Jobs registered in the Quartz Scheduler are uniquely identified by the JobKey which is composed of a name and group . You can fire the job which has a given JobKey immediately by calling triggerJob(JobKey jobKey) of your Scheduler instance.
//Create a new Job
JobKey jobKey = JobKey.jobKey("myNewJob", "myJobGroup");
JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity(jobKey).storeDurably().build();
//Register this job to the scheduler
scheduler.addJob(job, true);
//Immediately fire the Job MyJob.class
scheduler.triggerJob(jobKey);
注意:
-
scheduler
是整个应用程序中使用的Scheduler实例。其 start()方法应该在创建后调用它。
scheduler
is the Scheduler instance used throughout your application . Its start() method should be already called after it is created.
作业是持久的作业,不能附加任何触发器或cron。它只能通过编程方式触发致电 triggerJob(JobKey jobKey) )。
The job is the durable job which cannot attach any triggers or cron to it .It can only be fired programmatically by calling triggerJob(JobKey jobKey).
这篇关于java quartz scheduler立即开始一项新工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!