Play request processing flow frame

Play framework uses an event-driven model to provide flexible processing capabilities in an unpredictable operating environment.

In a web application, the event mainly refers to users initiate a HTTP request to the server. For the Play framework, such events are defined in the routes file, play the contents of the file and routes the user's request, to determine which procedures should be invoked. Play Netty frame using the server, the server uses a pipeline (Pipeline), provides excellent asynchronous processing capacity under high concurrency conditions.

When the server receives a user request to the time, you will get a pipeline, an incoming request information, frame after referred Play process. Play framework will be based on the content of the request, find the corresponding routing rules, call routing rules based on the corresponding event processing flow, and (in general will) eventually return results to the user, to complete a deal with the event:

Figure 1. The event flow

A user request processing flow Class

As a web application framework, the basic framework is Play function in response to user requests. In this section, the user requests a summary about when (request) arrives, play what will start to process the request is processed, and finally back to the appropriate user. This section focuses on the stated process, since this process involves three aspects to the MVC, for a specific implementation details will be described later.

 

First, the introduction of related classes

Before describing the processing flow, we need to introduce some of the classes involved in the process herein. This section focuses on the class structure of these classes, and they play a major role in this process.

 

1、PlayHandler

PlayHandler inherited org.jboss.netty.channel.SimpleChannelUpstreamHandler, in the pipeline for processing user requests to the server listens. FIG class as follows:

FIG 2 PlayHandler FIG class

The more important thing is

messageReceived(

 final ChannelHandlerContext ctx,

 final MessageEvent messageEvent)

method. The method of rewriting the same name as the parent class function (Override). Parent provide this function, provided by the specific implementation of each subclass. When news arrives, the server calls messageReceived function handler class, the use of multi-state, will perform different implementations.

In HttpServerPipelineFactory.getPipeline (), when the pipeline every need, a new instance of PlayHandler, registered in the pipeline. Thus, each request will be treated as a new instance PlayHandler followed PlayHandler.messageReceiveed () method is called to process the user request. PlayHandler.messageReceived () method performs the process will be described later.

     

2, Invoker and Invocation

Play Mode Frame command (Command pattern), for scheduling in a multi-task multitasking case, prototype command mode is as follows:

Command Mode Prototype 3

In command mode, Client calls Invoker, to add command. Command represents a command, developers inherit Command and implement a specific command, submitted for execution to the Invoker. Invoker is responsible for managing these commands, execute them at the right time, and provide the results. A command mode operation of the request object and know how to perform an operation object separated. In this way, developers only need to focus on to achieve command, without the need to focus on when and how to execute the command.

    Play in the frame, and within Invoker Invocation class implements the command mode, corresponding to Command Invocation class, subclass implemented by its execute method (here expressed as run () method). FIG their class as follows:

FIG 4 Invoker FIG class

And FIG. 5 Invocation FIG class DirectInvocation

 

In Invoker, primarily invoke methods and invokeInThread method. The former uses (ScheduledThreadPoolExecutor) executor scheduling thread execution Invocation; the latter to perform tasks directly in the current thread, when the execution is not successful, re-executed after waiting for some time. Invoke method also offers another version of the overloaded function, you can then execute the current task after waiting for some time: invoke (final Invocation invocation, longmillis).

About java.util.concurrent.ScheduledThreadPoolExecutor: inherited from java.util.concurrent.ThreadPoolExecutor, for execution after a given delay command, or to execute periodically, when multiple secondary thread, or have additional flexibility requirements ThreadPoolExecutor or function, such superior Timer.

Invoker.invoke (Invocation) code is as follows, you can see how the executor is scheduled Invocation:

publicstatic Future<?> invoke(final Invocation invocation) {

       Monitor monitor = MonitorFactory.getMonitor(

"Invokerqueue size", "elmts.");

       monitor.add(executor.getQueue().size());

       invocation.waitInQueue = MonitorFactory.start("Waiting forexecution");

        returnexecutor.submit(invocation);

}
publicvoid run() {

           if (waitInQueue != null) {

               waitInQueue.stop();

           }

           try {

               preInit();

               if (init()) {

                    before();

                    execute();

                    after();

                    onSuccess();

               }

           } catch (Suspend e) {

               suspend(e);

               after();

           } catch (Throwable e) {

               onException(e);

           } finally {

               _finally();

           }

    }

Implemented in a Template Method Invocation (Template method pattern), defines the run () method step, and will be referred Subclasses implement implementation of the various steps in a way as to complete the command mode custom commands . Template Method pattern prototype is as follows:

Figure 3-6 Template Method pattern prototype

 

Play Mode In the method of the template frame specific implementation, the Invocation implements Runnable interface, the run () method, as follows:

 

 

run () method is the template method, the run () method in order to call the init (), before (), execute (), after (), onSuccess () method and the like, these methods require a subclass of Invocation implementation, in order to achieve different functions. By this design, the execution process is divided into a plurality of portions, each implemented by subclasses, but the order of execution of the predetermined portion of the parent class. Hereinafter, reference will be made PlayHandler.NettyInvocation, that is a subclass of Invocation.

 

3, Result class

Result inherited from a RuntimeException, the view layer encapsulating the rendered result (which may be an HTML file, an XML file may be a binary file, or the like), class hierarchy as follows:

FIG. 3-7 Result class inheritance structure of FIG.

 

Play FastRunTimeException is defined in a frame can be instantiated and quick throw exception class (Exception), mainly modified fillInStackTrace () method, it directly returns null, enabling rapid instantiated:

 

public Throwable fillInStackTrace() {returnnull;}

 

Result in, provided apply () method, this method requires a subclass override, implement the functions of the corresponding contents of the output.

Under Result, play provides multiple subclasses Result for the implementation of the different appropriate action, which is more common RenderTemplate class that implements the functionality of the template file for final output.

Result As will try to form an Exception and / catch to capture Result, instead of the return values, which is relatively unusual framework Play point. This does not conform to generally "exception (Exception)" view - In general, only when the program appears unforeseen circumstances, will use try / catch block to catch Exception. However, an exception is thrown as a result of rendering and capture, to simplify the code, determined by the Java execution process itself, improve the development efficiency of the developer; In the process a user request process, many places may return results directly (e.g., direct Back page 404), the frame during the processing of all the processing results and return to the same place and judge unified process, if the return value as a result, require more complicated approach to collecting and processing the results.

 

4, Template class

Template class provides a package of template files, compile and execute and implement the template file. Class inheritance relationship is as follows:

FIG. 3-8 Template class inheritance structure of FIG.

The final actually used GroovyTemplate class that represents a template file, a method (render (Map <String, Object>)) on page rendering (converting the template files to html) is.

In RenderTemplate previously discussed, a pass (Template) Template in the constructor (i.e. a RenderTemplate "owns" a Template example), and performs

 

this.content = template.render(args);

 

Call Template.render (args) will render the results stored in the content.

 

In a request processing flow, sequentially undergoes PlayHandler-> Invoker-> Invocation-> Result-> Template

Turn https://blog.csdn.net/hjxgood/article/details/53910969

Published 15 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/J_M_S_H_T/article/details/88245598