Quartz – Java Job Scheduling Service

Most of the Java Projects, Enterprise Applications need some form of scheduling every now and then. People try to implement in number of ways like Timer, EJB etc. In one of my assignments I stumbled up on very interesting scheduling service known as Quartz. I was amazed by its simple but powerful implementation. Most interestingly its open source and can be integrated with virtually any Java application from the stand-alone application to the e-commerce system. Quartz supports all types of schedules, you can configure your timings from milliseconds to days to years. Quartz stands apart from other scheduling services by providing some state of the art features like Job Persistence, Transaction Support, Job Clustering, Listener Support.

Installation: Quartz get installed in matter of minutes. Only you have to download the latest jars that are available on their official site and add it to your project or application. That’s it!

Quartz allows you to divide your task in Job and run this job as per the schedule you have configured. You can uses a configuration/properties file to read the schedule. Sample properties file is shown below

In ideal scenario, you would need a scheduler class and a job to schedule. Here I am providing sample scheduler and job which is executed by trigger we create as Hourly.

Above scheduler class is creating an instance of job and a trigger to schedule the job. Job names are used so that job is uniquely identified in the schedule context. Here you will see that I am also trying to add the listener to the job. This is helpful when you want to do some another job once this is job is complete. e.g ETL. Here is the job and listener classes.
MyJob.java

MyJobListener.java

You can see that MyJobListener class is implementing JobListener interface and methods jobToBeExecuted and jobWasExecuted are implemented. These method will give user full control to listen to the jobs status.

Add a Comment

Your email address will not be published. Required fields are marked *