# How do you place a listener on all your Jobs ?

The JobListener interface contains a set of methods that are invoked when certain key events occur in the life cycle of a job. This is can be helpful to keep track of what your Jobs are doing.
By using the addGlobalListener method of the scheduler, you can place a global listener on all jobs:

JobDetail job = new JobDetail("job2", "group1", MyJob.class);
 
JobListener jobListener =
 new MyJobListener();
scheduler.addGlobalJobListener(jobListener);

CronTrigger  trigger = new CronTrigger("trigger2", "group1", "job2", "group1","15 0/2 * * * ?");
scheduler.addJob(job, true);

And here's the corresponding interface which needs to implement the JobListener interface:
package sample;

import org.quartz.JobExecutionContext;

public class MyJobListener implements JobListener {

 public String getName() {
 return getClass().getSimpleName();
 }

 public void jobExecutionVetoed(JobExecutionContext context) {
 String jobName = context.getJobDetail().getName();
 System.out.println(jobName + " is vetoed to be executed");

 }

 public void jobToBeExecuted(JobExecutionContext context) {
 String jobName = context.getJobDetail().getName();
 System.out.println(jobName + " is going to be executed");

 }

 public void jobWasExecuted(JobExecutionContext context,
 JobExecutionException exc) {
 String jobName = context.getJobDetail().getName();
 System.out.println(jobName + " was executed");

 }

}

STICKY NOTE: By using global listeners you can count the amount of jobs which are been executed or are going to be executed. This is quite important if you are going to monitor your Quartz application

# How do you set a listener on a specific Job ?

 

 

Sometimes you need a fine grained control over your Jobs, thus you can set a listener JUST on a specific Job. You don't
need to change too much the earlier code- just use the Scheduler's addJobListener method instead and register the listener BOTH on the Scheduler and on the Job:

JobDetail job = new JobDetail("job2", "group1", MyJob.class);
CronTrigger  trigger = new CronTrigger("trigger2", "group1", "job2", "group1","15 0/2 * * * ?");
JobListener jobListener =
 new MyJobListener();

scheduler.addJobListener(jobListener);

// Listener set on JobDetail before scheduleJob()
jobDetail.addJobListener(jobListener.getName());

// Register the JobDetail and Trigger
scheduler.scheduleJob(job, trigger);

#How do you configure Jobs using an XML file ?

 

If you want to configure your Jobs declaratively, Quartz can do that for you! Here's how to configure a Cron Job without writing a line of code!
Place at first this file jobs.xml (you can call it as you like it) at the root of your project. (It needs to be moved in the WEB-INF/classes of your Web application)


<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
 version="1.8">

 <schedule>
 <job>
 <name>AutomaticJob</name>
 <group>MYJOB_GROUP</group>
 <description>The job description</description>
 <job-class>sample.MyJob</job-class>

 </job>

 <trigger>
 <cron>
 <name>my-trigger</name>
 <group>MYTRIGGER_GROUP</group>
 <job-name>AutomaticJob</job-name>
 <job-group>MYJOB_GROUP</job-group>
 <cron-expression>15 0/2 * * * ?</cron-expression>

 </cron>
 </trigger>
 </schedule>
</job-scheduling-data> 

Here we are firing an AutomaticJob bound the the class sample.MyJob which will fire every 2 minutes. In order to activate this Job we need setting the jobInitializer class which is responsible for firing jobs automatically. Add to your quartz.properties the following properties and that's all!

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames =/jobs.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false


I've noticed that the Quartz's File scan keeps printing the message "Job file not found". That's a bit odd, because I've tested Jobs are fired correctly!

0
0
0
s2smodern