java quartz scheduler fire a new job immediately

时间:2022-05-23 02:13:00

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.

提前致谢。

3 个解决方案

#1


26  

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.

在Quartz Scheduler中注册的所有作业都由JobKey唯一标识,JobKey由名称和组组成。您可以通过调用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 . Its start() method should be already called after it is created.

    scheduler是整个应用程序中使用的Scheduler实例。它的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)以编程方式触发。

#2


49  

Yeah, use the following Trigger to immediately fire your job instead of waiting upon the Cron Expressions.

是的,使用以下触发器立即启动您的工作,而不是等待Cron表达式。

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

#3


0  

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

#1


26  

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.

在Quartz Scheduler中注册的所有作业都由JobKey唯一标识,JobKey由名称和组组成。您可以通过调用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 . Its start() method should be already called after it is created.

    scheduler是整个应用程序中使用的Scheduler实例。它的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)以编程方式触发。

#2


49  

Yeah, use the following Trigger to immediately fire your job instead of waiting upon the Cron Expressions.

是的,使用以下触发器立即启动您的工作,而不是等待Cron表达式。

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

#3


0  

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