Jbossweb/tomcat initializing the connector and processing the HTTP request process

Source: Internet
Author: User
Tags jboss

Overview

Jbossweb is the Web container in JBoss, which is the encapsulation of Tomcat, this article takes an HTTP connector as an example, simply describes the Jbossweb/tomcat initializing the connector and processing the HTTP request process. The summary of this article:

    • Connector Initialization START process
    • How to understand Max-connections
    • Jioendpoint Processing Socket Requests
Connector Initialization START process

As shown in the following:


    1. Webconnectorservice refers to ' org.jboss.as.web.WebConnectorService '
    2. Connector refers to ' org.apache.catalina.connector.Connector '
    3. Http11protocol refers to ' org.apache.coyote.http11.Http11Protocol '
    4. Jioendpoint refers to ' org.apache.tomcat.util.net.JIoEndpoint '
Connector Init ()

Connector can be an HTTP Connector, or it can be protocolhandler and Adapter properties in AJP Connector,connector, Connector initialization mainly includes: Initialize Adapter, and Initialize the Adapter setting to Protocolhandler, and then assign the Protocolhandler initialization method, as shown in the following code snippet:

        Initializa Adapter        adapter = new Coyoteadapter (this);        Protocolhandler.setadapter (adapter);        Introspectionutils.setproperty (Protocolhandler, "Jkhome", System.getproperty ("Catalina.base"));        try {            protocolhandler.init ();        } catch (Exception e) {            throw new lifecycleexception ( Messages.protocolhandlerinitfailed (e));        }

Http11protocol Init ()

Http11protocol it has a http11connectionhandler Handler, the Handler implements the ' Org.apache.tomcat.util.net.JIoEndpoint.Handler ' interface, Http11protocol also has a Jioendpoint property, which is used to handle incoming TCP connections, as shown in the following code snippet:

    protected Http11connectionhandler CHandler = new Http11connectionhandler (this);    Protected Jioendpoint endpoint = new Jioendpoint ();

Http11protocol initialization consists mainly of:

    • Set the name to Jioendpoint, the default setting is http-/127.0.0.1:8080
    • Set the socket handler for Jioendpoint, set the handler to Http11connectionhandler, the handler of handling of accepted sockets
    • The initialization method of dispatching Jioendpoint
Jioendpoint Init ()

Jioendpoint, we have said before about this kind of function, the most direct summary of this kind is as follows:

/** * Handle incoming TCP connections. * * This class implement a simple server Model:one listener thread accepts on a socket and * Creates a new worker thread For each incoming connection. * * More advanced endpoints'll reuse the threads, use queues, etc. * * @author James Duncan Davidson * @author Jason Hun ter * @author James Todd * @author Costin Manolache * @author Gal shachor * @author Yoav Shapira * @author Remy Maucherat */public class Jioendpoint {

Jioendpoint initialization consists of:

    • Initializes acceptor thread count, the default initial setting of Acceptor thread count is 1
    • Initializes the serversocketfactory and creates the ServerSocket through the initialized serversocketfactory
Connector Start ()

Connector Start method verifies the current state of the update and Http11protocol the starting method of the dispatching

Http11protocol Start ()

The starting method of direct dispatching Jioendpoint in Http11protocol's starting method.

Jioendpoint Start ()

Jioendpoint's starting methods mainly include:

If the external Executor-based thread pool is empty, the internal workers stack is initialized, the stack holds the worker, and the initialized stack size is defined as follows:

protected int maxthreads = (Org.apache.tomcat.util.Constants.LOW_MEMORY)? : ((Constants.max_threads = =-1)? * Runtime.getruntime (). Availableprocessors (): constants.max_threads);

Above

    • If the system parameter-dorg.apache.tomcat.util.low_memory=true, the initialized stack size is 64
    • If the maximum value is specified by the system parameter-dorg.apache.tomcat.util.net.max_threads=xxx, the initialized stack size is the maximum value specified by the system parameter
    • If Max_threads is not specified by the system parameter, the initialized stack size is runtime.getruntime (). Availableprocessors ()
Start the Poller thread, and the default thread name is Http-/127.0.0.1:8080-poller.

Start acceptor thread, the default thread name is http-/127.0.0.1:8080-acceptor-0

The following code snippet shows the above logic

public void Start () throws Exception {//Initialize socket if not done before if (!initialized) {        Init ();            } if (!running) {running = true;            paused = false;            Create Worker Collection if (executor = = null) {workers = new workerstack (maxthreads);            }//Start event Poller Thread Eventpoller = new Poller ();            Eventpoller.init ();            Thread pollerthread = new Thread (Eventpoller, getName () + "-poller");            Pollerthread.setpriority (threadpriority);            Pollerthread.setdaemon (TRUE);            Pollerthread.start (); Start acceptor threads for (int i = 0; i < Acceptorthreadcount; i++) {Thread Acceptorthre                AD = new Thread (New acceptor (), getName () + "-acceptor-" + i);                Acceptorthread.setpriority (threadpriority);             Acceptorthread.setdaemon (daemon);   Acceptorthread.start (); }        }    }

How to understand Max-connections

The definition of max-connections in the JBoss Web is as follows

<subsystem xmlns= "urn:jboss:domain:web:1.4" default-virtual-server= "Default-host" native= "false" >            < Connector name= "http" protocol= "http/1.1" scheme= "http" socket-binding= "http" max-connections= "/>"

As above, if we define the Max-connections,webconnectorservice start method there will be the following logic:

    • Set Jioendpoint in Pollersize, the following code:
    protected int pollersize = (Org.apache.tomcat.util.Constants.LOW_MEMORY)? (+ *);    public void setpollersize (int pollersize) {this.pollersize = pollersize;}    public int getpollersize () {return pollersize;}

As above, the default pollersize if there is no-dorg.apache.tomcat.util.low_memory=true setting, it has a value of 32 * 1024.

    • Set Jioendpoint in MaxThreads, the following code snippet:
    protected int maxthreads = (Org.apache.tomcat.util.Constants.LOW_MEMORY)? : ((Constants.max_threads = =-1)? * Runtime.getruntime (). Availableprocessors (): constants.max_threads);    public void setmaxthreads (int maxthreads) {this.maxthreads = MaxThreads;}    public int getmaxthreads () {return maxthreads;}

Note that the maxthreads is used to initialize the size of the internal workers stack.

Jioendpoint Processing Socket Requests

Jioendpoint processing the Socket request as shown in


As above, first the acceptor thread (usually named http-/127.0.0.1:8080-acceptor-0) blocks waiting for the Socket connection, as follows:

"http-/127.0.0.1:8080-acceptor-0" daemon prio=10 tid=0x49ed5800 nid=0xbe9 runnable [0x49789000]   Java.lang.Thread.State:RUNNABLE at        java.net.PlainSocketImpl.socketAccept (Native Method)        at Java.net.AbstractPlainSocketImpl.accept (abstractplainsocketimpl.java:398) at        Java.net.ServerSocket.implAccept (serversocket.java:522) at        java.net.ServerSocket.accept (Serversocket.java : 490) at        Org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket (Defaultserversocketfactory.java : At        Org.apache.tomcat.util.net.jioendpoint$acceptor.run (jioendpoint.java:309) at        Java.lang.Thread.run (thread.java:722)   Locked ownable synchronizers:        -None

Coming soon

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.