user590586
user590586

Reputation: 3050

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 ?

Thank's In Advance.

Upvotes: 37

Views: 70309

Answers (3)

granadaCoder
granadaCoder

Reputation: 27852

You can create the "JobKey" on the fly with the 2 key string values.

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

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

Upvotes: 0

Rohit Bansal
Rohit Bansal

Reputation: 1199

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

Upvotes: 58

Ken Chan
Ken Chan

Reputation: 90447

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

Upvotes: 39

Related Questions