简体   繁体   English

java quartz scheduler立即开始一项新工作

[英]java quartz scheduler fire a new job immediately

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 ? 当我想让这个工作成为现在的时候我用现在的日期和时间来存储一个cron表达式字符串 - 我觉得它太复杂了,还有另一种方法可以立即触发工作吗?

Thank's In Advance. 提前致谢。

Yeah, use the following Trigger to immediately fire your job instead of waiting upon the Cron Expressions. 是的,使用以下Trigger立即启动您的工作,而不是等待Cron表达式。

    String jobName = ""; // Your Job Name
    String groupName = ""; // Your Job Group
    Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(jobName, groupName)
                .startNow()
                .build();

All the Jobs registered in the Quartz Scheduler are uniquely identified by the JobKey which is composed of a name and group . 在Quartz Scheduler中注册的所有作业都由JobKey唯一标识, JobKey由名称和组组成。 You can fire the job which has a given JobKey immediately by calling triggerJob(JobKey jobKey) of your Scheduler instance. 您可以通过调用Scheduler实例的triggerJob(JobKey jobKey)来立即触发具有给定JobKey的作业。

//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);

Note : 注意 :

  • scheduler is the Scheduler instance used throughout your application . scheduler是整个应用程序中使用的Scheduler实例。 Its start() method should be already called after it is created. 它的start()方法应该在创建后调用。

  • 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) . 该作业是持久的作业,不能将任何触发器或cron附加到它。它只能通过调用triggerJob(JobKey jobKey)以编程方式触发。

You can create the "JobKey" on the fly with the 2 key string values. 您可以使用2个键字符串值动态创建“JobKey”。

IScheduler sched = /* however you get your scheduler*/;

sched.TriggerJob(new JobKey("myname", "mygroup"));

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

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