001: /*
002: * $Id: PersistedServiceJob.java,v 1.7 2003/12/14 02:16:47 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.service.job;
026:
027: import java.io.IOException;
028: import java.sql.Timestamp;
029: import java.util.HashMap;
030: import java.util.Map;
031: import java.util.Calendar;
032: import java.util.Date;
033:
034: import javax.xml.parsers.ParserConfigurationException;
035:
036: import org.ofbiz.base.util.Debug;
037: import org.ofbiz.base.util.UtilDateTime;
038: import org.ofbiz.base.util.UtilMisc;
039: import org.ofbiz.base.util.UtilProperties;
040: import org.ofbiz.entity.GenericDelegator;
041: import org.ofbiz.entity.GenericEntityException;
042: import org.ofbiz.entity.GenericValue;
043: import org.ofbiz.entity.serialize.SerializeException;
044: import org.ofbiz.entity.serialize.XmlSerializer;
045: import org.ofbiz.service.DispatchContext;
046: import org.ofbiz.service.GenericRequester;
047: import org.ofbiz.service.config.ServiceConfigUtil;
048: import org.ofbiz.service.calendar.RecurrenceInfo;
049: import org.xml.sax.SAXException;
050:
051: /**
052: * Entity Service Job - Store => Schedule => Run
053: *
054: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
055: * @version $Revision: 1.7 $
056: * @since 2.0
057: */
058: public class PersistedServiceJob extends GenericServiceJob {
059:
060: public static final String module = PersistedServiceJob.class
061: .getName();
062:
063: private transient GenericDelegator delegator = null;
064: private Timestamp storedDate = null;
065: private long nextRecurrence = -1;
066:
067: /**
068: * Creates a new PersistedServiceJob
069: * @param dctx
070: * @param jobValue
071: * @param req
072: */
073: public PersistedServiceJob(DispatchContext dctx,
074: GenericValue jobValue, GenericRequester req) {
075: super (jobValue.getString("jobName"));
076: this .delegator = dctx.getDelegator();
077: this .requester = req;
078: this .dctx = dctx;
079: this .storedDate = jobValue.getTimestamp("runTime");
080: this .runtime = storedDate.getTime();
081:
082: // set the start time to now
083: String instanceId = UtilProperties.getPropertyValue(
084: "general.properties", "unique.instanceId", "ofbiz0");
085: jobValue.set("startDateTime", UtilDateTime.nowTimestamp());
086: jobValue.set("runByInstanceId", instanceId);
087: try {
088: jobValue.store();
089: } catch (GenericEntityException e) {
090: Debug
091: .logError(e,
092: "Unable to set the startDateTime on the current job; not running!");
093: runtime = -1;
094: }
095: }
096:
097: /**
098: * @see org.ofbiz.service.job.GenericServiceJob#init()
099: */
100: protected void init() {
101: super .init();
102:
103: // configure any addition recurrences
104: GenericValue job = this .getJob();
105: RecurrenceInfo recurrence = JobManager.getRecurrenceInfo(job);
106:
107: try {
108: if (recurrence != null) {
109: recurrence.incrementCurrentCount();
110: long next = recurrence.next();
111: createRecurrence(job, next);
112: }
113: } catch (GenericEntityException e) {
114: throw new RuntimeException(e.getMessage());
115: }
116: if (Debug.infoOn())
117: Debug.logInfo(this .toString() + " -- Next runtime: "
118: + nextRecurrence, module);
119: }
120:
121: private void createRecurrence(GenericValue job, long next)
122: throws GenericEntityException {
123: if (Debug.verboseOn())
124: Debug.logVerbose("Next runtime returned: " + next, module);
125: GenericValue newJob = new GenericValue(job);
126: if (next > runtime) {
127: newJob.set("startDateTime", null);
128: newJob.set("runTime", new java.sql.Timestamp(next));
129: nextRecurrence = next;
130: delegator.create(newJob);
131: if (Debug.verboseOn())
132: Debug.logVerbose("Created next job entry: " + newJob,
133: module);
134: }
135: }
136:
137: /**
138: * @see org.ofbiz.service.job.GenericServiceJob#finish()
139: */
140: protected void finish() {
141: super .finish();
142:
143: // set the finish date
144: GenericValue job = getJob();
145: job.set("finishDateTime", UtilDateTime.nowTimestamp());
146: try {
147: job.store();
148: } catch (GenericEntityException e) {
149: Debug.logError(e, "Cannot update the datasource", module);
150: }
151: }
152:
153: /**
154: * @see org.ofbiz.service.job.GenericServiceJob#failed(Throwable)
155: */
156: protected void failed(Throwable t) {
157: super .failed(t);
158:
159: // if the job has not been re-scheduled; we need to re-schedule and run again
160: if (nextRecurrence == -1) {
161: // create a recurrence
162: Calendar cal = Calendar.getInstance();
163: cal.setTime(new Date());
164: cal.add(Calendar.MINUTE, ServiceConfigUtil
165: .getFailedRetryMin());
166: long next = cal.getTimeInMillis();
167: GenericValue job = getJob();
168: try {
169: createRecurrence(job, next);
170: } catch (GenericEntityException gee) {
171: Debug.logError(gee,
172: "ERROR: Unable to re-schedule job to re-run : "
173: + job, module);
174: }
175: Debug.log("Persisted Job Failed Re-Scheduling : " + next,
176: module);
177: }
178: }
179:
180: /**
181: * @see org.ofbiz.service.job.GenericServiceJob#getServiceName()
182: */
183: protected String getServiceName() {
184: GenericValue jobObj = getJob();
185: if (jobObj == null || jobObj.get("serviceName") == null)
186: return null;
187: return jobObj.getString("serviceName");
188: }
189:
190: /**
191: * @see org.ofbiz.service.job.GenericServiceJob#getContext()
192: */
193: protected Map getContext() {
194: Map context = null;
195: try {
196: GenericValue jobObj = getJob();
197: GenericValue contextObj = jobObj
198: .getRelatedOne("RuntimeData");
199:
200: if (contextObj != null) {
201: context = (Map) XmlSerializer.deserialize(contextObj
202: .getString("runtimeInfo"), delegator);
203: }
204:
205: if (context == null) {
206: context = new HashMap();
207: }
208:
209: // check the runAsUser
210: GenericValue runAsUser = jobObj
211: .getRelatedOne("RunAsUserLogin");
212: if (runAsUser != null) {
213: context.put("userLogin", runAsUser);
214: }
215: } catch (GenericEntityException e) {
216: Debug
217: .logError(
218: e,
219: "PersistedServiceJob.getContext(): Entity Exception",
220: module);
221: } catch (SerializeException e) {
222: Debug
223: .logError(
224: e,
225: "PersistedServiceJob.getContext(): Serialize Exception",
226: module);
227: } catch (ParserConfigurationException e) {
228: Debug
229: .logError(
230: e,
231: "PersistedServiceJob.getContext(): Parse Exception",
232: module);
233: } catch (SAXException e) {
234: Debug.logError(e,
235: "PersistedServiceJob.getContext(): SAXException",
236: module);
237: } catch (IOException e) {
238: Debug.logError(e,
239: "PersistedServiceJob.getContext(): IOException",
240: module);
241: }
242: if (context == null)
243: Debug.logError("Job context is null", module);
244: return context;
245: }
246:
247: // gets the job value object
248: private GenericValue getJob() {
249: try {
250: Map fields = UtilMisc.toMap("jobName", getJobName(),
251: "runTime", storedDate);
252: GenericValue jobObj = delegator.findByPrimaryKey(
253: "JobSandbox", fields);
254:
255: if (jobObj == null)
256: Debug.logError("Job came back null from datasource",
257: module);
258: return jobObj;
259: } catch (GenericEntityException e) {
260: Debug.logError(e, "Cannot get job definition from entity",
261: module);
262: e.printStackTrace();
263: }
264: return null;
265: }
266: }
|