Wednesday, January 6, 2010

Implementing Listeners in the quartz_jobs.xml File










Implementing Listeners in the quartz_jobs.xml File


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






<?xml version='1.0' encoding='utf-8'?>

<quartz>
<job-listener
class-name="org.cavaness.quartzbook.chapter7.SimpleJobListener"
name="SimpleJobListener">
</job-listener>

<job>
<job-detail>
<name>PrintInfoJob</name>
<group>DEFAULT</group>
<job-listener-ref>SimpleJobListener</job-listener-ref>
<job-class>
org.cavaness.quartzbook.common.PrintInfoJob
</job-class>
</job-detail>

<trigger>
<simple>
<name>printJobTrigger</name>
<group>DEFAULT</group>
<job-name> PrintInfoJob</job-name>
<job-group>DEFAULT</job-group>
<start-time>2005-09-13 6:10:00 PM</start-time>
<! repeat indefinitely every 10 seconds >
<repeat-count>-1</repeat-count>
<repeat-interval>10000</repeat-interval>
</simple>
</trigger>
</job>
</quartz>




You see in Listing 7.14 the additional <job-listener> element with the two required attributes:


<job-listener
class-name="org.cavaness.quartzbook.chapter7.SimpleJobListener"
name="SimpleJobListener">


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.














No comments: