1 25 package org.ofbiz.service.job; 26 27 import java.util.Date ; 28 import java.util.Map ; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.service.DispatchContext; 32 import org.ofbiz.service.GenericRequester; 33 import org.ofbiz.service.LocalDispatcher; 34 import org.ofbiz.service.ModelService; 35 36 43 public class GenericServiceJob extends AbstractJob { 44 45 public static final String module = GenericServiceJob.class.getName(); 46 47 protected transient GenericRequester requester = null; 48 protected transient DispatchContext dctx = null; 49 50 private String service = null; 51 private Map context = null; 52 53 public GenericServiceJob(DispatchContext dctx, String jobId, String jobName, String service, Map context, GenericRequester req) { 54 super(jobId, jobName); 55 this.dctx = dctx; 56 this.service = service; 57 this.context = context; 58 this.requester = req; 59 runtime = new Date ().getTime(); 60 } 61 62 protected GenericServiceJob(String jobId, String jobName) { 63 super(jobId, jobName); 64 this.dctx = null; 65 this.requester = null; 66 this.service = null; 67 this.context = null; 68 } 69 70 73 public void exec() throws InvalidJobException { 74 init(); 75 76 try { 78 LocalDispatcher dispatcher = dctx.getDispatcher(); 80 Map result = dispatcher.runSync(getServiceName(), getContext()); 81 82 boolean isError = ModelService.RESPOND_ERROR.equals(result.get(ModelService.RESPONSE_MESSAGE)); 84 if (isError) { 85 String errorMessage = (String ) result.get(ModelService.ERROR_MESSAGE); 86 this.failed(new Exception (errorMessage)); 87 } 88 89 if (requester != null) { 90 requester.receiveResult(result); 91 } 92 93 } catch (Throwable t) { 94 if (requester != null) { 96 requester.receiveThrowable(t); 97 } 98 99 this.failed(t); 101 } 102 103 this.finish(); 105 } 106 107 110 protected void init() throws InvalidJobException { 111 if (Debug.verboseOn()) Debug.logVerbose("Async-Service initializing.", module); 112 } 113 114 117 protected void finish() throws InvalidJobException { 118 if (Debug.verboseOn()) Debug.logVerbose("Async-Service finished.", module); 119 runtime = 0; 120 } 121 122 126 protected void failed(Throwable t) throws InvalidJobException { 127 Debug.logError(t, "Async-Service failed.", module); 128 runtime = 0; 129 } 130 131 135 protected Map getContext() throws InvalidJobException { 136 return context; 137 } 138 139 143 protected String getServiceName() throws InvalidJobException { 144 return service; 145 } 146 } 147 | Popular Tags |