1 25 package org.ofbiz.service.engine; 26 27 import java.io.FileNotFoundException ; 28 import java.io.IOException ; 29 import java.util.Date ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Map ; 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 58 public abstract class GenericAsyncEngine extends AbstractEngine { 59 60 public static final String module = GenericAsyncEngine.class.getName(); 61 62 protected GenericAsyncEngine(ServiceDispatcher dispatcher) { 63 super(dispatcher); 64 } 65 66 69 public abstract Map runSync(String localName, ModelService modelService, Map context) throws GenericServiceException; 70 71 74 public abstract void runSyncIgnore(String localName, ModelService modelService, Map context) throws GenericServiceException; 75 76 79 public void runAsync(String localName, ModelService modelService, Map context, boolean persist) throws GenericServiceException { 80 runAsync(localName, modelService, context, null, persist); 81 } 82 83 86 public void runAsync(String localName, ModelService modelService, Map context, GenericRequester requester, boolean persist) throws GenericServiceException { 87 DispatchContext dctx = dispatcher.getLocalContext(localName); 88 Job job = null; 89 90 if (persist) { 91 if (dispatcher.getDelegator() == null) { 93 throw new GenericServiceException("No reference to delegator; cannot run persisted services."); 94 } 95 96 GenericValue jobV = null; 97 try { 99 List toBeStored = new LinkedList (); 100 101 String 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 String jobId = dispatcher.getDelegator().getNextSeqId("JobSandbox").toString(); 112 String jobName = new String (new Long ((new Date ().getTime())).toString()); 113 114 115 Map 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 (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 e) { 132 throw new GenericServiceException("Problem serializing service attributes", e); 133 } catch (IOException e) { 134 throw new GenericServiceException("Problem serializing service attributes", e); 135 } 136 137 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 name = new Long (new Date ().getTime()).toString(); 145 String 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 158 public void sendCallbacks(ModelService model, Map context, Object 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 |