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;
019:
020: import java.util.Date;
021: import java.util.Map;
022:
023: import javax.transaction.xa.XAException;
024:
025: import org.ofbiz.service.calendar.RecurrenceRule;
026: import org.ofbiz.entity.GenericDelegator;
027: import org.ofbiz.security.Security;
028: import org.ofbiz.service.jms.JmsListenerFactory;
029: import org.ofbiz.service.job.JobManager;
030: import org.ofbiz.service.job.JobManagerException;
031: import org.ofbiz.base.util.Debug;
032:
033: /**
034: * Generic Services Local Dispatcher
035: */
036: public abstract class GenericAbstractDispatcher implements
037: LocalDispatcher {
038:
039: public static final String module = GenericAbstractDispatcher.class
040: .getName();
041:
042: protected DispatchContext ctx = null;
043: protected ServiceDispatcher dispatcher = null;
044: protected String name = null;
045:
046: public GenericAbstractDispatcher() {
047: }
048:
049: /**
050: * @see org.ofbiz.service.LocalDispatcher#schedule(java.lang.String, java.lang.String, java.util.Map, long, int, int, int, long, int)
051: */
052: public void schedule(String poolName, String serviceName,
053: Map context, long startTime, int frequency, int interval,
054: int count, long endTime, int maxRetry)
055: throws GenericServiceException {
056: schedule(null, poolName, serviceName, context, startTime,
057: frequency, interval, count, endTime, maxRetry);
058: }
059:
060: /**
061: * @see org.ofbiz.service.LocalDispatcher#schedule(java.lang.String, java.lang.String, java.lang.String, java.util.Map, long, int, int, int, long, int)
062: */
063: public void schedule(String jobName, String poolName,
064: String serviceName, Map context, long startTime,
065: int frequency, int interval, int count, long endTime,
066: int maxRetry) throws GenericServiceException {
067: try {
068: getJobManager().schedule(jobName, poolName, serviceName,
069: context, startTime, frequency, interval, count,
070: endTime, maxRetry);
071:
072: if (Debug.verboseOn()) {
073: Debug.logVerbose(
074: "[LocalDispatcher.schedule] : Current time : "
075: + (new Date()).getTime(), module);
076: Debug.logVerbose(
077: "[LocalDispatcher.schedule] : Runtime : "
078: + startTime, module);
079: Debug.logVerbose(
080: "[LocalDispatcher.schedule] : Frequency : "
081: + frequency, module);
082: Debug.logVerbose(
083: "[LocalDispatcher.schedule] : Interval : "
084: + interval, module);
085: Debug.logVerbose(
086: "[LocalDispatcher.schedule] : Count : "
087: + count, module);
088: Debug.logVerbose(
089: "[LocalDispatcher.schedule] : EndTime : "
090: + endTime, module);
091: Debug.logVerbose(
092: "[LocalDispatcher.schedule] : MazRetry : "
093: + maxRetry, module);
094: }
095:
096: } catch (JobManagerException e) {
097: throw new GenericServiceException(e.getMessage(), e);
098: }
099: }
100:
101: /**
102: * @see org.ofbiz.service.LocalDispatcher#setRollbackService(java.lang.String, java.util.Map, boolean)
103: */
104: public void addRollbackService(String serviceName, Map context,
105: boolean persist) throws GenericServiceException {
106: ServiceXaWrapper xa = new ServiceXaWrapper(this
107: .getDispatchContext());
108: xa.setRollbackService(serviceName, context, true, persist);
109: try {
110: xa.enlist();
111: } catch (XAException e) {
112: Debug.logError(e, module);
113: throw new GenericServiceException(e.getMessage(), e);
114: }
115: }
116:
117: /**
118: * @see org.ofbiz.service.LocalDispatcher#setCommitService(java.lang.String, java.util.Map, boolean)
119: */
120: public void addCommitService(String serviceName, Map context,
121: boolean persist) throws GenericServiceException {
122: ServiceXaWrapper xa = new ServiceXaWrapper(this
123: .getDispatchContext());
124: xa.setCommitService(serviceName, context, true, persist);
125: try {
126: xa.enlist();
127: } catch (XAException e) {
128: Debug.logError(e, module);
129: throw new GenericServiceException(e.getMessage(), e);
130: }
131: }
132:
133: /**
134: * @see org.ofbiz.service.LocalDispatcher#schedule(java.lang.String, java.util.Map, long, int, int, int, long)
135: */
136: public void schedule(String serviceName, Map context,
137: long startTime, int frequency, int interval, int count,
138: long endTime) throws GenericServiceException {
139: ModelService model = ctx.getModelService(serviceName);
140: schedule(null, serviceName, context, startTime, frequency,
141: interval, count, endTime, model.maxRetry);
142: }
143:
144: /**
145: * @see org.ofbiz.service.LocalDispatcher#schedule(java.lang.String, java.util.Map, long, int, int, int)
146: */
147: public void schedule(String serviceName, Map context,
148: long startTime, int frequency, int interval, int count)
149: throws GenericServiceException {
150: schedule(serviceName, context, startTime, frequency, interval,
151: count, 0);
152: }
153:
154: /**
155: * @see org.ofbiz.service.LocalDispatcher#schedule(java.lang.String, java.util.Map, long, int, int, long)
156: */
157: public void schedule(String serviceName, Map context,
158: long startTime, int frequency, int interval, long endTime)
159: throws GenericServiceException {
160: schedule(serviceName, context, startTime, frequency, interval,
161: -1, endTime);
162: }
163:
164: /**
165: * @see org.ofbiz.service.LocalDispatcher#schedule(java.lang.String, java.util.Map, long)
166: */
167: public void schedule(String serviceName, Map context, long startTime)
168: throws GenericServiceException {
169: schedule(serviceName, context, startTime, RecurrenceRule.DAILY,
170: 1, 1);
171: }
172:
173: /**
174: * @see org.ofbiz.service.LocalDispatcher#getJobManager()
175: */
176: public JobManager getJobManager() {
177: return dispatcher.getJobManager();
178: }
179:
180: /**
181: * @see org.ofbiz.service.LocalDispatcher#getJMSListeneFactory()
182: */
183: public JmsListenerFactory getJMSListeneFactory() {
184: return dispatcher.getJMSListenerFactory();
185: }
186:
187: /**
188: * @see org.ofbiz.service.LocalDispatcher#getDelegator()
189: */
190: public GenericDelegator getDelegator() {
191: return dispatcher.getDelegator();
192: }
193:
194: /**
195: * @see org.ofbiz.service.LocalDispatcher#getSecurity()
196: */
197: public Security getSecurity() {
198: return dispatcher.getSecurity();
199: }
200:
201: /**
202: * @see org.ofbiz.service.LocalDispatcher#getName()
203: */
204: public String getName() {
205: return this .name;
206: }
207:
208: /**
209: * @see org.ofbiz.service.LocalDispatcher#getDispatchContext()
210: */
211: public DispatchContext getDispatchContext() {
212: return ctx;
213: }
214:
215: /**
216: * @see org.ofbiz.service.LocalDispatcher#deregister()
217: */
218: public void deregister() {
219: dispatcher.deregister(this );
220: }
221:
222: /**
223: * @see org.ofbiz.service.LocalDispatcher#registerCallback(String, GenericServiceCallback)
224: */
225: public void registerCallback(String serviceName,
226: GenericServiceCallback cb) {
227: dispatcher.registerCallback(serviceName, cb);
228: }
229: }
|