KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > quartz > QuartzComponent


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

17 package org.apache.servicemix.components.quartz;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.servicemix.MessageExchangeListener;
22 import org.apache.servicemix.components.util.ComponentSupport;
23 import org.quartz.JobDetail;
24 import org.quartz.JobExecutionContext;
25 import org.quartz.JobExecutionException;
26 import org.quartz.Scheduler;
27 import org.quartz.SchedulerException;
28 import org.quartz.SchedulerFactory;
29 import org.quartz.Trigger;
30 import org.quartz.impl.StdSchedulerFactory;
31
32 import javax.jbi.JBIException;
33 import javax.jbi.messaging.InOnly;
34 import javax.jbi.messaging.MessageExchange;
35 import javax.jbi.messaging.MessagingException;
36 import javax.jbi.messaging.NormalizedMessage;
37 import java.util.Date JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * A <a HREF="http://www.opensymphony.com/quartz/">Quartz</a> component for triggering components when timer events fire.
43  *
44  * @version $Revision: 433012 $
45  */

46 public class QuartzComponent extends ComponentSupport implements MessageExchangeListener {
47     private static final transient Log log = LogFactory.getLog(QuartzComponent.class);
48
49     public static final String JavaDoc COMPONENT_KEY = "org.apache.servicemix.component";
50
51     private SchedulerFactory factory;
52     private Scheduler scheduler;
53     private Map JavaDoc triggers;
54     private QuartzMarshaler marshaler = new DefaultQuartzMarshaler();
55
56     public void start() throws JBIException {
57         try {
58             scheduler.start();
59             super.start();
60         } catch (SchedulerException e) {
61             throw new JBIException(e);
62         }
63     }
64
65     public void stop() throws JBIException {
66         try {
67             super.stop();
68             scheduler.standby();
69         } catch (SchedulerException e) {
70             throw new JBIException(e);
71         }
72     }
73
74     public void shutDown() throws JBIException {
75         try {
76             scheduler.shutdown();
77         } catch (SchedulerException e) {
78             throw new JBIException(e);
79         } finally {
80             super.shutDown();
81         }
82     }
83
84     public void addTrigger(Trigger trigger, JobDetail detail) throws JBIException {
85         try {
86             // lets default the trigger name to the job name
87
if (trigger.getName() == null) {
88                 trigger.setName(detail.getName());
89             }
90             // lets default the trigger group to the job group
91
if (trigger.getGroup() == null) {
92                 trigger.setGroup(detail.getGroup());
93             }
94             // default start time to now if not specified
95
if (trigger.getStartTime() == null) {
96                 trigger.setStartTime(new Date JavaDoc());
97             }
98             detail.getJobDataMap().put(COMPONENT_KEY, this);
99             Class JavaDoc jobClass = detail.getJobClass();
100             if (jobClass == null) {
101                 detail.setJobClass(ServiceMixJob.class);
102             }
103             scheduler.scheduleJob(detail, trigger);
104         }
105         catch (SchedulerException e) {
106             throw new JBIException("Failed to add trigger: " + trigger + " with detail: " + detail + ". Reason: " + e, e);
107         }
108     }
109
110
111     /**
112      * This method is invoked when a Quartz job is fired.
113      *
114      * @param context the Quartz Job context
115      */

116     public void onJobExecute(JobExecutionContext context) throws JobExecutionException {
117         if (log.isDebugEnabled()) {
118             log.debug("Firing Quartz Job with context: " + context);
119         }
120         try {
121             InOnly exchange = getExchangeFactory().createInOnlyExchange();
122             NormalizedMessage message = exchange.createMessage();
123             getMarshaler().populateNormalizedMessage(message, context);
124             exchange.setInMessage(message);
125             send(exchange);
126         }
127         catch (MessagingException e) {
128             throw new JobExecutionException(e);
129         }
130     }
131
132
133     // Properties
134
//-------------------------------------------------------------------------
135
public SchedulerFactory getFactory() {
136         return factory;
137     }
138
139     public void setFactory(SchedulerFactory factory) {
140         this.factory = factory;
141     }
142
143     public Scheduler getScheduler() {
144         return scheduler;
145     }
146
147     public void setScheduler(Scheduler scheduler) {
148         this.scheduler = scheduler;
149     }
150
151     public Map JavaDoc getTriggers() {
152         return triggers;
153     }
154
155     public void setTriggers(Map JavaDoc triggers) {
156         this.triggers = triggers;
157     }
158
159     public QuartzMarshaler getMarshaler() {
160         return marshaler;
161     }
162
163     public void setMarshaler(QuartzMarshaler marshaler) {
164         this.marshaler = marshaler;
165     }
166
167     // Implementation methods
168
//-------------------------------------------------------------------------
169
protected void init() throws JBIException {
170         super.init();
171         try {
172             if (scheduler == null) {
173                 if (factory == null) {
174                     factory = new StdSchedulerFactory();
175                 }
176                 scheduler = factory.getScheduler();
177             }
178         }
179         catch (SchedulerException e) {
180             throw new JBIException(e);
181         }
182
183         if (triggers != null) {
184             for (Iterator JavaDoc iter = triggers.entrySet().iterator(); iter.hasNext();) {
185                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
186                 Object JavaDoc key = entry.getKey();
187                 Object JavaDoc value = entry.getValue();
188                 if (key == null) {
189                     throw new IllegalArgumentException JavaDoc("Key of the map cannot be null");
190                 }
191                 if (value == null) {
192                     throw new IllegalArgumentException JavaDoc("Key of the map cannot be null");
193                 }
194                 if (!(key instanceof Trigger)) {
195                     throw new IllegalArgumentException JavaDoc("Key of the map must be a Trigger but was: " + key.getClass().getName());
196                 }
197                 if (!(value instanceof JobDetail)) {
198                     throw new IllegalArgumentException JavaDoc("Key of the map must be a JobDetail but was: " + value.getClass().getName());
199                 }
200                 addTrigger((Trigger) key, (JobDetail) value);
201             }
202         }
203     }
204
205     public void onMessageExchange(MessageExchange exchange) throws MessagingException {
206         // As we send in-only MEPS, we will only
207
// receive DONE or ERROR status
208
}
209
210 }
211
Popular Tags