Quartz 2.0 (RamJobStore and JobStoreCMT)

Qaurtz 2.0 allows you to schedule your job manually or through QuartzInitializerListener.

1. Lets start first with manually scheduling the job.

//Creating the Trigger :
Trigger can be of two type:

 Simple Trigger
Cron Trigger : which will take cron expression for scheduling the job.

a.) Simple Trigger (Run in every 5 sec)

 Trigger trigger = TriggerBuider.newTrigger().withIdentity(“dummyTriggerName”,”dummyGroup”)            .withSchedule(            // Creating the simple scheduler             SimpleScheduleBuilder.simpleSchedule()                .withIntervalInSeconds(5).repeatForever()            ).build();

 

b.) Cron Trigger (Run in every 5 sec)

Trigger trigger = TriggerBuider.newTrigger().withIdentity(“dummyTriggerName”,”dummyGroup”) .withSchedule( // Creating the cron scheduler CronScheduleBuilder.cronSchedule(“0/5 * * * * ?”) ).build();

c.) Now Define the job

JobDetail job = JobBuilder.newJob(HelloJob.class).
withIdentity(“dummyJobName”, “dummyGroup”).build();

d.) Now Schedule the job :

Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger)

Note: Scheduler is taking the reference of job : scheduler.scheduleJob(job, trigger)

Code For HelloJob.class
========================
So now we will define one job class like below :

public class HelloJob implements Job{
  public void execute(JobExecutionContext context) throws JobExecutionException{
    // code anything
  }
}

Not: Above is the code for scheduling job manually Also this is called Ram Job Store, since all jobs works in ram.

Here we are manually making the job as above HelloJob class. We can also read the job from xml file and resgister them to scheduler with the help of QuartzInitializerListener.

First make xml file named as quartz-config.xml like below:

<?xml version=”1.0? encoding=”UTF-8??>
<job-scheduling-data
    xmlns=”http://www.quartz-scheduler.org/xml/JobSchedulingData&#8221;
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
    xsi:schemaLocation=”http://www.quartz-scheduler.org/xml/JobSchedulingData
    http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd”
    version=”1.8?>
 
    <schedule>
        <job>
            <name>MailingJobA</name>
            <group>GroupDummy</group>
            <description>This is First Sample Mailing Job</description>
            <job-class>com.job.quartz.jobs.MailingJobA</job-class>
        </job>
 
        <trigger>
            <cron>
                <name>dummyTriggerNameA</name>
                <job-name>MailingJobA</job-name>
                <job-group>GroupDummy</job-group>
                <!– It will run every 30 seconds –>
                <cron-expression>0/30 * * * * ?</cron-expression>
            </cron>
        </trigger>
    </schedule>
 
    <schedule>
        <job>
            <name>MailingJobB</name>
            <group>GroupDummy</group>
            <description>This is Second Sample Mailing Job</description>
            <job-class>com.job.quartz.jobs.MailingJobB</job-class>
        </job>
 
        <trigger>
            <cron>
                <name>dummyTriggerNameB</name>
                <job-name>MailingJobB</job-name>
                <job-group>GroupDummy</job-group>
                <!– It will run every 30 seconds –>
                <cron-expression>0/30 * * * * ?</cron-expression>
            </cron>
        </trigger>
    </schedule>
</job-scheduling-data>

Now regiter it through a property file : quartz.properties

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true

Now code for scheduling the job will be as below:
———————————————–

SchedulerFactory factory = (SchedulerFactory) servletContext
.getAttribute(QuartzInitializerListener. QUARTZ_FACTORY_KEY);
scheduler = factory.getScheduler(“quartz.properties”);

Here note one thing : in above quartz.properties file see the property
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

So this is the same thing which we do manually.

Now there is an another thing called JobStoreCMT. In this the job get stored inside the DB, and can be recovered through the same if any job fails or some crashed down happened.

In this only thing which we have to focus on quartz.properties file.

Here the file will be look like as below:
For JobStoreCMT
———————–
#============================================================================ # Configure Main Scheduler Properties #============================================================================

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.scheduler.instanceId = AUTO

#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure JobStore
#============================================================================
#specify the jobstore used
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreCMT
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.useProperties = false

#read jobs from xml file
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true

#The datasource for the jobstore that is to be used
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.nonManagedTXDataSource = myDS

#quartz table prefixes in the database
org.quartz.jobStore.tablePrefix = qrtz_
#org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.jobStore.isClustered = false
#org.quartz.jobStore.acquireTriggersWithinLock=true

#============================================================================
# Configure Datasources
#============================================================================

#org.quartz.dataSource.quartzDS.jndiURL= java:jboss/datasources/quartzDS
#org.quartz.dataSource.quartzDSNoTx.jndiURL= java:jboss/datasources/quartzDS

org.quartz.dataSource.myDS.driver = org.postgresql.Driver org.quartz.dataSource.myDS.URL = jdbc:postgresql:// localhost:5432/postgres org.quartz.dataSource.myDS.user = postgres org.quartz.dataSource.myDS.password = abc org.quartz.dataSource.myDS.maxConnections = 20 ============================================================================================

So in above properties file you can check that we are reading the jobs from quartz-config.xml file and storing them inside the DB, in my case it is Postgres.
Also note the property
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreCMT

Else things are the same as we do above.

Leave a Reply