Job Scheduling Using Quartz

By | May 2, 2013
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

logo-quartz-schedulerQuartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java application – from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do.

If your application has tasks that need to occur at any given moment, or if your system has recurring maintenance jobs, then Quartz may be your ideal solution.

Sample uses of job scheduling with Quartz:

  • Driving Process Workflow: As a new order is initially placed, schedule a Job to fire in exactly 2 hours, that will check the status of that order, and trigger a warning notification if an order confirmation message has not yet been received for the order, as well as changing the order’s status to ‘awaiting intervention’.
  • System Maintenance: Schedule a job to dump the contents of a database into an XML file every business day (all weekdays except holidays) at 11:30 PM.
  • Providing reminder services within an application.

Let’s see now how Quarts works in the cloud!

Create the environment

1. Go to and sign up if you haven’t done it yet or log in with your Jelastic credentials by clicking the Sign In link on the page.

2. Ask Jelastic to create a new environment.


3. Choose Tomcat 7 as your application server and set the cloudlets limit. Then type your environment name, for example, quartz, and click Create.


Wait just a minute for your environment to be created.

Create the application

1. Create your web application (in our case we are using Maven based application) and add the following dependencies to your pom.xml file in order to include Quartz libraries to your project:

 <!-- Quartz API -->



2. Build your project with dependencies.

3. Create a new Java class, which will implement your jobs. Here’s an example, which displays server time:

package com;

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

public class HelloJob implements Job
    public void execute(JobExecutionContext context)
    throws JobExecutionException {

        System.out.println("Server Time: " + new Date());



3. Create a new Servlet (you can easily implement logic on another level) and specify Quartz trigger to define when the Quartz will run your above job. In our case we have created which contains command (logging which was implemented in HelloJob class ) that is being executed every minute.

package com;

import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {

            JobDetail job = new JobDetail();

            CronTrigger trigger = new CronTrigger();
            trigger.setCronExpression("0 */1 * * * ?");

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

        } catch (SchedulerException ex) {
            Logger.getLogger(QuartzServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(QuartzServlet.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    public String getServletInfo() {
        return "Short description";

4. Build your project into WAR file.

Deploy the application

1. Go back to the Jelastic dashboard and upload the WAR file, you’ve just created.


2. Deploy the your application to the environment you’ve created earlier.


3. Open your app in a browser and check the results. In our case we have to navigate to the quartz context (http://quartz.{hoster_domain}/quartz , according to Servlet “QuartzServlet” mapping) and check our application logs.


As you can see job scheduling using Quartz works like a charm.


Quartz is in used by many thousands of entities, many of whom have directly embedded Quartz in their own custom applications, and others who are using products that already have Quartz embedded within them. Quartz 1.6.0 was directly downloaded more than 100,000 times, and countless additional times through secondary repositories. So, give it a try and let us know how it goes!

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Subscribe to get the latest updates