How the Jetty Works

Source: Internet
Author: User

Jetty Start-up process

The Jetty portal is the server class, and the server class is started, which means that Jetty can provide you with services. What services it can provide depends on when the Server class starts and calls the Start method of other components. From the Jetty configuration file we can see that the process of configuring Jetty is the process of configuring those classes to the Server. The following is the start sequence diagram for Jetty:

Figure 4. Jetty Start-up process


Because all the components in Jetty inherit lifecycle, the server's Start method call invokes all components that are already registered with the server, and the order in which the server starts the other components is to first start the Handler set to server, usually this H Andler will have a lot of sub-Handler, these Handler will form a Handler chain. The Server starts all Handler on the chain in turn. The Mbean that is registered on the Server is then started, and the Mbean is also working together, finally starting Connector, opening the port, accepting the client request, and starting the logic very simple.

Back to top of page

Accept Request

Jetty as a standalone servlet engine can provide Web services independently, but it can also be integrated with other Web application servers, so it can provide work based on two protocols, one HTTP and one AJP protocol. If you integrate Jetty into Jboss or Apache, you can make Jetty work based on AJP mode. The following describes how jetty works based on these two protocols, and how they establish connections and accept requests.

Work based on HTTP protocol

If there is no other Web server on the front end, then jetty should work based on the HTTP protocol. That is, when Jetty receives a request, it must parse the request and encapsulate the returned data in accordance with the HTTP protocol. So how does jetty accept a connection and how does it handle the connection?

We set Jetty's connector implementation class for Org.eclipse.jetty.server.bi.SocketConnector to let Jetty work in bio mode, Jetty will create a bio-based work environment at startup, which will create Ht The Tpconnection class is used to parse and encapsulate HTTP1.1 protocol, Connectorendpoint class is processing the connection request with BIO, ServerSocket is to establish socket connection to accept and transfer data, Executor Is the thread pool that handles the connection, and it is responsible for processing each request queue task. The Acceptorthread is a listening connection request, one with a socket connection, which will enter the following processing flow.

When the socket is actually executed, httpconnection will be called, which defines how the request is passed to the servlet container, and how the request is eventually routed to the destination servlet, which can be referred to in the article "Servlet Working principle parsing".

Is jetty start to create a time series diagram to establish the connection:

Figure 5. Establish a timing diagram of the connection


Jetty Creating an accepted connection environment requires three steps:

    1. Create a queue thread pool to handle each task created by the connection, the thread pool can be specified by the user, this and Tomcat is similar.
    1. Create ServerSocket, which prepares a socket request for the client and some helper classes that the client uses to wrap the socket .
    2. Create one or more listener threads to listen for connections to the access port.

Jetty's logic is simpler than Tomcat creates a connected environment, with fewer classes involved and less code to execute.

When the connected environment is ready, the HTTP request can be accepted, and when Acceptor accepts the socket connection, it goes to the indicated process execution:

Figure 6. Working with connection timing diagrams


The Accetptor thread will create a connectorendpoint for this request. Httpconnection is used to indicate that this connection is a connection to an HTTP protocol that creates a httpparse class to resolve the HTTP protocol and creates a Request and response object that conforms to the HTTP protocol. The next step is to hand this thread to the queue thread pool for execution.


Reference: http://www.ibm.com/developerworks/cn/java/j-lo-jetty/

How the Jetty Works

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.