Job Scheduling using Quartz

2013-05-02 - Reading time: 4 minutes #jelastic #quartz

Quartz 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 how Quartz works in the cloud!

Create the environment

  1. Go to and click Get started now to create account or log in with your Jelastic here.
  2. Click Create environment.

  1. In the Environment topology dialog which opens, pick Tomcat 7 as your application server, set your cloudlet limits and type your environment name, for example, quarts. Then 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());
  4. 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";
  5. 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.

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

  1. Open your app in a browser and check the results. In our case we have to navigate to the quartz context ( , 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!

Joakim Öhman

#jelastic #quartz