001: /*
002: * $Id: GenericAsyncEngine.java,v 1.3 2004/03/12 23:50:45 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.engine;
026:
027: import java.io.FileNotFoundException;
028: import java.io.IOException;
029: import java.util.Date;
030: import java.util.LinkedList;
031: import java.util.List;
032: import java.util.Map;
033:
034: import org.ofbiz.base.util.Debug;
035: import org.ofbiz.base.util.UtilDateTime;
036: import org.ofbiz.base.util.UtilMisc;
037: import org.ofbiz.entity.GenericEntityException;
038: import org.ofbiz.entity.GenericValue;
039: import org.ofbiz.entity.serialize.SerializeException;
040: import org.ofbiz.entity.serialize.XmlSerializer;
041: import org.ofbiz.service.*;
042: import org.ofbiz.service.config.ServiceConfigUtil;
043: import org.ofbiz.service.job.GenericServiceJob;
044: import org.ofbiz.service.job.Job;
045: import org.ofbiz.service.job.JobManagerException;
046:
047: /**
048: * Generic Asynchronous Engine
049: *
050: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
051: * @version $Revision: 1.3 $
052: * @since 2.0
053: */
054: public abstract class GenericAsyncEngine implements GenericEngine {
055:
056: public static final String module = GenericAsyncEngine.class
057: .getName();
058:
059: protected ServiceDispatcher dispatcher = null;
060:
061: protected GenericAsyncEngine(ServiceDispatcher dispatcher) {
062: this .dispatcher = dispatcher;
063: }
064:
065: /**
066: * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
067: */
068: public abstract Map runSync(String localName,
069: ModelService modelService, Map context)
070: throws GenericServiceException;
071:
072: /**
073: * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
074: */
075: public abstract void runSyncIgnore(String localName,
076: ModelService modelService, Map context)
077: throws GenericServiceException;
078:
079: /**
080: * @see org.ofbiz.service.engine.GenericEngine#runAsync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map, boolean)
081: */
082: public void runAsync(String localName, ModelService modelService,
083: Map context, boolean persist)
084: throws GenericServiceException {
085: runAsync(localName, modelService, context, null, persist);
086: }
087:
088: /**
089: * @see org.ofbiz.service.engine.GenericEngine#runAsync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map, org.ofbiz.service.GenericRequester, boolean)
090: */
091: public void runAsync(String localName, ModelService modelService,
092: Map context, GenericRequester requester, boolean persist)
093: throws GenericServiceException {
094: DispatchContext dctx = dispatcher.getLocalContext(localName);
095: Job job = null;
096:
097: if (persist) {
098: // check for a delegator
099: if (dispatcher.getDelegator() == null) {
100: throw new GenericServiceException(
101: "No reference to delegator; cannot run persisted services.");
102: }
103:
104: GenericValue jobV = null;
105: // Build the value object(s).
106: try {
107: List toBeStored = new LinkedList();
108:
109: // Create the runtime data
110: String dataId = dispatcher.getDelegator().getNextSeqId(
111: "RuntimeData").toString();
112:
113: GenericValue runtimeData = dispatcher
114: .getDelegator()
115: .makeValue("RuntimeData",
116: UtilMisc.toMap("runtimeDataId", dataId));
117:
118: runtimeData.set("runtimeInfo", XmlSerializer
119: .serialize(context));
120: toBeStored.add(runtimeData);
121:
122: // Create the job info
123: String jobName = new String(new Long((new Date()
124: .getTime())).toString());
125: Map jFields = UtilMisc.toMap("jobName", jobName,
126: "runTime", UtilDateTime.nowTimestamp(),
127: "poolId", ServiceConfigUtil.getSendPool(),
128: "serviceName", modelService.name, "loaderName",
129: localName, "runtimeDataId", dataId);
130:
131: jobV = dispatcher.getDelegator().makeValue(
132: "JobSandbox", jFields);
133: toBeStored.add(jobV);
134: dispatcher.getDelegator().storeAll(toBeStored);
135:
136: } catch (GenericEntityException e) {
137: throw new GenericServiceException(
138: "Unable to create persisted job", e);
139: } catch (SerializeException e) {
140: throw new GenericServiceException(
141: "Problem serializing service attributes", e);
142: } catch (FileNotFoundException e) {
143: throw new GenericServiceException(
144: "Problem serializing service attributes", e);
145: } catch (IOException e) {
146: throw new GenericServiceException(
147: "Problem serializing service attributes", e);
148: }
149:
150: // make sure we stored okay
151: if (jobV == null) {
152: throw new GenericServiceException(
153: "Persisted job not created");
154: } else {
155: Debug.logInfo("Persisted job queued : "
156: + jobV.getString("jobName"), module);
157: }
158: } else {
159: String name = new Long(new Date().getTime()).toString();
160: job = new GenericServiceJob(dctx, name, modelService.name,
161: context, requester);
162: try {
163: dispatcher.getJobManager().runJob(job);
164: } catch (JobManagerException jse) {
165: throw new GenericServiceException("Cannot run job.",
166: jse);
167: }
168: }
169: }
170: }
|