KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > job > PersistedServiceJob


1 /*
2  * $Id: PersistedServiceJob.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service.job;
26
27 import java.io.IOException JavaDoc;
28 import java.sql.Timestamp JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.UtilDateTime;
38 import org.ofbiz.base.util.UtilMisc;
39 import org.ofbiz.base.util.UtilProperties;
40 import org.ofbiz.base.util.UtilValidate;
41 import org.ofbiz.entity.GenericDelegator;
42 import org.ofbiz.entity.GenericEntityException;
43 import org.ofbiz.entity.GenericValue;
44 import org.ofbiz.entity.serialize.SerializeException;
45 import org.ofbiz.entity.serialize.XmlSerializer;
46 import org.ofbiz.service.DispatchContext;
47 import org.ofbiz.service.GenericRequester;
48 import org.ofbiz.service.ServiceUtil;
49 import org.ofbiz.service.calendar.RecurrenceInfo;
50 import org.ofbiz.service.config.ServiceConfigUtil;
51 import org.xml.sax.SAXException JavaDoc;
52
53 /**
54  * Entity Service Job - Store => Schedule => Run
55  *
56  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
57  * @version $Rev: 5462 $
58  * @since 2.0
59  */

60 public class PersistedServiceJob extends GenericServiceJob {
61
62     public static final String JavaDoc module = PersistedServiceJob.class.getName();
63
64     private transient GenericDelegator delegator = null;
65     private Timestamp JavaDoc storedDate = null;
66     private long nextRecurrence = -1;
67     private long maxRetry = -1;
68
69     /**
70      * Creates a new PersistedServiceJob
71      * @param dctx
72      * @param jobValue
73      * @param req
74      */

75     public PersistedServiceJob(DispatchContext dctx, GenericValue jobValue, GenericRequester req) {
76         super(jobValue.getString("jobId"), jobValue.getString("jobName"));
77         this.delegator = dctx.getDelegator();
78         this.requester = req;
79         this.dctx = dctx;
80         this.storedDate = jobValue.getTimestamp("runTime");
81         this.runtime = storedDate.getTime();
82         this.maxRetry = jobValue.get("maxRetry") != null ? jobValue.getLong("maxRetry").longValue() : -1;
83     }
84
85     public void queue() throws InvalidJobException {
86         super.queue();
87
88         // refresh the job object
89
GenericValue jobValue = null;
90         try {
91             jobValue = this.getJob();
92             jobValue.refresh();
93         } catch (GenericEntityException e) {
94             runtime = -1;
95             throw new InvalidJobException("Unable to refresh Job object", e);
96         }
97
98         // make sure it isn't already set/cancelled
99
if (runtime != -1) {
100             Timestamp JavaDoc cancelTime = jobValue.getTimestamp("cancelDateTime");
101             Timestamp JavaDoc startTime = jobValue.getTimestamp("startDateTime");
102             if (cancelTime != null || startTime != null) {
103                 // job not available
104
runtime = -1;
105                 throw new InvalidJobException("Job [" + getJobId() + "] is not available");
106
107             } else {
108                 // set the start time to now
109
jobValue.set("startDateTime", UtilDateTime.nowTimestamp());
110                 jobValue.set("statusId", "SERVICE_RUNNING");
111                 try {
112                     jobValue.store();
113                 } catch (GenericEntityException e) {
114                     runtime = -1;
115                     throw new InvalidJobException("Unable to set the startDateTime on the current job [" + getJobId() + "]; not running!", e);
116
117                 }
118             }
119         }
120     }
121
122     /**
123      * @see org.ofbiz.service.job.GenericServiceJob#init()
124      */

125     protected void init() throws InvalidJobException {
126         super.init();
127
128         // configure any addition recurrences
129
GenericValue job = this.getJob();
130         RecurrenceInfo recurrence = JobManager.getRecurrenceInfo(job);
131
132         String JavaDoc instanceId = UtilProperties.getPropertyValue("general.properties", "unique.instanceId", "ofbiz0");
133         if (!instanceId.equals(job.getString("runByInstanceId"))) {
134             throw new InvalidJobException("Job has been accpeted by a different instance!");
135         }
136
137         try {
138             if (recurrence != null) {
139                 recurrence.incrementCurrentCount();
140                 long next = recurrence.next();
141                 createRecurrence(job, next);
142             }
143         } catch (GenericEntityException e) {
144             throw new RuntimeException JavaDoc(e.getMessage());
145         }
146         if (Debug.infoOn()) Debug.logInfo(this.toString() + "[" + getJobId() + "] -- Next runtime: " + nextRecurrence, module);
147     }
148
149     private void createRecurrence(GenericValue job, long next) throws GenericEntityException {
150         if (Debug.verboseOn()) Debug.logVerbose("Next runtime returned: " + next, module);
151
152         if (next > runtime) {
153             String JavaDoc newJobId = job.getDelegator().getNextSeqId("JobSandbox");
154             String JavaDoc pJobId = job.getString("parentJobId");
155             if (pJobId == null) {
156                 pJobId = job.getString("jobId");
157             }
158             GenericValue newJob = GenericValue.create(job);
159             newJob.set("jobId", newJobId);
160             newJob.set("previousJobId", job.getString("jobId"));
161             newJob.set("parentJobId", pJobId);
162             newJob.set("statusId", "SERVICE_PENDING");
163             newJob.set("startDateTime", null);
164             newJob.set("runByInstanceId", null);
165             newJob.set("runTime", new java.sql.Timestamp JavaDoc(next));
166             nextRecurrence = next;
167             delegator.create(newJob);
168             if (Debug.verboseOn()) Debug.logVerbose("Created next job entry: " + newJob, module);
169         }
170     }
171
172     /**
173      * @see org.ofbiz.service.job.GenericServiceJob#finish()
174      */

175     protected void finish() throws InvalidJobException {
176         super.finish();
177
178         // set the finish date
179
GenericValue job = getJob();
180         String JavaDoc status = job.getString("statusId");
181         if (status == null || "SERVICE_RUNNING".equals(status)) {
182             job.set("statusId", "SERVICE_FINISHED");
183         }
184         job.set("finishDateTime", UtilDateTime.nowTimestamp());
185         try {
186             job.store();
187         } catch (GenericEntityException e) {
188             Debug.logError(e, "Cannot update the job [" + getJobId() + "] sandbox", module);
189         }
190     }
191
192     /**
193      * @see org.ofbiz.service.job.GenericServiceJob#failed(Throwable)
194      */

195     protected void failed(Throwable JavaDoc t) throws InvalidJobException {
196         super.failed(t);
197
198         // if the job has not been re-scheduled; we need to re-schedule and run again
199
if (nextRecurrence == -1) {
200             if (this.canRetry()) {
201                 // create a recurrence
202
Calendar JavaDoc cal = Calendar.getInstance();
203                 cal.setTime(new Date JavaDoc());
204                 cal.add(Calendar.MINUTE, ServiceConfigUtil.getFailedRetryMin());
205                 long next = cal.getTimeInMillis();
206                 GenericValue job = getJob();
207                 try {
208                     createRecurrence(job, next);
209                 } catch (GenericEntityException gee) {
210                     Debug.logError(gee, "ERROR: Unable to re-schedule job [" + getJobId() + "] to re-run : " + job, module);
211                 }
212
213                 // set the failed status
214
job.set("statusId", "SERVICE_FAILED");
215                 try {
216                     job.store();
217                 } catch (GenericEntityException e) {
218                     Debug.logError(e, "Cannot update the job sandbox", module);
219                 }
220                 Debug.log("Persisted Job [" + getJobId() + "] Failed Re-Scheduling : " + next, module);
221             } else {
222                 Debug.logWarning("Persisted Job [" + getJobId() + "] Failed - Max Retry Hit; not re-scheduling", module);
223             }
224         }
225     }
226
227     /**
228      * @see org.ofbiz.service.job.GenericServiceJob#getServiceName()
229      */

230     protected String JavaDoc getServiceName() throws InvalidJobException {
231         GenericValue jobObj = getJob();
232         if (jobObj == null || jobObj.get("serviceName") == null) {
233             return null;
234         }
235         return jobObj.getString("serviceName");
236     }
237
238     /**
239      * @see org.ofbiz.service.job.GenericServiceJob#getContext()
240      */

241     protected Map JavaDoc getContext() throws InvalidJobException {
242         Map JavaDoc context = null;
243         try {
244             GenericValue jobObj = getJob();
245             if (!UtilValidate.isEmpty(jobObj.getString("runtimeDataId"))) {
246                 GenericValue contextObj = jobObj.getRelatedOne("RuntimeData");
247                 if (contextObj != null) {
248                     context = (Map JavaDoc) XmlSerializer.deserialize(contextObj.getString("runtimeInfo"), delegator);
249                 }
250             }
251
252             if (context == null) {
253                 context = new HashMap JavaDoc();
254             }
255
256             // check the runAsUser
257
if (!UtilValidate.isEmpty(jobObj.get("runAsUser"))) {
258                 context.put("userLogin", ServiceUtil.getUserLogin(dctx, context, jobObj.getString("runAsUser")));
259             }
260         } catch (GenericEntityException e) {
261             Debug.logError(e, "PersistedServiceJob.getContext(): Entity Exception", module);
262         } catch (SerializeException e) {
263             Debug.logError(e, "PersistedServiceJob.getContext(): Serialize Exception", module);
264         } catch (ParserConfigurationException JavaDoc e) {
265             Debug.logError(e, "PersistedServiceJob.getContext(): Parse Exception", module);
266         } catch (SAXException JavaDoc e) {
267             Debug.logError(e, "PersistedServiceJob.getContext(): SAXException", module);
268         } catch (IOException JavaDoc e) {
269             Debug.logError(e, "PersistedServiceJob.getContext(): IOException", module);
270         }
271         if (context == null) {
272             Debug.logError("Job context is null", module);
273         }
274
275         return context;
276     }
277
278     // gets the job value object
279
private GenericValue getJob() throws InvalidJobException {
280         try {
281             Map JavaDoc fields = UtilMisc.toMap("jobId", getJobId());
282             GenericValue jobObj = delegator.findByPrimaryKey("JobSandbox", fields);
283
284             if (jobObj == null) {
285                 throw new InvalidJobException("Job [" + getJobId() + "] came back null from datasource");
286             }
287             return jobObj;
288         } catch (GenericEntityException e) {
289             throw new InvalidJobException("Cannot get job definition [" + getJobId() + "] from entity", e);
290         }
291     }
292
293     // returns the number of current retries
294
private long getRetries() throws InvalidJobException {
295         GenericValue job = this.getJob();
296         String JavaDoc pJobId = job.getString("parentJobId");
297         if (pJobId == null) {
298             return 0;
299         }
300
301         Map JavaDoc fields = UtilMisc.toMap("parentJobId", pJobId, "statusId", "SERVICE_FAILED");
302         long count = 0;
303         try {
304             count = delegator.findCountByAnd("JobSandbox", fields);
305         } catch (GenericEntityException e) {
306             Debug.logError(e, module);
307         }
308
309         return count + 1; // add one for the parent
310
}
311
312     private boolean canRetry() throws InvalidJobException {
313         if (maxRetry == -1) {
314             return true;
315         }
316         if (this.getRetries() < maxRetry) {
317             return true;
318         }
319         return false;
320     }
321 }
322
Popular Tags