KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > engine > GenericAsyncEngine


1 /*
2  * $Id: GenericAsyncEngine.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 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.engine;
26
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.UtilDateTime;
36 import org.ofbiz.base.util.UtilMisc;
37 import org.ofbiz.entity.GenericEntityException;
38 import org.ofbiz.entity.GenericValue;
39 import org.ofbiz.entity.serialize.SerializeException;
40 import org.ofbiz.entity.serialize.XmlSerializer;
41 import org.ofbiz.service.DispatchContext;
42 import org.ofbiz.service.GenericRequester;
43 import org.ofbiz.service.GenericServiceException;
44 import org.ofbiz.service.ModelService;
45 import org.ofbiz.service.ServiceDispatcher;
46 import org.ofbiz.service.config.ServiceConfigUtil;
47 import org.ofbiz.service.job.GenericServiceJob;
48 import org.ofbiz.service.job.Job;
49 import org.ofbiz.service.job.JobManagerException;
50
51 /**
52  * Generic Asynchronous Engine
53  *
54  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
55  * @version $Rev: 5462 $
56  * @since 2.0
57  */

58 public abstract class GenericAsyncEngine extends AbstractEngine {
59     
60     public static final String JavaDoc module = GenericAsyncEngine.class.getName();
61
62     protected GenericAsyncEngine(ServiceDispatcher dispatcher) {
63         super(dispatcher);
64     }
65
66     /**
67      * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
68      */

69     public abstract Map JavaDoc runSync(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException;
70     
71     /**
72      * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
73      */

74     public abstract void runSyncIgnore(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException;
75
76     /**
77      * @see org.ofbiz.service.engine.GenericEngine#runAsync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map, boolean)
78      */

79     public void runAsync(String JavaDoc localName, ModelService modelService, Map JavaDoc context, boolean persist) throws GenericServiceException {
80         runAsync(localName, modelService, context, null, persist);
81     }
82     
83     /**
84      * @see org.ofbiz.service.engine.GenericEngine#runAsync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map, org.ofbiz.service.GenericRequester, boolean)
85      */

86     public void runAsync(String JavaDoc localName, ModelService modelService, Map JavaDoc context, GenericRequester requester, boolean persist) throws GenericServiceException {
87         DispatchContext dctx = dispatcher.getLocalContext(localName);
88         Job job = null;
89
90         if (persist) {
91             // check for a delegator
92
if (dispatcher.getDelegator() == null) {
93                 throw new GenericServiceException("No reference to delegator; cannot run persisted services.");
94             }
95
96             GenericValue jobV = null;
97             // Build the value object(s).
98
try {
99                 List JavaDoc toBeStored = new LinkedList JavaDoc();
100
101                 // Create the runtime data
102
String JavaDoc dataId = dispatcher.getDelegator().getNextSeqId("RuntimeData");
103
104                 GenericValue runtimeData = dispatcher.getDelegator().makeValue("RuntimeData",
105                         UtilMisc.toMap("runtimeDataId", dataId));
106
107                 runtimeData.set("runtimeInfo", XmlSerializer.serialize(context));
108                 toBeStored.add(runtimeData);
109
110                 // Create the job info
111
String JavaDoc jobId = dispatcher.getDelegator().getNextSeqId("JobSandbox").toString();
112                 String JavaDoc jobName = new String JavaDoc(new Long JavaDoc((new Date JavaDoc().getTime())).toString());
113
114
115                 Map JavaDoc jFields = UtilMisc.toMap("jobId", jobId, "jobName", jobName, "runTime", UtilDateTime.nowTimestamp());
116                 jFields.put("poolId", ServiceConfigUtil.getSendPool());
117                 jFields.put("statusId", "SERVICE_PENDING");
118                 jFields.put("serviceName", modelService.name);
119                 jFields.put("loaderName", localName);
120                 jFields.put("maxRetry", new Long JavaDoc(modelService.maxRetry));
121                 jFields.put("runtimeDataId", dataId);
122
123                 jobV = dispatcher.getDelegator().makeValue("JobSandbox", jFields);
124                 toBeStored.add(jobV);
125                 dispatcher.getDelegator().storeAll(toBeStored);
126
127             } catch (GenericEntityException e) {
128                 throw new GenericServiceException("Unable to create persisted job", e);
129             } catch (SerializeException e) {
130                 throw new GenericServiceException("Problem serializing service attributes", e);
131             } catch (FileNotFoundException JavaDoc e) {
132                 throw new GenericServiceException("Problem serializing service attributes", e);
133             } catch (IOException JavaDoc e) {
134                 throw new GenericServiceException("Problem serializing service attributes", e);
135             }
136
137             // make sure we stored okay
138
if (jobV == null) {
139                 throw new GenericServiceException("Persisted job not created");
140             } else {
141                 Debug.logInfo("Persisted job queued : " + jobV.getString("jobName"), module);
142             }
143         } else {
144             String JavaDoc name = new Long JavaDoc(new Date JavaDoc().getTime()).toString();
145             String JavaDoc jobId = modelService.name + "." + name;
146             job = new GenericServiceJob(dctx, jobId, name, modelService.name, context, requester);
147             try {
148                 dispatcher.getJobManager().runJob(job);
149             } catch (JobManagerException jse) {
150                 throw new GenericServiceException("Cannot run job.", jse);
151             }
152         }
153     }
154
155     /**
156      * @see org.ofbiz.service.engine.GenericEngine#sendCallbacks(org.ofbiz.service.ModelService, java.util.Map, java.lang.Object, int)
157      */

158     public void sendCallbacks(ModelService model, Map JavaDoc context, Object JavaDoc cbObj, int mode) throws GenericServiceException {
159         if (mode == GenericEngine.SYNC_MODE) {
160             super.sendCallbacks(model, context, cbObj, mode);
161         }
162     }
163 }
164
165
Popular Tags