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.job;
019:
020: import java.util.Date;
021: import java.util.Map;
022:
023: import org.ofbiz.base.util.Debug;
024: import org.ofbiz.service.DispatchContext;
025: import org.ofbiz.service.GenericRequester;
026: import org.ofbiz.service.LocalDispatcher;
027: import org.ofbiz.service.ModelService;
028:
029: /**
030: * Generic Service Job - A generic async-service Job.
031: */
032: public class GenericServiceJob extends AbstractJob {
033:
034: public static final String module = GenericServiceJob.class
035: .getName();
036:
037: protected transient GenericRequester requester = null;
038: protected transient DispatchContext dctx = null;
039:
040: private String service = null;
041: private Map context = null;
042:
043: public GenericServiceJob(DispatchContext dctx, String jobId,
044: String jobName, String service, Map context,
045: GenericRequester req) {
046: super (jobId, jobName);
047: this .dctx = dctx;
048: this .service = service;
049: this .context = context;
050: this .requester = req;
051: runtime = new Date().getTime();
052: }
053:
054: protected GenericServiceJob(String jobId, String jobName) {
055: super (jobId, jobName);
056: this .dctx = null;
057: this .requester = null;
058: this .service = null;
059: this .context = null;
060: }
061:
062: /**
063: * Invokes the service.
064: */
065: public void exec() throws InvalidJobException {
066: init();
067:
068: // no transaction is necessary since runSync handles this
069: try {
070: // get the dispatcher and invoke the service via runSync -- will run all ECAs
071: LocalDispatcher dispatcher = dctx.getDispatcher();
072: Map result = dispatcher.runSync(getServiceName(),
073: getContext());
074:
075: // check for a failure
076: boolean isError = ModelService.RESPOND_ERROR.equals(result
077: .get(ModelService.RESPONSE_MESSAGE));
078: if (isError) {
079: String errorMessage = (String) result
080: .get(ModelService.ERROR_MESSAGE);
081: this .failed(new Exception(errorMessage));
082: }
083:
084: if (requester != null) {
085: requester.receiveResult(result);
086: }
087:
088: } catch (Throwable t) {
089: // pass the exception back to the requester.
090: if (requester != null) {
091: requester.receiveThrowable(t);
092: }
093:
094: // call the failed method
095: this .failed(t);
096: }
097:
098: // call the finish method
099: this .finish();
100: }
101:
102: /**
103: * Method is called prior to running the service.
104: */
105: protected void init() throws InvalidJobException {
106: if (Debug.verboseOn())
107: Debug.logVerbose("Async-Service initializing.", module);
108: }
109:
110: /**
111: * Method is called after the service has finished.
112: */
113: protected void finish() throws InvalidJobException {
114: if (Debug.verboseOn())
115: Debug.logVerbose("Async-Service finished.", module);
116: runtime = 0;
117: }
118:
119: /**
120: * Method is called when the service fails.
121: * @param t Throwable
122: */
123: protected void failed(Throwable t) throws InvalidJobException {
124: Debug.logError(t, "Async-Service failed.", module);
125: runtime = 0;
126: }
127:
128: /**
129: * Gets the context for the service invocation.
130: * @return Map of name value pairs making up the service context.
131: */
132: protected Map getContext() throws InvalidJobException {
133: return context;
134: }
135:
136: /**
137: * Gets the name of the service as defined in the definition file.
138: * @return The name of the service to be invoked.
139: */
140: protected String getServiceName() throws InvalidJobException {
141: return service;
142: }
143: }
|