All the examples in this chapter have shown how to set up listeners using a programmatic approach. This chapter wouldn't be complete if we didn't provide at least an example of configuring a listener using a declarative approach with the quartz_jobs.xml file.
Starting with Quartz 1.5, you are able to specify listeners in the job-definition file, otherwise known as the quartz_jobs.xml file. Listing 7.14 shows an example of using a global listener.
Listing 7.14. Quartz Listeners Can Be Implemented with the quartz_jobs.xml File
PrintInfoJob DEFAULT SimpleJobListener org.cavaness.quartzbook.common.PrintInfoJob printJobTrigger DEFAULT PrintInfoJob DEFAULT 2005-09-13 6:10:00 PM -1 10000
You see in Listing 7.14 the additional <job-listener> element with the two required attributes:
The class-name property identifies the fully qualified name of the listener class. The name attribute assigns a logical name to the listener used in the <job-detail> element.
The next step is to define a <job-listener-ref> element in the <job-detail> element in the same file for each job that you want the listener on. The value of the element must match the name property of one of the defined <job-listener> elements in the file.
After you have done that, make sure you've configured the Scheduler to use the JobInitializationPlugin by setting the properties in the quartz.properties file. Quartz plug-ins are discussed in detail in the next chapter. For now, just add the following lines to your quartz.properties file:
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin org.quartz.plugin.jobInitializer.overWriteExistingJobs = true org.quartz.plugin.jobInitializer.failOnFileNotFound = true org.quartz.plugin.jobInitializer.validating=false
Then name your XML file quartz_jobs.xml and put the file in your classpath.
Some Gotchas to Watch Out For
It's worth mentioning a couple problems that you will likely run into when trying to set up listeners with the XML file. In Quartz 1.5, at least, the setName() method for the listeners was not included in the interface. The getName() method is present, but not the corresponding setName(). This doesn't seem to cause a problem when using listeners programmatically, but it will with the declarative approach. You simply need to create a setName() method for your listener.
The other tip is to make sure you have a no-arg constructor for your listener. Under certain conditions, the Quartz framework won't complain, but when using this declarative approach, you will get an error. It's better just to declare the no-arg constructor and always be safe.
Thread Use in Listeners
Scheduling in the Enterprise
Getting Started with Quartz
Cron Triggers and More
JobStores and Persistence
Implementing Quartz Listeners
Using Quartz Plug-Ins
Using Quartz Remotely
Using Quartz with J2EE
Quartz and Web Applications
Using Quartz with Workflow
Appendix A. Quartz Configuration Reference