How to use Quartz schedular with JDBC jobStoreTX with MySql database

By default Quartz schedular stores job information in RAM,but for a distributed appplication there may be situation where jobs are running in multiple instances,so to avoid this situation we can use  Quartz schedular with JDBC jobStore.In  JDBC jobStore job details are stored in Database and are triggered accordingly so for a distributed application we can use JDBC jobStore.

How to Use JDBC jobStore

1.Create a quartz configuration file as show below

# Main Quartz configuration
org.quartz.scheduler.skipUpdateCheck = true
org.quartz.scheduler.instanceName = DatabaseClusteredScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.jobFactory.class = org.quartz.simpl.SimpleJobFactory
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.dataSource = quartzDataSource
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5

# JobStore: JDBC jobStoreTX
org.quartz.dataSource.quartzDataSource.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.quartzDataSource.URL = jdbc:mysql://localhost:3306/quartz
org.quartz.dataSource.quartzDataSource.user = root
org.quartz.dataSource.quartzDataSource.password = password
org.quartz.dataSource.quartzDataSource.maxConnections = 8

2.Now create database quartz and execute tables_mysql_innodb.sql sql file present in quartz-2.2.1\docs\dbTables\ directory,or manually execute sql command present in the above file.

3.After executing sql commands it will create following tables

4.Now create TestJob  class .

import java.util.Date;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

@DisallowConcurrentExecution // to avoid multiple instances being executed concurrently
public class TestJob implements Job {

 @Override
 public void execute(JobExecutionContext arg0) throws JobExecutionException {

 System.out.println("executing job at "+new Date());

 }

}

5.Now run the below code

import java.io.IOException;
import java.util.Properties;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzSchedularExample {

 public static void main(String[] args) throws SchedulerException, ClassNotFoundException, IOException {

 Properties props=new Properties();
 props.load(QuartzSchedularExample.class.getResourceAsStream("quartz.properties"));

 SchedulerFactory schdFact = new StdSchedulerFactory(props);
 Scheduler scheduler = schdFact.getScheduler();
 scheduler.start();
 JobDetail job = JobBuilder.newJob(TestJob.class).withIdentity("test","test").build();
 Trigger trigger = TriggerBuilder.newTrigger().withIdentity("test","test").withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever()).build();
 JobKey jobKey=JobKey.jobKey("test","test");
 scheduler.deleteJob(jobKey); // deleting the job test if it already existed to to avoid  org.quartz.ObjectAlreadyExistsException
 scheduler.scheduleJob(job, trigger);

 }

}

Output: The job  will run every one second.

executing job at Fri Feb 10 12:31:27 IST 2017
executing job at Fri Feb 10 12:31:28 IST 2017
executing job at Fri Feb 10 12:31:29 IST 2017
executing job at Fri Feb 10 12:31:30 IST 2017
executing job at Fri Feb 10 12:31:31 IST 2017
executing job at Fri Feb 10 12:31:32 IST 2017
executing job at Fri Feb 10 12:31:33 IST 2017
executing job at Fri Feb 10 12:31:34 IST 2017
executing job at Fri Feb 10 12:31:35 IST 2017
executing job at Fri Feb 10 12:31:36 IST 2017

Note:deleteJob() method will delete the job from memory/Database and job can not be rescheduled again it has to be added(scheduleJob) again, where as unscheduleJob() method will just stop the job execution and it still remains in memory/Database  and job can be scheduled again using scheduleJob() method.

Example:

JobKey jobKey=JobKey.jobKey("test","test");
TriggerKey triggerKey=TriggerKey.triggerKey("test","test");

 scheduler.deleteJob(jobKey);
 scheduler.scheduleJob(job, trigger);
 scheduler.unscheduleJob(triggerKey);
Advertisements
This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s