Java Programming Tutorials

Java programming tutorials with many code examples!

Quartz Scheduler start and shutdown

Quartz Scheduler start

Quartz Scheduler is one of the most widely used Java libraries. In tutorial we’ll show how to create, start, and shutdown Quartz Scheduler in a Java program.

SchedulerFactor and StdSchedulerFactory

Quartz Scheduler can be created only by org.quartz.SchedulerFactory of which org.quartz.impl.StdSchedulerFactory is default implementation. It can be configured in different ways (mainly using quartz.properties file), but here we’ll use default configuration for clarity:

SchedulerFactory factory = new StdSchedulerFactory();
// Further initialization of SchedulerFactory...

The Scheduler

Having SchedulerFactory we can instantiate QuartzScheduler:

Scheduler scheduler = factory.getScheduler();

Notice that the getScheduler() method will throw org.quartz.SchedulerException, when there are problems with underlying scheduler.

The scheduler is instantiated, but it is not running, so it won’t do anything.

Starting Quartz Scheduler

At this point scheduler can be started:

scheduler.start();
System.out.println("Scheduler started? "
    + scheduler.isStarted());

Now we can work with Quartz Scheduler, which we will show in subsequent posts, but at this moment we’ll just print its basic information:

// print some info about the Scheduler:
System.out.println("Scheduler name: "
        + scheduler.getSchedulerName());
System.out.println("Scheduler instanceId: "
        + scheduler.getSchedulerInstanceId());

Shutting down the Quartz Scheduler

To finish work with Quartz Scheduler one should stop it from executing jobs and triggers. There are two shutdown methods to do that:

  • void shutdown() throws SchedulerException
    Is the same as shutdown(false).
  • void shutdown(boolean waitForJobsToComplete) throws SchedulerException
    When waitForJobsToComplete is true then the scheduler won’t return from this method until all jobs are completed.
boolean waitForJobsToComplete = true;
scheduler.shutdown(waitForJobsToComplete);
System.out.println("Scheduler shutdown? "
        + scheduler.isShutdown());

Note: after shutdown is called the scheduler cannot be restarted.

Complete Example

In the following code we create default Quartz Scheduler, start it, print its basic information, and shutdown, waiting for all jobs to complete:

package com.farenda.tutorial.quartz;

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

public class StartQuartzScheduler {

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

        scheduler.start();
        System.out.println("Scheduler started? "
                + scheduler.isStarted());

        // print some info about the Scheduler
        System.out.println("Scheduler name: "
                + scheduler.getSchedulerName());
        System.out.println("Scheduler instanceId: "
                + scheduler.getSchedulerInstanceId());

        boolean waitForJobsToComplete = true;
        scheduler.shutdown(waitForJobsToComplete);
        System.out.println("Scheduler shutdown? "
                + scheduler.isShutdown());
    }
}

The above code produces the following output:

23:27:43.305 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
23:27:43.335 [main] INFO org.quartz.simpl.SimpleThreadPool - Job execution threads will use class loader of thread: main
23:27:43.526 [main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
23:27:43.526 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.2.3 created.
23:27:43.528 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
23:27:43.529 [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.

23:27:43.529 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
23:27:43.530 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.2.3
23:27:43.530 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
Scheduler started? true
Scheduler name: DefaultQuartzScheduler
23:27:43.530 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
Scheduler instanceId: NON_CLUSTERED
23:27:43.530 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.
23:27:43.530 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.
23:27:43.547 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutting down threadpool...
23:27:43.852 [DefaultQuartzScheduler_Worker-1] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
23:27:43.857 [DefaultQuartzScheduler_Worker-2] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
23:27:43.872 [DefaultQuartzScheduler_Worker-3] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
23:27:43.888 [DefaultQuartzScheduler_Worker-4] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
23:27:43.889 [DefaultQuartzScheduler_Worker-5] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
23:27:43.892 [DefaultQuartzScheduler_Worker-6] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
23:27:43.897 [DefaultQuartzScheduler_Worker-7] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
23:27:43.907 [DefaultQuartzScheduler_Worker-8] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
23:27:43.909 [DefaultQuartzScheduler_Worker-9] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
23:27:43.918 [DefaultQuartzScheduler_Worker-10] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
23:27:43.919 [main] DEBUG org.quartz.simpl.SimpleThreadPool - No executing jobs remaining, all threads stopped.
23:27:43.919 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutdown of threadpool complete.
23:27:43.919 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.
Scheduler shutdown? true
Share with the World!