How Tomcat works reading notes 14 server components and service components

Source: Internet
Author: User

There are some problems with the previous project, such as
1 only one connector can handle HTTP requests, and another connector cannot be added to handle HTTPS.
2 The closure of the container can only be rough closing the bootstrap.

An instance of the server component Org.apache.catalina.Server interface is used to represent the entire servlet engine for Catalina.
We use the server because it uses an elegant way to start/shut down the entire system.
Here's how the start and stop mechanisms work. When the server starts, it starts all the components inside it. Then wait indefinitely to close the command, if you want to shut down the system, send a close command path to the specified port. When the server receives the correct shutdown instruction, it stops all Component Services.
The server also uses another component, the service component, to hold components, such as containers or a plurality of connectors. The serviced components are described in the service section of this chapter. (Note that there are server components and service components, and these two are different.) )
The attribute shutdown in the server interface is used to hold a stop service instruction. Property port is the port on which the server waits to close a command. You can call the server's AddService method to add the service to the server. Use the RemoveService method to remove the service. Findservices returns all services on all servers.

The Standardserver class Standardserver class is the standard implementation of the server interface. Here, we mainly introduce its four methods. Initialize (), start (), Stop (), await ();
Let's say one by one.
Initialize method
    public void Initialize () throws Lifecycleexception {        if (initialized)            throw new Lifecycleexception (                Sm.getstring ("standardServer.initialize.initialized"));        initialized = true;        Initialize our defined Services for        (int i = 0; i < services.length; i++) {            services[i].initialize ();        }    }


We can see here that a initialized is used to identify whether the server has been initialized and to initialize the serviced components it contains.
Also in stop, initialized does not change, so if the server initializes, then shuts down, and so on, it throws an exception when it is initialized again.
Start method
public void Start () throws Lifecycleexception {        //Validate and update we component state        if (started) 
   throw new Lifecycleexception                (sm.getstring ("standardServer.start.started"));        Notify our interested lifecyclelisteners ...        started = true;        Start our defined Services        synchronized (services) {for            (int i = 0; i < services.length; i++) {                if (SE Rvices[i] instanceof Lifecycle)                    ((Lifecycle) services[i]). Start ();            }        }        Notify our interested lifecyclelisteners ...    }
Like initializing a dry thing, looping through the service components it is associated with. In addition, in the Stop method, started is set to false.

Stop method
public void Stop () throws Lifecycleexception {        //Validate and update we component state        if (!started) 
   throw new Lifecycleexception                (sm.getstring ("standardServer.stop.notStarted"));        Notify our interested lifecyclelisteners ...        started = false;        Stop our defined Services for        (int i = 0; i < services.length; i++) {            if (Services[i] instanceof Lifecycle) c10/> ((Lifecycle) services[i]). Stop ();        }        Notify our interested lifecyclelisteners ...    }
Close all service components.

Await method no longer post code, await to do is listen to 8005 port, if the user to 8005 port to "SHUTDOWN" on the shutdown ServerSocket, and then the await method is done.

Now say the Initialize method, the Start method, the await method, the stop four are called in the bootstrap.
When the await method finishes executing, the server's Stop method is called.

The Service interface Org.apache.catalina.Service interface is used to represent services. A service can have one container and multiple connectors. You can add multiple connectors and associate them with the container.

The Standardservice class Standardservice is a standard implementation of the service interface.
Containers and connectors A Standardservice instance consists of two components: one container and multiple connectors. Multiple connectors can enable TOMCAT to serve multiple protocols. One protocol is used to handle HTTP requests and the other to handle HTTPS requests.
The Standardservice class holds container instances with container variables, and holds all connectors with an connectors array
Private Container Container = Null;private Connector connectors[] = new Connector[0];
To associate a container with a service, you can use its Setcontainer method,
In Setcontainer, the container is passed to each connector.
        Synchronized (connectors) {for            (int i = 0; i < connectors.length; i++)                Connectors[i].setcontainer ( This.container);        }
To add connectors to a service, you can use the AddConnector method. They are also initialized when you add connectors.
To remove a connector, you can use the Removeconnector method.
The life cycle-related approach consists mainly of three methods, Initialize,start,stop.
The Initialize function is called the Initialize method of the connector.
The start function is to start a container and several connectors.
The Stop function is to close a container and several connectors.
These three methods of the serviced component are called in the corresponding method in the server component, and the three methods of the server component are called in the bootstrap.

The application here is mainly two classes, one is the Bootstrap launcher, the other is the stopper class used to stop it.

Bootstrap class
public static void Main (String args[]) {...    Service service = new Standardservice ();    Service.setname ("stand-alone Service");    Server server = new Standardserver ();      Server.addservice (service); Add the service component to the server component Service.addconnector (connector); Connect the connector to the service component//standardservice class ' s setcontainer would call all its connector ' s Setcontainer method service.setc    Ontainer (engine); The container is not directly associated with the connector, but is associated with the service component//Start The new server if (server instanceof Lifecycle) {try {Server.initia         Lize ();                                 Call the Initialize,start,await method ((Lifecycle) server). Start ();//This invokes the start of the service component to invoke the start of the connector and the container.              The connector waits for a message on 8080 .... server.await (); Unless the shutdown message is received on port 8005, the program waits until the await method returns,//i.e. until a shutdo      WN command is received.      } catch (Lifecycleexception e) {e.printstacktrace (System.out);   }}//Shut down the server if (server instanceof Lifecycle) {try {((Lifecycle) server). Stop ();      SHUTDOWM message received, Shutdown server component} catch (Lifecycleexception e) {e.printstacktrace (System.out); }    }  }

Stopper class
public class Stopper {public  static void Main (string[] args) {    //The following code was taken from the Stop Metho D of    //the Org.apache.catalina.startup.Catalina class    int port = 8005;    try {socket      socket = new Socket ("127.0.0.1", port);      OutputStream stream = Socket.getoutputstream ();      String shutdown = "shutdown";      for (int i = 0; i < shutdown.length (); i++)        Stream.Write (Shutdown.charat (i));      Stream.flush ();      Stream.Close ();      Socket.close ();      SYSTEM.OUT.PRINTLN ("The server was successfully shut down.");    catch (IOException e) {      System.out.println ("Error. The server has not been started. ");}}  


How Tomcat works reading notes 14 server components and service components

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.