KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > listeners > JobChainingJobListener


1 /*
2  * Copyright 2004-2006 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package org.quartz.listeners;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.quartz.utils.Key;
22 import org.quartz.JobExecutionContext;
23 import org.quartz.JobExecutionException;
24 import org.quartz.SchedulerException;
25
26 /**
27  * Keeps a collection of mappings of which Job to trigger after the completion
28  * of a given job. If this listener is notified of a job completing that has a
29  * mapping, then it will then attempt to trigger the follow-up job. This
30  * achieves "job chaining", or a "poor man's workflow".
31  *
32  * <p>Generally an instance of this listener would be registered as a global
33  * job listener, rather than being registered directly to a given job.</p>
34  *
35  * <p>If for some reason there is a failure creating the trigger for the
36  * follow-up job (which would generally only be caused by a rare serious
37  * failure in the system, or the non-existence of the follow-up job), an error
38  * messsage is logged, but no other action is taken. If you need more rigorous
39  * handling of the error, consider scheduling the triggering of the flow-up
40  * job within your job itself.</p>
41  *
42  * @author James House (jhouse AT revolition DOT net)
43  */

44 public class JobChainingJobListener extends JobListenerSupport {
45
46     private String JavaDoc name;
47     private Map JavaDoc chainLinks;
48
49
50     /**
51      * Construct an instance with the given name.
52      *
53      * @param name the name of this instance
54      */

55     public JobChainingJobListener(String JavaDoc name) {
56         if(name == null)
57             throw new IllegalArgumentException JavaDoc("Listener name cannot be null!");
58         this.name = name;
59         chainLinks = new HashMap JavaDoc();
60     }
61
62     public String JavaDoc getName() {
63         return name;
64     }
65
66     /**
67      * Add a chain mapping - when the Job identified by the first key completes
68      * the job identified by the second key will be triggered.
69      *
70      * @param firstJob a Key with the name and group of the first job
71      * @param secondJob a Key with the name and group of the follow-up job
72      */

73     public void addJobChainLink(Key firstJob, Key secondJob) {
74
75         if(firstJob == null || secondJob == null)
76             throw new IllegalArgumentException JavaDoc("Key cannot be null!");
77         if(firstJob.getName() == null || secondJob.getName() == null)
78             throw new IllegalArgumentException JavaDoc("Key cannot have a null name!");
79
80         chainLinks.put(firstJob, secondJob);
81     }
82
83     public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
84
85         Key sj = (Key) chainLinks.get(context.getJobDetail().getKey());
86
87         if(sj == null)
88             return;
89
90         getLog().info("Job '" + context.getJobDetail().getFullName() + "' will now chain to Job '" + sj + "'");
91
92         try {
93             if(context.getJobDetail().isVolatile() || context.getTrigger().isVolatile())
94                 context.getScheduler().triggerJobWithVolatileTrigger(sj.getName(), sj.getGroup());
95             else
96                 context.getScheduler().triggerJob(sj.getName(), sj.getGroup());
97         }
98         catch(SchedulerException se) {
99             getLog().error("Error encountered during chaining to Job '" + sj + "'", se);
100         }
101     }
102 }
103
104
Popular Tags