Java Programming Tutorials

Java programming tutorials with many code examples!

Quartz Scheduler one shot trigger

Quartz Scheduler one shot trigger

In this tutorial we’re going to show how to use Quartz Scheduler to execute a Job using simple, one shot Trigger that will run immediately.

The Job

A Quartz Job is the part of code you want to have executed at specified time. All jobs implement org.quartz.Job interface that has only one execute(JobExecutionContext context) method. Here’s the simplest job:

package com.farenda.tutorial.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class PingJob implements Job {

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        System.out.println("Ping-pong Job!");
    }
}

The only parameter to the execute() method is JobExecutionContext that is used to pass different parameters to jobs. We will show how to use it in subsequent tutorials.

Create and start the scheduler

See post How to start and stop Quartz Scheduler to learn more about it.

Create Job using JobBuilder

Quartz provides a nice JobBuilder DSL to create definitions of jobs – org.quartz.JobDetail. Parameters of configured JobDetail are passed in runtime to associated Job (here PingJob):

JobDetail jobDetail = newJob(PingJob.class)
    // selected job id in quartz:
    .withIdentity("job name", "job group")
    .build();

One shot Trigger

Similarly to the JobBuilder there’s org.quartz.TriggerBuilder that allows to easily configure triggers to run:

Trigger trigger = newTrigger()
    // selected trigger id in quartz:
    .withIdentity("trigger name", "trigger group")
    .startNow()
    .build();

Schedule Job using Trigger

Now we can pass job configuration and trigger configuration to Quartz to perform scheduling:

scheduler.scheduleJob(jobDetail, trigger);

Quartz will run the job referred in given JobDetail at time specified by given Trigger.

Complete Example:

In the following code we show all the pieces together:

package com.farenda.tutorial.quartz;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

public class OneShotTriggerExample {

    public static void main(String[] args)
            throws SchedulerException {
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();

        scheduler.start();

        JobDetail jobDetail = newJob(PingJob.class)
                .withIdentity("job name", "job group")
                .build();

        Trigger trigger = newTrigger()
                .withIdentity("trigger name", "trigger group")
                .startNow()
                .build();

        System.out.println("Scheduling the job:");
        scheduler.scheduleJob(jobDetail, trigger);

        System.out.println("Shutting down the scheduler...");
        scheduler.shutdown(true);
    }
}

The above code produces the following output:

20:07:26.378 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
20:07:26.403 [main] INFO org.quartz.simpl.SimpleThreadPool - Job execution threads will use class loader of thread: main
20:07:26.530 [main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
20:07:26.531 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.2.3 created.
20:07:26.533 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
20:07:26.534 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.2.3) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

20:07:26.534 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
20:07:26.534 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.2.3
20:07:26.535 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
20:07:26.535 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
Scheduling the job:
20:07:26.550 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
20:07:26.554 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'job group.job name', class=com.farenda.tutorial.quartz.OneShotTriggerExample$PingJob
Shutting down the scheduler...
20:07:26.556 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.
20:07:26.556 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.
20:07:26.559 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutting down threadpool...
20:07:26.559 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Waiting for thread DefaultQuartzScheduler_Worker-1 to shut down
20:07:26.559 [DefaultQuartzScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job job group.job name
Ping-pong Job!
20:07:26.560 [DefaultQuartzScheduler_Worker-1] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
20:07:26.916 [DefaultQuartzScheduler_Worker-2] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
20:07:26.920 [DefaultQuartzScheduler_Worker-3] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
20:07:26.924 [DefaultQuartzScheduler_Worker-4] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
20:07:26.927 [DefaultQuartzScheduler_Worker-5] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
20:07:26.930 [DefaultQuartzScheduler_Worker-6] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
20:07:26.949 [DefaultQuartzScheduler_Worker-7] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
20:07:26.957 [DefaultQuartzScheduler_Worker-8] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
20:07:26.961 [DefaultQuartzScheduler_Worker-9] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
20:07:26.971 [DefaultQuartzScheduler_Worker-10] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
20:07:26.971 [main] DEBUG org.quartz.simpl.SimpleThreadPool - No executing jobs remaining, all threads stopped.
20:07:26.971 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutdown of threadpool complete.
20:07:26.971 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.
Share with the World!