back to listing index

java quartz scheduler fire a new job immediately - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: quartz jvm
Clipped on: 2012-08-21

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 ?

Thank's In Advance.

Image (Asset 1/4) alt=49.4k761144
asked Mar 27 at 13:11
Image (Asset 2/4) alt=323734

66% accept rate
add comment

2 Answers

up vote 1 down vote accepted

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

Note :

  • scheduler is the Scheduler instance used throughout your application . Its start() method should be already called after it is created.

  • 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).

answered Mar 27 at 14:53
Image (Asset 3/4) alt=8,2542929
add comment

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

    String jobName = ""; // Your Job Name
   
String groupName = ""; // Your Job Group
   
Trigger trigger = TriggerBuilder.newTrigger()
               
.withIdentity(jobName, groupName)
               
.startNow()
               
.build();
answered Mar 28 at 11:20
Image (Asset 4/4) alt=555
add comment

Your Answer

 
community wiki

Not the answer you're looking for? Browse other questions tagged or ask your own question.