码迷,mamicode.com
首页 > 其他好文 > 详细

《how tomcat work》 搬运工 Chapter 14 Server and Service

时间:2015-10-25 17:43:25      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

之前的章节是将container和connector联系了在一起,但是connector只能和一个端口相连接。而且,之前的application都缺少了一个开启服务,关闭服务的接口。这章节就是介绍tomcat的server和service。server和service就是方便了启动。

Server 

org.apache.catalina.Server接口代表了catalinaservlet的server。server整合了将connector和container的开启和关闭。

public interface Server {   
    /**    
        * 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);

    /**    
        * 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    
         * onnectors to bind to restricted ports under Unix operating    
         * environments.    *    
         * @exception LifecycleException If this server was already    
         * initialized.    
     */
      public void initialize() throws LifecycleException;
    }

StandardServer

standardServer实现了server的标准方法。

initiate method:将service初始化。

start method:将service启动。

stop method:将service停止。

await method:监听一个端口,如果在那个端口输入了stop命令这停止。

 

 

Service

一个service可以有一个container和多个connector。

service接口

public interface Service {  
  /**    
  * 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);

  /**  
  * 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中可以修改container,添加、移除connector。

 

 

具体的main函数

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

    //StandardService class‘s setContainer will call all its connector‘s setContainer method
    service.setContainer(engine);

    // Start the new server
    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);
      }
    }

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

 

《how tomcat work》 搬运工 Chapter 14 Server and Service

标签:

原文地址:http://www.cnblogs.com/xuyung/p/4908910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!