Quartz Scheduling

Job Scheduling Using Quartz

Quartz is an open-source job scheduling service for Java applications. It can execute simple or complex recurring tasks, from small maintenance operations to large-scale application jobs.

Quartz is suitable when an application must run tasks at a specific time or execute recurring maintenance operations automatically.

Create the Environment

1Open the dashboard

Sign in to the platform dashboard and click Create environment.

Create environment button
Start creating a new environment from the dashboard.

2Select Tomcat

Choose Tomcat as the application server, configure the Cloudlet limits, enter an environment name such as quartz, and click Create.

Tomcat environment wizard
Create a Tomcat environment for the Quartz application.

Wait until the environment provisioning process is complete.

Create the Application

Create a Java web application. The source example uses a Maven-based project with Quartz libraries included in the project dependencies.

Add Quartz Dependencies

Add the following dependencies to the project’s pom.xml file:

<!-- Quartz API -->
<dependency>
    <groupId>opensymphony</groupId>
    <artifactId>quartz</artifactId>
    <version>1.6.3</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>

Build the project after adding the required dependencies.

Create the Scheduled Job

Create a Java class that implements the Quartz Job interface. The following example writes the server time to the application log:

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());
    }
}

Create the Quartz Servlet

Create a servlet that defines the Quartz job and its trigger. In this example, the scheduler runs the HelloJob class every minute.

package com;

import java.io.IOException;
import java.io.PrintWriter;
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 {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            JobDetail job = new JobDetail();
            job.setName("dummyJobName");
            job.setJobClass(HelloJob.class);

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

            Scheduler scheduler =
                new StdSchedulerFactory().getScheduler();

            scheduler.start();
            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 {
            out.close();
        }
    }

    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";
    }
}
The cron expression 0 */1 * * * ? runs the Quartz job once every minute.

Build the completed application into a WAR archive.

Deploy the Application

1Upload the WAR archive

Return to the platform dashboard and upload the generated WAR file through Deployment Manager.

Upload the Quartz WAR archive
Upload the Quartz application archive.

2Deploy to the environment

Select the Quartz environment and deploy the application to the required context.

Deploy the Quartz application
Deploy the WAR archive to the Tomcat environment.

Verify the Scheduler

Open the application in a browser using the mapped servlet context. The example uses:

http://{env_name}.{hoster_domain}/quartz

Open the Tomcat log to review the scheduler output.

Open the Tomcat log
Open the Tomcat log for the deployed Quartz application.
Quartz scheduler output
The log shows the Quartz scheduler and the server-time job running every minute.
Verification: Repeated server-time entries in the application log confirm that the Quartz trigger is executing the job according to the configured schedule.

What’s next?