Quartz Scheduling
Job Scheduling Using Quartz
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.
2Select Tomcat
Choose Tomcat as the application server, configure the Cloudlet limits, enter an environment name such as quartz, and click Create.
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";
}
}
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.
2Deploy to the environment
Select the Quartz environment and deploy the application to the required context.
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.
