001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.service.engine;
019:
020: import java.io.FileNotFoundException;
021: import java.io.IOException;
022: import java.util.Date;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.ofbiz.base.util.Debug;
028: import org.ofbiz.base.util.UtilDateTime;
029: import org.ofbiz.base.util.UtilMisc;
030: import org.ofbiz.base.util.UtilValidate;
031: import org.ofbiz.entity.GenericEntityException;
032: import org.ofbiz.entity.GenericValue;
033: import org.ofbiz.entity.serialize.SerializeException;
034: import org.ofbiz.entity.serialize.XmlSerializer;
035: import org.ofbiz.service.DispatchContext;
036: import org.ofbiz.service.GenericRequester;
037: import org.ofbiz.service.GenericServiceException;
038: import org.ofbiz.service.ModelService;
039: import org.ofbiz.service.ServiceDispatcher;
040: import org.ofbiz.service.config.ServiceConfigUtil;
041: import org.ofbiz.service.job.GenericServiceJob;
042: import org.ofbiz.service.job.Job;
043: import org.ofbiz.service.job.JobManagerException;
044:
045: /**
046: * Generic Asynchronous Engine
047: */
048: public abstract class GenericAsyncEngine extends AbstractEngine {
049:
050: public static final String module = GenericAsyncEngine.class
051: .getName();
052:
053: protected GenericAsyncEngine(ServiceDispatcher dispatcher) {
054: super (dispatcher);
055: }
056:
057: /**
058: * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
059: */
060: public abstract Map runSync(String localName,
061: ModelService modelService, Map context)
062: throws GenericServiceException;
063:
064: /**
065: * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
066: */
067: public abstract void runSyncIgnore(String localName,
068: ModelService modelService, Map context)
069: throws GenericServiceException;
070:
071: /**
072: * @see org.ofbiz.service.engine.GenericEngine#runAsync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map, boolean)
073: */
074: public void runAsync(String localName, ModelService modelService,
075: Map context, boolean persist)
076: throws GenericServiceException {
077: runAsync(localName, modelService, context, null, persist);
078: }
079:
080: /**
081: * @see org.ofbiz.service.engine.GenericEngine#runAsync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map, org.ofbiz.service.GenericRequester, boolean)
082: */
083: public void runAsync(String localName, ModelService modelService,
084: Map context, GenericRequester requester, boolean persist)
085: throws GenericServiceException {
086: DispatchContext dctx = dispatcher.getLocalContext(localName);
087: Job job = null;
088:
089: if (persist) {
090: // check for a delegator
091: if (dispatcher.getDelegator() == null) {
092: throw new GenericServiceException(
093: "No reference to delegator; cannot run persisted services.");
094: }
095:
096: GenericValue jobV = null;
097: // Build the value object(s).
098: try {
099: List toBeStored = new LinkedList();
100:
101: // Create the runtime data
102: String dataId = dispatcher.getDelegator().getNextSeqId(
103: "RuntimeData");
104:
105: GenericValue runtimeData = dispatcher
106: .getDelegator()
107: .makeValue("RuntimeData",
108: UtilMisc.toMap("runtimeDataId", dataId));
109:
110: runtimeData.set("runtimeInfo", XmlSerializer
111: .serialize(context));
112: toBeStored.add(runtimeData);
113:
114: // Get the userLoginId out of the context
115: String authUserLoginId = null;
116: if (context.containsKey("userLogin")) {
117: GenericValue userLogin = (GenericValue) context
118: .get("userLogin");
119: authUserLoginId = userLogin
120: .getString("userLoginId");
121: }
122:
123: // Create the job info
124: String jobId = dispatcher.getDelegator().getNextSeqId(
125: "JobSandbox");
126: String jobName = Long.toString((new Date().getTime()));
127:
128: Map jFields = UtilMisc
129: .toMap("jobId", jobId, "jobName", jobName,
130: "runTime", UtilDateTime.nowTimestamp());
131: jFields.put("poolId", ServiceConfigUtil.getSendPool());
132: jFields.put("statusId", "SERVICE_PENDING");
133: jFields.put("serviceName", modelService.name);
134: jFields.put("loaderName", localName);
135: jFields
136: .put("maxRetry",
137: new Long(modelService.maxRetry));
138: jFields.put("runtimeDataId", dataId);
139: if (UtilValidate.isNotEmpty(authUserLoginId)) {
140: jFields.put("authUserLoginId", authUserLoginId);
141: }
142:
143: jobV = dispatcher.getDelegator().makeValue(
144: "JobSandbox", jFields);
145: toBeStored.add(jobV);
146: dispatcher.getDelegator().storeAll(toBeStored);
147:
148: } catch (GenericEntityException e) {
149: throw new GenericServiceException(
150: "Unable to create persisted job", e);
151: } catch (SerializeException e) {
152: throw new GenericServiceException(
153: "Problem serializing service attributes", e);
154: } catch (FileNotFoundException e) {
155: throw new GenericServiceException(
156: "Problem serializing service attributes", e);
157: } catch (IOException e) {
158: throw new GenericServiceException(
159: "Problem serializing service attributes", e);
160: }
161:
162: // make sure we stored okay
163: if (jobV == null) {
164: throw new GenericServiceException(
165: "Persisted job not created");
166: } else {
167: Debug.logInfo("Persisted job queued : "
168: + jobV.getString("jobName"), module);
169: }
170: } else {
171: String name = Long.toString(new Date().getTime());
172: String jobId = modelService.name + "." + name;
173: job = new GenericServiceJob(dctx, jobId, name,
174: modelService.name, context, requester);
175: try {
176: dispatcher.getJobManager().runJob(job);
177: } catch (JobManagerException jse) {
178: throw new GenericServiceException("Cannot run job.",
179: jse);
180: }
181: }
182: }
183:
184: /**
185: * @see org.ofbiz.service.engine.GenericEngine#sendCallbacks(org.ofbiz.service.ModelService, java.util.Map, java.lang.Object, int)
186: */
187: public void sendCallbacks(ModelService model, Map context,
188: Object cbObj, int mode) throws GenericServiceException {
189: if (mode == GenericEngine.SYNC_MODE) {
190: super.sendCallbacks(model, context, cbObj, mode);
191: }
192: }
193: }
|