Darth Shirr
Darth Shirr

Reputation: 587

Quartz + Spring : Configure jobs to run on specific time using JobStore

I'm trying out Quartz scheduler and managed to get it to work with Spring using Maven.

What I need to do is configure Quartz to store the jobs so that for a scheduled time it may execute the job. As far as I know there are two types of triggers in Quartz, Simple and Cron. And I also found out that there is something called JobStore in Quartz. I got it configured to some extent.

Could someone please give me a good reference/references on how to setup Quartz, JobStore? Big help, thank you.

Upvotes: 1

Views: 2163

Answers (3)

Julius Krah
Julius Krah

Reputation: 422

You will usually create a Scheduler from a factory class. Quartz can be setup in several ways.

  1. By using the org.quartz.impl.StdSchedulerFactory.getDefaultScheduler(). This will load the quartz.properties file in the Quartz distribution if you have not provided your own.

  2. By specifying your configuration as Key-Value pairs in a quartz.properties file and loading it in org.quartz.impl.StdSchedulerFactory(java.lang.String fileName).getScheduler().

  3. By specifying your configuration in a java.util.Properties as Key-Value pairs and loading it in org.quartz.impl.StdSchedulerFactory(java.util.Properties props).getScheduler().

  4. By using the spring-context-support jar from the Spring Framework and using a higher level abstraction such as org.springframework.scheduling.quartz.SchedulerFactoryBean.

  5. etc.

Quartz will start triggering jobs only when the org.quartz.Scheduler#start() has been invoked. Until this method is called the Scheduler will be in Standby mode.
The Scheduler can be destroyed to release threads by calling org.quartz.Scheduler#shutdown().

Example of Bootstrapping Quartz with Spring

@org.springframework.context.annotation.Configuration
public class QuartzExample {
    ...
    @org.springframework.context.annotation.Bean
    public org.springframework.scheduling.quartz.SchedulerFactoryBean schedulerFactory() {
        org.springframework.scheduling.quartz.SchedulerFactoryBean factoryBean = new org.springframework.scheduling.quartz.SchedulerFactoryBean();
        return factoryBean;
    }
}

The bean definition above is enough to perform the following configuration:-

  1. JobFactory - The default is Spring’s org.springframework.scheduling.quartz.AdaptableJobFactory, which supports java.lang.Runnable objects as well as standard Quartz org.quartz.Job instances.
  2. ThreadPool - Default is a Quartz org.quartz.simpl.SimpleThreadPool with a pool size of 10. This is configured through the corresponding Quartz properties.
  3. SchedulerFactory - The default used here is the org.quartz.impl.StdSchedulerFactory, reading in the standard quartz.properties from quartz.jar.
  4. JobStore - The default used is org.quartz.simpl.RAMJobStore which does not support persistence and is not clustered.
  5. Life-Cycle - The org.springframework.scheduling.quartz.SchedulerFactoryBean implements org.springframework.context.SmartLifecycle and org.springframework.beans.factory.DisposableBean which means the life-cycle of the scheduler is managed by the Spring container. The org.quartz.Scheduler#start() is called in the start() implementation of SmartLifecycle after initialization and the org.quartz.Scheduler#shutdown() is called in the destroy() implementation of DisposableBean at application teardown. You can override the startup behaviour by setting org.springframework.scheduling.quartz.SchedulerFactoryBean().setAutoStartup(false). With this setting you have to manually start the scheduler.

All these default settings can be overridden by the calling the various setter methods on org.springframework.scheduling.quartz.SchedulerFactoryBean.

I have provided a full working example on Github. If you are interested in an example that saves the jobs in a database checkout the HSQLDB branch of the same repository.

Upvotes: 0

paul
paul

Reputation: 13481

Just to give you another option, have you try task scheduling of Spring?. Nowadays I change all my old Quartz jobs for this and is easier to configure and you can use the annotations.

http://spring.io/blog/2010/01/05/task-scheduling-simplifications-in-spring-3-0/

Upvotes: 0

Mudassar
Mudassar

Reputation: 3225

You can have a look at these links Quartz JobStore with Spring Framework http://trimplement.com/using-spring-and-quartz-with-jobstore-properties/

If you still cant figure it out then let me know

Upvotes: 1

Related Questions