19tomcat的服务器和服务

Server服务器
Server接口表示整个Catalina Servlet容器以及其它组件。
它提供了一种优雅的机制来启动和停止整个系统。不必再单独的启动连接器和容器了。
当服务器启动的时候,它启动它内部的所有组件。然后无限期的等待关闭命令,
如果你想要关闭系统,发送一个关闭命令道指定端口即可。当服务器收到正确的关闭指令后,它停止所有组件的服务。
服务器还使用了另外一个组件,服务(service),它用来持有组件
public interface Server {


    // ------------------------------------------------------------- Properties


    /**
     * Return descriptive information about this Server implementation and
     * the corresponding version number, in the format
     * <code>&lt;description&gt;/&lt;version&gt;</code>.
     */
    public String getInfo();


    /**
     * Return the global naming resources.
     */
    public NamingResources getGlobalNamingResources();


    /**
     * Set the global naming resources.
     * 
     * @param namingResources The new global naming resources
     */
    public void setGlobalNamingResources
        (NamingResources globalNamingResources);


    /**
     * Return the port number we listen to for shutdown commands.
     */
    public int getPort();


    /**
     * Set the port number we listen to for shutdown commands.
     *
     * @param port The new port number
     */
    public void setPort(int port);


    /**
     * Return the shutdown command string we are waiting for.
     */
    public String getShutdown();


    /**
     * Set the shutdown command we are waiting for.
     *
     * @param shutdown The new shutdown command
     */
    public void setShutdown(String shutdown);


    // --------------------------------------------------------- Public Methods


    /**
     * Add a new Service to the set of defined Services.
     *
     * @param service The Service to be added
     */
    public void addService(Service service);


    /**
     * Wait until a proper shutdown command is received, then return.
     */
    public void await();


    /**
     * Return the specified Service (if it exists); otherwise return
     * <code>null</code>.
     *
     * @param name Name of the Service to be returned
     */
    public Service findService(String name);


    /**
     * Return the set of Services defined within this Server.
     */
    public Service[] findServices();


    /**
     * Remove the specified Service from the set associated from this
     * Server.
     *
     * @param service The Service to be removed
     */
    public void removeService(Service service);

    /**
     * Invoke a pre-startup initialization. This is used to allow connectors
     * to bind to restricted ports under Unix operating environments.
     *
     * @exception LifecycleException If this server was already initialized.
     */
    public void initialize()
    throws LifecycleException;
}

属性shutdown用来持有一个停止服务的指令。属性port则是服务器等待关闭命令的端口。
可以调用服务器的addService方法将服务添加到服务器。使用removeService方法将服务删除。
findServices返回所有服务器中所有的服务。Initialize方法包括在启动之前需要执行的代码。

StandardServer类
StandardServer类是服务器的标准实现
一个服务器可以有零个或多个服务,StandardServer类提供了addService、removeService、findServices方法的实现。
另外还有四个跟生命周期相关的方法:initialize 、tart 、stop以及await。
initialize方法
Initialize方法用于初始化要添加到服务器实例上的服务

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();
        }
    }

注意该方法使用了一个名为initialized的变量来避免多次启动该服务器


start方法
可以使用start方法来启动一个服务器,StandardServer的start方法的实现将会启动所有服务及其相关组件,例如连接器和容器

 public void start() throws LifecycleException {

        // Validate and update our current component state
        if (started)
            throw new LifecycleException
                (sm.getString("standardServer.start.started"));
        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);

        lifecycle.fireLifecycleEvent(START_EVENT, null);
        started = true;

        // Start our defined Services
        synchronized (services) {
            for (int i = 0; i < services.length; i++) {
                if (services[i] instanceof Lifecycle)
                    ((Lifecycle) services[i]).start();
            }
        }

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);

    }

该方法使用了一个started布尔变量来避免一个服务器被启动两次。Stop方法会重置该变量的值

stop方法

 public void stop() throws LifecycleException {

        // Validate and update our current component state
        if (!started)
            throw new LifecycleException
                (sm.getString("standardServer.stop.notStarted"));

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);

        lifecycle.fireLifecycleEvent(STOP_EVENT, null);
        started = false;

        // Stop our defined Services
        for (int i = 0; i < services.length; i++) {
            if (services[i] instanceof Lifecycle)
                ((Lifecycle) services[i]).stop();
        }

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);

    }


await方法
Await负责整个Tomcat部署的停止机制

public void await() {

        // Set up a server socket to wait on
        ServerSocket serverSocket = null;
        try {
            serverSocket =
                new ServerSocket(port, 1,
                                 InetAddress.getByName("127.0.0.1"));
        } catch (IOException e) {
            System.err.println("StandardServer.await: create[" + port
                               + "]: " + e);
            e.printStackTrace();
            System.exit(1);
        }

        // Loop waiting for a connection and a valid command
        while (true) {

            // Wait for the next connection
            Socket socket = null;
            InputStream stream = null;
            try {
                socket = serverSocket.accept();
                socket.setSoTimeout(10 * 1000);  // Ten seconds
                stream = socket.getInputStream();
            } catch (AccessControlException ace) {
                System.err.println("StandardServer.accept security exception: "
                                   + ace.getMessage());
                continue;
            } catch (IOException e) {
                System.err.println("StandardServer.await: accept: " + e);
                e.printStackTrace();
                System.exit(1);
            }

            // Read a set of characters from the socket
            StringBuffer command = new StringBuffer();
            int expected = 1024; // Cut off to avoid DoS attack
            while (expected < shutdown.length()) {
                if (random == null)
                    random = new Random(System.currentTimeMillis());
                expected += (random.nextInt() % 1024);
            }
            while (expected > 0) {
                int ch = -1;
                try {
                    ch = stream.read();
                } catch (IOException e) {
                    System.err.println("StandardServer.await: read: " + e);
                    e.printStackTrace();
                    ch = -1;
                }
                if (ch < 32)  // Control character or EOF terminates loop
                    break;
                command.append((char) ch);
                expected--;
            }

            // Close the socket now that we are done with it
            try {
                socket.close();
            } catch (IOException e) {
                ;
            }

            // Match against our command string
            boolean match = command.toString().equals(shutdown);
            if (match) {
                break;
            } else
                System.err.println("StandardServer.await: Invalid command '" +
                                   command.toString() + "' received");

        }

        // Close the server socket and return
        try {
            serverSocket.close();
        } catch (IOException e) {
            ;
        }

    }

方法await在8085端口创建一个ServerSocket对象,在while循环调用它的accept方法。
Accept方法仅仅接受8085端口的信息。它将接受到的信息跟shutdown命令进行匹配,
如果匹配的话跳出循环关闭SocketServer,如果不匹配继续while循环等待另一个命令。

服务
org.apache.catalina.Service接口用于表示服务。一个服务可以可以有一个容器和多个连接器。
可以添加多个连接器 ,并将它们跟容器相关联

public interface Service {


    // ------------------------------------------------------------- Properties


    /**
     * Return the <code>Container</code> that handles requests for all
     * <code>Connectors</code> associated with this Service.
     */
    public Container getContainer();


    /**
     * Set the <code>Container</code> that handles requests for all
     * <code>Connectors</code> associated with this Service.
     *
     * @param container The new Container
     */
    public void setContainer(Container container);


    /**
     * Return descriptive information about this Service implementation and
     * the corresponding version number, in the format
     * <code>&lt;description&gt;/&lt;version&gt;</code>.
     */
    public String getInfo();


    /**
     * Return the name of this Service.
     */
    public String getName();


    /**
     * Set the name of this Service.
     *
     * @param name The new service name
     */
    public void setName(String name);


    /**
     * Return the <code>Server</code> with which we are associated (if any).
     */
    public Server getServer();


    /**
     * Set the <code>Server</code> with which we are associated (if any).
     *
     * @param server The server that owns this Service
     */
    public void setServer(Server server);

    
    // --------------------------------------------------------- Public Methods


    /**
     * Add a new Connector to the set of defined Connectors, and associate it
     * with this Service's Container.
     *
     * @param connector The Connector to be added
     */
    public void addConnector(Connector connector);


    /**
     * Find and return the set of Connectors associated with this Service.
     */
    public Connector[] findConnectors();


    /**
     * Remove the specified Connector from the set associated from this
     * Service.  The removed Connector will also be disassociated from our
     * Container.
     *
     * @param connector The Connector to be removed
     */
    public void removeConnector(Connector connector);

    /**
     * Invoke a pre-startup initialization. This is used to allow connectors
     * to bind to restricted ports under Unix operating environments.
     *
     * @exception LifecycleException If this server was already initialized.
     */
    public void initialize()
    throws LifecycleException;

}

StandardService类

StandardService类是Service接口的标准实现。StandardService类的initialize方法初始化所有的添加到该服务的连接器

public void initialize()
    throws LifecycleException {
        if (initialized)
            throw new LifecycleException (
                sm.getString("standardService.initialize.initialized"));
        initialized = true;

        // Initialize our defined Connectors
        synchronized (connectors) {
                for (int i = 0; i < connectors.length; i++) {
                    connectors[i].initialize();
                }
        }
    }

容器和连接器
一个StandardService实例包括两种组件:一个容器和多个连接器。
多个连接器可以使得Tomcat能服务于多个协议。一个协议用处处理HTTP请求,另一个用于处理HTTPS请求。
StandardService类用container变量来持有容器实例,用connectors数组来持有所有的连接器
private Container container = null;
private Connector connectors[] = new Connector[0];

要将一个容器跟一个服务相关联,可以使用它的setContainer方法

public void setContainer(Container container) {

        Container oldContainer = this.container;
        if ((oldContainer != null) && (oldContainer instanceof Engine))
            ((Engine) oldContainer).setService(null);
        this.container = container;
        if ((this.container != null) && (this.container instanceof Engine))
            ((Engine) this.container).setService(this);
        if (started && (this.container != null) &&
            (this.container instanceof Lifecycle)) {
            try {
                ((Lifecycle) this.container).start();
            } catch (LifecycleException e) {
                ;
            }
        }
        synchronized (connectors) {
            for (int i = 0; i < connectors.length; i++)
                connectors[i].setContainer(this.container);
        }
        if (started && (oldContainer != null) &&
            (oldContainer instanceof Lifecycle)) {
            try {
                ((Lifecycle) oldContainer).stop();
            } catch (LifecycleException e) {
                ;
            }
        }

        // Report this property change to interested listeners
        support.firePropertyChange("container", oldContainer, this.container);

    }


要与服务相关联的容器传递给该每个连接器,这样来建立容器和每个连接器的关系。

要给一个服务添加连接器,可以使用addConnector方法。要删除一个连接器,可以使用removeConnector方法

 public void addConnector(Connector connector) {

        synchronized (connectors) {
            connector.setContainer(this.container);
            connector.setService(this);
            Connector results[] = new Connector[connectors.length + 1];
            System.arraycopy(connectors, 0, results, 0, connectors.length);
            results[connectors.length] = connector;
            connectors = results;

            if (initialized) {
                try {
                    connector.initialize();
                } catch (LifecycleException e) {
                    e.printStackTrace(System.err);
                }
            }

            if (started && (connector instanceof Lifecycle)) {
                try {
                    ((Lifecycle) connector).start();
                } catch (LifecycleException e) {
                    ;
                }
            }

            // Report this property change to interested listeners
            support.firePropertyChange("connector", null, connector);
        }

    }

    public void removeConnector(Connector connector) {

        synchronized (connectors) {
            int j = -1;
            for (int i = 0; i < connectors.length; i++) {
                if (connector == connectors[i]) {
                    j = i;
                    break;
                }
            }
            if (j < 0)
                return;
            if (started && (connectors[j] instanceof Lifecycle)) {
                try {
                    ((Lifecycle) connectors[j]).stop();
                } catch (LifecycleException e) {
                    ;
                }
            }
            connectors[j].setContainer(null);
            connector.setService(null);
            int k = 0;
            Connector results[] = new Connector[connectors.length - 1];
            for (int i = 0; i < connectors.length; i++) {
                if (i != j)
                    results[k++] = connectors[i];
            }
            connectors = results;

            // Report this property change to interested listeners
            support.firePropertyChange("connector", connector, null);
        }

    }

Start方法用于启动相关联的连接器和容器

public void start() throws LifecycleException {

        // Validate and update our current component state
        if (started) {
            throw new LifecycleException
                (sm.getString("standardService.start.started"));
        }

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);

        System.out.println
            (sm.getString("standardService.start.name", this.name));
        lifecycle.fireLifecycleEvent(START_EVENT, null);
        started = true;

        // Start our defined Container first
        if (container != null) {
            synchronized (container) {
                if (container instanceof Lifecycle) {
                    ((Lifecycle) container).start();
                }
            }
        }

        // Start our defined Connectors second
        synchronized (connectors) {
            for (int i = 0; i < connectors.length; i++) {
                if (connectors[i] instanceof Lifecycle)
                    ((Lifecycle) connectors[i]).start();
            }
        }

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);

    }

Stop方法关闭所有关联连接器和容器

public void stop() throws LifecycleException {

        // Validate and update our current component state
        if (!started) {
            throw new LifecycleException
                (sm.getString("standardService.stop.notStarted"));
        }

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);

        lifecycle.fireLifecycleEvent(STOP_EVENT, null);

        System.out.println
            (sm.getString("standardService.stop.name", this.name));
        started = false;

        // Stop our defined Connectors first
        synchronized (connectors) {
            for (int i = 0; i < connectors.length; i++) {
                if (connectors[i] instanceof Lifecycle)
                    ((Lifecycle) connectors[i]).stop();
            }
        }

        // Stop our defined Container second
        if (container != null) {
            synchronized (container) {
                if (container instanceof Lifecycle) {
                    ((Lifecycle) container).stop();
                }
            }
        }

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);

  }

例子

public final class Bootstrap {
	public static void main(String[] args) {
		System.setProperty("catalina.base", System.getProperty("user.dir"));
		Connector connector = new HttpConnector();
		Wrapper wrapper1 = new StandardWrapper();
		wrapper1.setName("Primitive");
		wrapper1.setServletClass("PrimitiveServlet");
		Wrapper wrapper2 = new StandardWrapper();
		wrapper2.setName("Modern"); 
		wrapper2.setServletClass("ModernServlet"); 
		Context context = new StandardContext();
		// StandardContext's start method adds a default mapper 
		context.setPath("/app1"); 
		context.setDocBase("app1"); 
		context.addChild(wrapper1); 
		context.addChild(wrapper2); 
		LifecycleListener listener = new SimpleContextConfig(); 
		((Lifecycle) context).addLifecycleListener(listener);
		Host host = new StandardHost(); 
		host.addChild(context);
		host.setName("localhost"); 
		host.setAppBase("webapps"); 
		Loader loader = new WebappLoader();
		context.setLoader(loader);
		// context.addServletMapping(pattern, name); 
		context.addServletMapping("/Primitive", "Primitive"); 
		context.addServletMapping("/Modern", "Modern"); 
		Engine engine = new StandardEngine(); 
		engine.addChild(host); 
		engine.setDefaultHost("localhost");
		Service service = new StandardService(); 
		service.setName("Stand-alone Service");
		Server server = new StandardServer(); 
		server.addService(service);
		service.addConnector(connector);
		service.setContainer(engine);
		if (server instanceof Lifecycle) {
			try { 
				server.initialize(); 
				((Lifecycle) server).start();
				server.await();
				// the program waits until the await method returns,
				// i.e. until a shutdown command is received. 
			}
			catch (LifecycleException e) {
				e.printStackTrace(System.out);
			}
		}

		if (server instanceof Lifecycle) { 
			try { 
				((Lifecycle) server).stop(); 
			} catch (LifecycleException e) { 
				e.printStackTrace(System.out);
			}
		}
	{
}

Bootstrap主方法创建了一个连接器、两个包装器、一个上下文、一个主机以及一个引擎。
然后将包装器添加到上下文,将上下文添加到主机,主机添加到引擎。它并没有将连接器和引擎相关联,
而是创建了一个一个服务对象,设置它的名字,创建一个服务器对象,并给该服务器添加服务。
然后,主方法将连接器和引擎添加到服务上。
这样讲连接器添加到服务上,连接器在服务上跟容器相关联。
然后主方法调用服务器的initialize和start方法。初始化连接器并启动它以及容器。

Stopper类
Stopper类提供了一种优雅的方式来关闭Catalina服务器。它还保证了所有生命周期组件的stop方法会被调用

public class Stopper {
	public static void main(String[] args) {
		// the following code is taken from the Stop method 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."); 
		}
	}
}

猜你喜欢

转载自501565246-qq-com.iteye.com/blog/1731313