Jetty 9 source code analysis of ctor and server classes #1

Source: Internet
Author: User

The source code in this article is based on jetty9. It mainly analyzes some details of jetty's ctor and server classes during the jetty startup process.
Jetty9 restructured the previous ctor system. The structure is different from those of 6 and 7, and some of the original bio classes have been abandoned.


First look at the server Constructor

public Server(@Name("port")int port){this((ThreadPool)null);ServerConnector connector=new ServerConnector(this);connector.setPort(port);setConnectors(new Connector[]{connector});}

 

Pass itself into the serverconnector structure, set the port of the connector, and pass the serverconnector into its own
Connector array to retain reference.

The copyors array variable of the server uses copyonwritearraylist to ensure the security of possible thread operations.

private final List<Connector> _connectors = new CopyOnWriteArrayList<>();

 

The server startup class is defined in Jetty. XML, and the thread pool and handler are set.

<Configure id="Server" class="org.eclipse.jetty.server.Server">

 

Threadpool is shared and used by ctor and dispatch classes.

Server inherits from handlerwrapper. handlerwrapper proxy handle to the decorated class, that is, the actual handler class, and implements the decorator mode.
The inheritance relationship is as follows:


Abstracthandler
|-Abstracthandlercontainer
|-Handlerwrapper
|-Server

The start method of the server is dostart (). This method first calls the upper-layer dostart () method, and then calls the START () method of the connector class in its own connector array.

try{super.doStart();}for (Connector _connector : _connectors){try{_connector.start();}catch (Throwable e){mex.add(e);}}

 

Serverconnector is the main connector implementation in jetty9 and is responsible for access processing. The main operations include abstract connections for access connections in Java,
Connection is generated by the factory class set in connector. If it is not set, the default factory class is httpconnectionfactory.
Connector also interacts with another abstract selector.

Class Diagram describing the inheritance relationship of serverconnector:


The inheritance level of serverconnector is as follows:

Let's take a look at some specific code:


Abstractlifecycle start ()-> dostart (); The dostart () method is reserved for the subclass to override.
|-Containerlifecycle dostart ()
|-Abstractconnector
|-Abstractnetworkconnector dostart ()-> open (); an open method is set for the subclass to override.
|-Serverconnectoropen (); implements the Open Method to open the serversocketchannel


Start () in abstractlifecycle:

try{if (_state == __STARTED || _state == __STARTING)return;setStarting();doStart();setStarted();}

 

You can see that this method is used as a template method, and the implementation of dostart () is left to the subclass to rewrite. Its own dostart () is an empty method, there are many classes related to the lifecycle of jetty.

The content of the open method hides some less important details:

if (serverChannel == null){serverChannel = ServerSocketChannel.open();InetSocketAddress bindAddress = getHost() == null ? new InetSocketAddress(getPort()) : new InetSocketAddress(getHost(), getPort());serverChannel.socket().bind(bindAddress, getAcceptQueueSize());serverChannel.socket().setReuseAddress(getReuseAddress());_localPort = serverChannel.socket().getLocalPort();if (_localPort <= 0)throw new IOException("Server channel not bound");addBean(serverChannel);}serverChannel.configureBlocking(true);addBean(serverChannel);_acceptChannel = serverChannel;

 

Serversocketchannel uses the blocking mode,

Return to dostart of abstractnetworkconnector:

protected void doStart() throws Exception{open();super.doStart();}

 

The method continues to call the dostart () of the parent class, then let's look at the dostart () of the parent class abstractconnector ():

protected void doStart() throws Exception{_defaultConnectionFactory = getConnectionFactory(_defaultProtocol);super.doStart();_stopping=new CountDownLatch(_acceptors.length);for (int i = 0; i < _acceptors.length; i++)getExecutor().execute(new Acceptor(i));}

 

This method sets the default connectionfactory. Another important concept is the acceptor class.
The acceptor is a private internal class of abstractconnector. The countdownlatch used here is used for dostop. In the stop operation, it calculates whether the countdownlatch is null,
If it is not empty, countdownlatch. Await will be called to wait, and other tasks of stop will continue until the acceptor thread is stopped.
The number of acceptor is initialized in the constructor of this class. The following is a snippet:

if (acceptors<=0)acceptors=Math.max(1,(Runtime.getRuntime().availableProcessors()) / 2);

 

The primary calculation method is to calculate at least one acceptor, and whether to calculate the number of Acceptor Based on the number of CPUs and the number of CPU cores/2.

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.