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:
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.
/**
* 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 ");
}
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.
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.