码迷,mamicode.com
首页 > 其他好文 > 详细

OFBiz中services调用机制

时间:2016-08-18 14:17:02      阅读:388      评论:0      收藏:0      [点我收藏+]

标签:

OFBiz业务方法里面,当执行一个service的时候,通常采用如下的方式:

    LocalDispatcher dispatcher = dctx.getDispatcher();
    Map<String, Object> result = dispatcher.runSync(getServiceName(), getContext());

LocalDispatcher是本地调度器,实现服务的同步异步调度和定时任务的调度。与服务调度相关的类图如下:

技术分享

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);
        }

 

 

 

转载自:http://www.cnblogs.com/Ivan-j2ee/archive/2012/04/13/2445414.html

OFBiz中services调用机制

标签:

原文地址:http://www.cnblogs.com/AsherBlog/p/5783528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!