Jetty Source Code Analysis

Source: Internet
Author: User
Tags httpcontext socket thread class jboss

first, the overall do you know jetty, like the tomcat we are familiar with, Jetty is a free open source 100% Pure Java HTTP server and servlet container.

Jetty has the following characteristics:

Fast and efficient

。 Jetty is one of the fastest servlet servers

。 Jetty can handle thousands of concurrent connections
Compact Embedding
。 Jetty Jar is only more than 600 k
。 Dynamically embedded in the application for development of applications such as web2.0

Wide Application

。 Open source projects include Geronimo, JBoss, Jonas, etc.

。 Commercial projects with IBM Tivoli, Sonic MQ and Cisco sesm, etc.

Access to the jetty website http://jetty.mortbay.org/jetty/see the latest information

This article will be the latest stable version of the jetty Jetty5.1.5rc2 source code research, to the reader to show the design of jetty in the use of different design concepts, I hope that the vast number of developers in the design of their own system is helpful.

Jetty according to the function can be divided into four main main parts, Httpserver, Httpcontext,httphandler,httplistener, see the following class diagram:

< figure 1-1>

Second, httpserver and configuration
For the first contact jetty will be confused about the above figure, in fact, in Jetty Httpserver is a Server Core control class, we can see that the other component classes are extended by this class, The role of Httpserver is to set up a bridge between a series of listener classes and processor classes, effectively controlling the delivery of messages within the system, as shown below:
< figure 1-2 >
The httpserver duty is to accept request from HttpListener, httpserver by matching the request's host (host) or path (path) and distributing it to the corresponding HttpContext ( Can be understood as a Web application).
For example, let's say that we are now going to create a Web service that provides a static page, the content of which is under C:\root \, which can be configured by this httpserver:
Httpserver Server = new Httpserver (); Create a new Httpserver
SocketListener listener = new SocketListener (); Create a new listener
Listener.setport (8080);//Set the listening port to 8080
Server.addlistener (listener);//Register the Listener class with the server
HttpContext context = new HttpContext (); Create a new HttpContext
Context.setcontextpath ("/app/*"); Set Access Path
Context.setresourcebase ("c:/root/"); To set a static resource path
Context.addhandler (New Resourcehandler ()); Add a static resource processor for this HttpContext
Server.addcontext (context); Register this HttpContext in the server
Server.start ();//finally start the server
When we want to build a Web service that provides dynamic pages, suppose our own Web application is placed under WebApps in the Jetty directory and the package file is named Myapp.war, which can be configured Httpserver:
Server server = new server (); Create a new Httpserver
SocketListener listener = new SocketListener ();//Create a new listener
Listener.setport (8080); Set the listening port to 8080
Server.addlistener (listener); Registering the Listener class with the server
Server.addwebapplication ("MyApp", "./webapps/myapp/"); Register this web app with this server
Server.start (); Finally start the server
Just a few lines of code can create a Web server and launch it, which is a bit like the plug-and-play concept in our windows, what we need to add, these classes with Httpserver as the core together, you can complete the powerful function.
third, Jetty Server
1. Above we discussed the start of httpserver, readers must still have such doubts, the entire jetty server is how to start.
First we can see in figure 1-1 that there is a server class in the lower left corner, this class actually inherits the Httpserver, when starting the jetty server, specifically, under the jetty root directory under the command line such as input Java-jar Start.jar etc/ Demo.xml, note that there is a configuration file Demo.xml as a running parameter, this parameter can also be a different configuration file, but also a number of XML configuration files, in fact, this configuration file is similar to the struts when we use struts- A. config file that writes the components needed to run the server, such as the component classes required for Httpserver configuration in the previous section, can be written in this configuration file.
2. How do we deploy our own web Application,jetty in the WebApps directory of jetty to run our own Web application?
First, when we start Jetty server as described above, we call the main method inside the server class, which first constructs an instance of the server class (in fact, it constructs a httpserver). The Xmlconfiguration class object is constructed during the instance creation to read the parameter configuration file, which is then configured by the Xmlconfiguration object generated by the configuration file. The configuration process is actually using the Java reflection mechanism to call the server's method and pass in the parameters written in the configuration file to add Httplistener,httpcontext,httphandler,web application to this server ( Corresponding to our own deployed web App).
Adding our own WEB application process will read our familiar/web-inf/web.xml to create an instance of Webapplicationcontext (which inherits HttpContext). It also creates the servlethandler of the Webapplicationcontext itself (implements the HttpHandler interface), noting that Servlethandler contains a set of Servletholder pointing to the actual servlet , for example, we have configured two filter and one servlet in the Web. xml file, there will be three servletholder, when actually processing the request Servelethandler will call these three servletholder incoming request,response processing (actually finally to the two filter and servlet processing), so that we do our own The web app is attached to the server and can accept the client request.
Iv. Operating principle (timing diagram)

< figure 1-7 >

The diagram above shows the process of requesting a request, first httplistener the client to create a Httpconnection instance (encapsulating the connection details, such as the input and output streams obtained from the socket connection), The Httpconnection object build process creates jetty internally customized HttpRequest and HttpResponse objects, and HttpListener calls the Httpconnection method of the handle instance. The Httpconnection instance reads the information by invoking the read () method of the HttpRequest object, invokes the service method of Httpserver to httprequest,httpresponse the parameter to Httpserver, Httpserver and HttpRequest and HttpResponse distribution to the corresponding Httpcotext,httpcontext finally HttpRequest and HttpResponse to their own HttpHandler processing, Here Httprequest,httpresponse is again encapsulated as Servlethttprequest and servlethttpresponse, in fact, these two classes realize the httpservletrequest we are familiar with and HttpServletResponse interface.

v. Advanced Components

1.HttpHandler:

The implementation class of this interface is used to handle HttpContext distributed reqeust, different implementation classes have different processing functions, here are a few common HttpHandler implementation classes:
Reourcehandler: For handling static content, such as files with an. html extension

Securityhandler: Provides basic security validation

ForwardHandler: Forwarding a request to another URL

Servlethandler: For handling request to a specific servlet class
2. When you look at Figure 1-2 you will notice that Httpserver and Httplistener,httpserver have a one-to-many relationship with Httpcontext,httpcontext and HttpHandler, Here's a look at how the relationship between them is configured through a program.

HttpListener & Httpserver:

HttpListener is the interface of all listener classes, as shown in Figure SocketListener (based on the traditional socket technology) to implement the interface, jetty there are other implementations of the interface class, such as Socketchannellistener ( Based on NIO Technology), HttpListener responsibility is primarily to listen for the corresponding port from the client request and establish the connection (as shown in Figure 1-1, with the Httpconnection Encapsulation Connection details), after the server is started. The listener can open multiple ports on the same IP to listen for the same httpserver, so HttpListener and Httpserver are many-to-one relationships, as shown below:

< figure 1-3 >

Configuration code:
Httpserver Server = new Httpserver ();
Httplistenrer listener1 = new Socketchannelistener ();
Listener1.setport (8080);
Httplistenrer listener1 = new SocketListener ();
Listener1.setport (8443);
Server.addlistener (Listener1);
Server.addlistener (LISTENER2);

HttpContext & HttpHandler:

The HttpContext corresponds to the URL of the client request or to a virtual machine, and its subclasses contain several HttpHandler, and when the request is accepted, HttpContext will sequentially (in a predetermined order) Submit the request to these HttpHandler until the request has been marked for processing, it is important to note that this request may be processed by multiple HttpHandler, But only one httphandler can indicate that the request has been processed.

A typical HttpContext has httphandler for secure handling, static resource handling, and servlet classes, as shown in the following diagram:
< figure 1-4>
Configuration code:
HttpContext context = new HttpContext ();
Context.setcontextpath ("/myapp/*");
HttpHandler Securithandler = new Securityhandler ();
HttpHandler Resourcehandler = new Resourcehandler ();
HttpHandler Servlethandler = new Servlethandler ();
Context.addhandler (Securithandler);
Context.addhandler (Resourcehandler);
Context.addhandler (Servlethandler);
Httpserver & HttpContext:
The general HTTP Server software can handle multiple Web application at the same time, the same httpserver can contain multiple HttpContext, the following figure can be mapped through the same port listener class to map multiple HttpContext:

< figure 1-5 >

Configuration code:
Httpserver Server = new Httpserver ();
HttpContext context1 = new HttpContext ();
Context1.setcontextpath ("/app1/*");
HttpContext context2 = new HttpContext ();
Context2.setcontextpath ("/app2/*");
Server.addcontext (CONTEXT1);
Httpserver & Httplister & HttpContext:

In addition, Jetty provides good support for servers with multiple NICs (multiple IP addresses, different hostnames), and each HttpContext has its own httpserver:

< figure 1-6 >

Configuration code:
Httpserver server1 = new Httpserver ();
SocketListener listener1 = new SocketListener ();

Listener1.sethost ("www.app1.com");//orlistener1.sethost ("www.app2.com")

Listener2.setport (80);

HttpContext context1 = new HttpContext ();

Context1.setcontextpath ("/");

Server1.addlistener (Listener1);

Server1.addcontext (CONTEXT1);

3.Jetty Support for high concurrency
< figure 1-8>
If the multi-user request service will involve multithreading management, as shown in Figure 1-8,jetty mainly by ThreadPool responsible for managing multi-threading, note that Pool.pondlife is an internal interface of the pool, Threadpool.poolthread is an internal thread class of ThreadPool, We see a clustered relationship between the pool.pondlife and the pool, in fact the pool object is stored in a Threadpool.poolthread thread object, and when a new user connects to the server, ThreadPool takes an idle thread from the pool for the current user Connection service.
Vi. Summary

This paper briefly introduces the whole architecture of jetty and the main component classes and server startup execution process, in fact, jetty is often used as an embedded Web server to use, some common server software, such as Apache Cocoon, JBoss, Jonas will use jetty as the Web solution, in addition, because jetty performance and stability is better than similar HTTP server reasons, Jetty has been very popular abroad, in view of this, the author can predict in the near future jetty will also be popular in the country.

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.