OFBiz业务方法里面,当执行一个service的时候,通常采用如下的方式:
LocalDispatcher dispatcher = dctx.getDispatcher();
Map<String, Object> result = dispatcher.runSync(getServiceName(), getContext());
LocalDispatcher是本地调度器,实现服务的同步异步调度和定时任务的调度。与服务调度相关的类图如下:
![](https://i-blog.csdnimg.cn/blog_migrate/07f2025ba260f430f84863926b845d8b.jpeg)
LocalDispatcher是一个接口,实例化的都是GenericDispatcher类,ContextFilter实现了Servlet Filter,会初始化一个GenericDispatcher,并将其存放在ServletContext中,以备整个应用使用。
在请求处理过程中,如果遇到service的event,那么EventHandler会使用LocalDispatcher执行service。
实际上GenericDispatcher只是一个Proxy,自己并不处理相关的调度工作,真正最苦最累的调度工作是由ServiceDispatcher完成的。下面具体研究一下Dispatcher同步和异步调用方法的实现代码:
1.同步调用
通过dispatcher调用runSync方法,也即是调用GenericDispatcher的runSync方法:
/**
* @see org.ofbiz.service.LocalDispatcher#runSync(java.lang.String, java.util.Map)
*/
public Map<String, Object> runSync(String serviceName, Map<String, ? extends Object> context)
throws ServiceValidationException, GenericServiceException
{
ModelService service = ctx.getModelService(serviceName);
return dispatcher.runSync(this.name, service, context);
}
dispatcher实际是ServiceDispatcher对象。ServiceDispatcher的runSync方法有三百多行,比较复杂,
但最终调用service的是GenericEngine。
GenericEngine engine = this.getGenericEngine(modelService.engineName);
……
Map<String, Object> invokeResult = engine.runSync(localName, modelService, context);
GenericEngine是其工厂类GenericEngineFactory获取的,这个Factory类非常简单:
public class GenericEngineFactory {
protected ServiceDispatcher dispatcher = null;
protected Map<String, GenericEngine> engines = null;
public GenericEngineFactory(ServiceDispatcher dispatcher) {
this.dispatcher = dispatcher;
engines = FastMap.newInstance();
}
/**
* Gets the GenericEngine instance that corresponds to given the name
*@param engineName Name of the engine
*@return GenericEngine that corresponds to the engineName
*/
public GenericEngine getGenericEngine(String engineName) throws GenericServiceException {
Element rootElement = null;
try {
rootElement = ServiceConfigUtil.getXmlRootElement();
} catch (GenericConfigException e) {
throw new GenericServiceException("Error getting Service Engine XML root element", e);
}
Element engineElement = UtilXml.firstChildElement(rootElement, "engine", "name", engineName);
if (engineElement == null) {
throw new GenericServiceException("Cannot find a service engine definition for the engine name [" + engineName + "] in the serviceengine.xml file");
}
String className = engineElement.getAttribute("class");
GenericEngine engine = engines.get(engineName);
if (engine == null) {
synchronized (GenericEngineFactory.class) {
engine = engines.get(engineName);
if (engine == null) {
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class<?> c = loader.loadClass(className);
Constructor cn = c.getConstructor(ServiceDispatcher.class);
engine = (GenericEngine) cn.newInstance(dispatcher);
} catch (Exception e) {
throw new GenericServiceException(e.getMessage(), e);
}
if (engine != null) {
engines.put(engineName, engine);
}
}
}
}
return engine;
}
}
从配置文件serviceengine.xml文件中获取相应的engine子类,如java的是org.ofbiz.service.engine.StandardJavaEngine
bsh的是org.ofbiz.service.engine.BeanShellEngine。
Java的StandardJavaEnignerunSync方法采用的是反射来执行相应的方法,如下:
Class<?> c = cl.loadClass(this.getLocation(modelService));
Method m = c.getMethod(modelService.invoke, DispatchContext.class, Map.class);
result = m.invoke(null, dctx, context);
不同的Engine实现的方式不一样。
2. 异步调用
异步调用怎么实现的呢?实现异步的原理就是启动一个线程来执行相应的业务逻辑,原方法直接返回,从而实现异步。具体实现的时候可以根据实际情况而定,比如 将业务逻辑封装成一个任务,将此任务放到一个任务链中,线程池采用先进先出的方式来选择任务进行执行。OFBiz中怎么实现呢?具体查看 GenericAsyncEngine的runAsync方法发现是通过一个生成一个Job来实现的:
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);
}