Quartz Scheduler with MySQL Database

Anurag Dhunna
Viithiisys
Published in
3 min readAug 19, 2017

Hello friends, In previous post I shared how to use Amazon SNS via SMS. Today, I will show how to schedule these SMS or any JOB(Task) because in business there are always things which are pre-planned like when subscription of a product ends or at a launch of new product, Company wants to perform certain tasks all at once. It might be a big challenge to manage all things together. So, to overcome this problem we can use Quartz Scheduler for scheduling jobs.

In this post I will show you how to integrate Quartz Scheduler using MySQL Database.

Steps :

  • Install MySQL Database and setup database for quartz
  • Setup Quartz Server
  • Add dependencies to your project for Quartz and MySQL
  • Setup Quartz Properties with MySql and other properties.
  • Start Quartz Server
  • Create a JOB for quartz scheduler(Send sms or any other Task)
  • Submit and Execute the JOB to quartz scheduler
  • Verify

I hope you already installed MySQL. Now we will setup database for Quartz.

  • Create a database named “quartz”.
  • For creating tables execute the file tables_mysql_innodb.sql” in MySQL console. You can download it from here. OR you can simply copy paste the command in MySQL console.

Now we will Setup the Quartz Server.

Add Dependencies to your project

If you are using Maven

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>

or if you are using Gradle

compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.2.1'compile group: 'org.quartz-scheduler', name: 'quartz-jobs', version: '2.2.1'compile group: 'commons-collections', name: 'commons-collections', version: '3.2.1'compile group: 'commons-logging', name: 'commons-logging', version: '1.1.1'compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.2.2'compile group: 'commons-pool', name: 'commons-pool', version: '1.5.3'compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.34'

Setup Quartz Properties and Start Quartz Server

Setup Quartz Properties using MySQL DB

Executing the main method will start the Quartz Server.

Now create a JOB that we will schedule

import org.quartz.Job;
import org.quartz.JobExecutionContext;
public class TestJOB implements Job {
public void execute(JobExecutionContext context){
System.out.println("I am JOB, schdule me with Quartz");
System.out.println("Send SMS here");
}
}

Submit the JOB to scheduler

Schedule Job in Quartz Scheduler

Executing ScheduleJOb class will submit Job to Scheduler.

Now you check the job is executed after every 10 seconds.

You can verify your job schedule via MySQL tables entry.

Set Parameters to the JOB

You can set parameters to job like :

JobDataMap jobData = new JobDataMap();
jobData.put("
NAME", "TEST_PARAMETER");
// Create Job
JobDetail job = newJob(TestJOB.class).withIdentity
("jobName", "groupName")
.setJobData(jobData) // Set parameters
.build();

Retrieve Parameters

JobDataMap jobData = context.getJobDetail().getJobDataMap();
//fetch parameters from JobDataMap
String name = jobData.getString(NAME);

Only for Amazon SNS

Sometimes, there can be a lot of pending sms to send them in a order, will send them via Amazon SQS . In the next post I will be sharing how to integrate Amazon SNS with Amazon SQS.

❤ Please hit clap if you learned something!

If you have any questions leave a comment or message me on linkedin.

--

--

Anurag Dhunna
Viithiisys

Android, Java Software Developer. I write about technologies that I used in the journey of programming. Email: anurag.dhunna@gmail.com