Services invocation mechanism in OFBiz

Source: Internet
Author: User
OFBiz business methods inside, when executing a service, the following is usually the way:

Localdispatcher dispatcher = Dctx.getdispatcher ();
map<string, object> result = Dispatcher.runsync (Getservicename (), GetContext ());

Localdispatcher is a local scheduler that implements synchronous asynchronous scheduling of services and scheduling of scheduled tasks. The class diagram associated with service scheduling is as follows:



Localdispatcher is an interface that instantiates a Genericdispatcher class, Contextfilter implements a servlet Filter, initializes a genericdispatcher, and store it in a servletcontext for the entire application to use.
During request processing, if a service event is encountered, EventHandler uses Localdispatcher to perform the service.
In fact, Genericdispatcher is just a proxy, he does not deal with the relevant scheduling work, the most painful and most exhausting scheduling work is done by Servicedispatcher. The following is a detailed study of the implementation code for Dispatcher synchronous and asynchronous invocation methods:

1. Synchronous invocation

Calling the Runsync method through dispatcher is also the Runsync method that calls Genericdispatcher:

/**
* @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 is actually a Servicedispatcher object. Servicedispatcher's Runsync method has more than 300 lines, more complex,
But the final call to service is genericengine.

Genericengine engine = This.getgenericengine (modelservice.enginename);
......

map<string, object> invokeresult = Engine.runsync (LocalName, modelservice, context);

Genericengine is obtained from its factory class Genericenginefactory, this factory class is very simple:

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

Get the corresponding engine subclass from the configuration file Serviceengine.xml file, as in Java org.ofbiz.service.engine.StandardJavaEngine
The BSH is org.ofbiz.service.engine.BeanShellEngine.

The Java Standardjavaenignerunsync method uses reflection to perform the corresponding method, as follows:

class<?> C = Cl.loadclass (This.getlocation (Modelservice));
Method m = C.getmethod (Modelservice.invoke, Dispatchcontext.class, Map.class);
result = M.invoke (null, DCTX, context);

Different engine implementations are not the same way.

2. Asynchronous invocation

How asynchronous calls are implemented. The principle of asynchronous implementation is to start a thread to execute the corresponding business logic, and the original method returns directly, thus realizing the asynchronous. The implementation of the time can be based on the actual situation, such as the business logic packaged into a task, the task into a task chain, the thread pool in a first-out way to select the task to execute. How to achieve it in ofbiz. The specific view of the Genericasyncengine Runasync method discovery is achieved by creating a 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);
}
For job execution, it is interesting to see my other article on the operation mechanism of job in ofbiz.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.