use of org.apache.ofbiz.service.job.JobManager in project ofbiz-framework by apache.
the class GenericAsyncEngine method runAsync.
/**
* @see org.apache.ofbiz.service.engine.GenericEngine#runAsync(java.lang.String, org.apache.ofbiz.service.ModelService, java.util.Map, org.apache.ofbiz.service.GenericRequester, boolean)
*/
public void runAsync(String localName, ModelService modelService, Map<String, Object> context, GenericRequester requester, boolean persist) throws GenericServiceException {
DispatchContext dctx = dispatcher.getLocalContext(localName);
Job job = null;
if (persist) {
// check for a delegator
if (dispatcher.getDelegator() == null) {
throw new GenericServiceException("No reference to delegator; cannot run persisted services.");
}
GenericValue jobV = null;
// Build the value object(s).
try {
// Create the runtime data
String dataId = dispatcher.getDelegator().getNextSeqId("RuntimeData");
GenericValue runtimeData = dispatcher.getDelegator().makeValue("RuntimeData", "runtimeDataId", dataId);
runtimeData.set("runtimeInfo", XmlSerializer.serialize(context));
runtimeData.create();
// Get the userLoginId out of the context
String authUserLoginId = null;
if (context.get("userLogin") != null) {
GenericValue userLogin = (GenericValue) context.get("userLogin");
authUserLoginId = userLogin.getString("userLoginId");
}
// Create the job info
String jobId = dispatcher.getDelegator().getNextSeqId("JobSandbox");
String jobName = Long.toString(System.currentTimeMillis());
Map<String, Object> jFields = UtilMisc.toMap("jobId", jobId, "jobName", jobName, "runTime", UtilDateTime.nowTimestamp());
jFields.put("poolId", ServiceConfigUtil.getServiceEngine().getThreadPool().getSendToPool());
jFields.put("statusId", "SERVICE_PENDING");
jFields.put("serviceName", modelService.name);
jFields.put("loaderName", localName);
jFields.put("maxRetry", Long.valueOf(modelService.maxRetry));
jFields.put("runtimeDataId", dataId);
if (UtilValidate.isNotEmpty(authUserLoginId)) {
jFields.put("authUserLoginId", authUserLoginId);
}
jobV = dispatcher.getDelegator().makeValue("JobSandbox", jFields);
jobV.create();
} catch (GenericEntityException e) {
throw new GenericServiceException("Unable to create persisted job", e);
} catch (SerializeException e) {
throw new GenericServiceException("Problem serializing service attributes", e);
} catch (FileNotFoundException e) {
throw new GenericServiceException("Problem serializing service attributes", e);
} catch (IOException e) {
throw new GenericServiceException("Problem serializing service attributes", e);
} catch (GenericConfigException e) {
throw new GenericServiceException("Problem serializing service attributes", e);
}
Debug.logInfo("Persisted job queued : " + jobV.getString("jobName"), module);
} else {
JobManager jMgr = dispatcher.getJobManager();
if (jMgr != null) {
String name = Long.toString(System.currentTimeMillis());
String jobId = modelService.name + "." + name;
job = new GenericServiceJob(dctx, jobId, name, modelService.name, context, requester);
try {
dispatcher.getJobManager().runJob(job);
} catch (JobManagerException jse) {
throw new GenericServiceException("Cannot run job.", jse);
}
} else {
throw new GenericServiceException("Cannot get JobManager instance to invoke the job");
}
}
}
Aggregations