How to configure Quartz scheduler to run the cron jobs

Purpose of the article: In most current applications, especially those in the J2EE space, there typically arises the requirement to run certain jobs (e.g., send nightly or weekly reports to users, send notification emails, etc.) in the background that would be triggered at periodic intervals, repeating the entire duration over which the application is live and running. One recommended approach for these jobs is to utilize Appworx; however, that has its own configuration, settings and necessity for external teams to implement.

In this article, I am showing you how to achieve job scheduling, without external support, making use of the Quartz scheduler in a JBOSS J2EE application server environment. Follow the below steps.


1. You need to have the required library files to be imported to the library directory of your application. You can get them online from http://www.opensymphony.com/quartz/download.action .
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="war/src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="lib" path="lib/commons-digester-1.8.jar"/>
    <classpathentry kind="lib" path="lib/j2ee1.3.1.jar"/>
    <classpathentry kind="lib" path="lib/jsf-api.jar"/>
    <classpathentry kind="lib" path="lib/jsf-impl.jar"/>
    <classpathentry kind="lib" path="lib/jstl.jar"/>
    <classpathentry kind="lib" path="lib/log4j-1.2.14.jar"/>
    <classpathentry kind="lib" path="lib/mail-1.3.3.jar"/>
    <classpathentry kind="lib" path="lib/ojdbc14.jar"/>
    <classpathentry kind="lib" path="lib/poi2.5.1.jar"/>
    <classpathentry kind="lib" path="lib/richfaces-api-3.2.2.GA.jar"/>
    <classpathentry kind="lib" path="lib/richfaces-impl-3.2.2.GA.jar"/>
    <classpathentry kind="lib" path="lib/richfaces-ui-3.2.2.GA.jar"/>
    <classpathentry kind="lib" path="lib/spring-2.0.6.jar"/>
    <classpathentry kind="lib" path="lib/wfapi-1.0.jar"/>
    <classpathentry kind="lib" path="war/lib/build/activation.jar"/>
    <classpathentry kind="lib" path="war/lib/build/ejb.jar"/>
    <classpathentry kind="lib" path="war/lib/build/jdbc2_0-stdext.jar"/>
    <classpathentry kind="lib" path="war/lib/build/jms.jar"/>
    <classpathentry kind="lib" path="war/lib/build/jmx.jar"/>
    <classpathentry kind="lib" path="war/lib/build/jta.jar"/>
    <classpathentry kind="lib" path="war/lib/build/junit.jar"/>
    <classpathentry kind="lib" path="war/lib/build/servlet.jar"/>
    <classpathentry kind="lib" path="war/lib/core/commons-collections-3.2.jar"/>
    <classpathentry kind="lib" path="war/lib/core/commons-logging-1.1.jar"/>
    <classpathentry kind="lib" path="war/lib/optional/commons-beanutils-1.7.0.jar"/>
    <classpathentry kind="lib" path="war/lib/optional/commons-dbcp-1.2.2.jar"/>
    <classpathentry kind="lib" path="war/lib/optional/commons-modeler-2.0.jar"/>
    <classpathentry kind="lib" path="war/lib/optional/commons-pool-1.3.jar"/>
    <classpathentry kind="lib" path="war/lib/optional/commons-validator-1.3.1.jar"/>
    <classpathentry kind="lib" path="war/lib/quartz-1.6.5.jar"/>
    <classpathentry kind="lib" path="war/lib/quartz-all-1.6.5.jar"/>
    <classpathentry kind="lib" path="war/lib/quartz-jboss-1.6.5.jar"/>
    <classpathentry kind="lib" path="war/lib/quartz-oracle-1.6.5.jar"/>
    <classpathentry kind="lib" path="war/lib/quartz-weblogic-1.6.5.jar"/>
    <classpathentry kind="output" path="war/web/WEB-INF/classes"/>
</classpath>



2. Edit the web.xml file.
1:
2:
3:
4:
5:
6:
7:
8:
<servlet>
    <servlet-name>quartz</servlet-name>        
    <servlet-class>
    org.quartz.ee.servlet.QuartzInitializerServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>


NB: <load-on-startup>1</load-on-startup> ensures our scheduler starts loading the jobs immediately on server startup and fire them at the specified time.


3. Write your job bean.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
package com.geinfra.gedrb.scheduler;
 
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
 
public class StatusJob implements Job {
 
  public void execute(JobExecutionContext context) throws JobExecutionException {
    // TODO Auto-generated method stub
    String name = context.getJobDetail().getName();
    String group = context.getJobDetail().getGroup();
    System.out.println("Job name: " + name + "\t" + "Group: " + group);
    System.out.println("Trigger name: " + context.getTrigger().getName());
    System.out.println("Firing Time: " + context.getFireTime());
  }
} 


The execute() method will have the logic/operations you need to perform when the job is triggered.


4. Write the quartz_jobs.xml file which contains all the jobs configured one after the other and ensure this file goes into the classpath of the project when the ear file is being created. (WEBINF/classes/ directory).
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
<?xml version='1.0' encoding='utf-8'?>
<quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData"
  xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opensymphony.com/quartz/JobSchedulingData 
  http://www.opensymphony.com/quartz/xml/job_scheduling_data_1_5.xsd"
  version="1.5">
 
<job>
    <job-detail>
      <name>StatusJob</name>
      <group>StatusJobs</group>
      <job-class>com.geinfra.gedrb.scheduler.StatusJob</job-class>      
      <!-- <volatility>false</volatility>
      <durability>false</durability>
      <recover>false</recover> -->
    </job-detail>
    <trigger>
      <cron>
        <name>dailyTrigger</name>
        <group>alertTriggers</group>
        <job-name>StatusJob</job-name>
        <job-group>StatusJobs</job-group>
        <!-- every day at 01:00 AM, M-F -->
        <cron-expression>0 00 01 ? * MON-FRI</cron-expression>
        
      </cron>
    </trigger>
  </job>
 </quartz>



5. Write the quartz.properties file as below and ensure it goes into the classpath of the project.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
#===============================================================
# Configure Main Scheduler Properties
#===============================================================
 
org.quartz.scheduler.instanceName = QuartzScheduler
org.quartz.scheduler.instanceId = AUTO
 
#==========================================================
# Configure ThreadPool
#==========================================================
 
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount =  5
org.quartz.threadPool.threadPriority = 5
 
#==========================================================
# Configure JobStore
#==========================================================
 
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
 
 
#==========================================================
# Configure Plugins
#==========================================================
org.quartz.plugin.jobLoader.class =  org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz_jobs.xml
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
org.quartz.plugin.jobInitializer.failOnFileNotFound = true



This is it. Start you JBOSS application server and the Quartz Scheduler will take care of the rest...

quartz download link