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

之前的章节是将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);
      }
    }
  }
}
时间: 2024-08-01 20:43:16

《how tomcat work》 搬运工 Chapter 14 Server and Service的相关文章

解决eclipse配置Tomcat时找不到server选项(Mac通用)

集成Eclipse和Tomcat时找不到server选项: 按照网上的步骤如下: 在Eclipse中,窗口(window)——首选项(preferences)——服务器(Server)——运行时环境(Runtime Environments) ——添加(Add),添加Tomcat服务器.对应安装的Tomcat版本选择Apache Tomcat v6.0.下一步通过“浏览(Brower)”按钮选择之前Tomcat的安装目录,指定后点击“完成”完成配置. 问题在于我的Eclipse为新版本eclip

解决eclipse配置Tomcat时找不到server选项

集成Eclipse和Tomcat时找不到server选项: 按照网上的步骤如下: 在Eclipse中,窗口(window)--首选项(preferences)--服务器(Server)--运行时环境(Runtime Environments) --添加(Add),添加Tomcat服务器.对应安装的Tomcat版本选择Apache Tomcat v6.0.下一步通过"浏览(Brower)"按钮选择之前Tomcat的安装目录,指定后点击"完成"完成配置. 问题在于我的E

零元学Expression Blend 4 - Chapter 14 用实例了解布局容器系列-「Pathlistbox」II

原文:零元学Expression Blend 4 - Chapter 14 用实例了解布局容器系列-「Pathlistbox」II 本章将延续上一章的范例,步骤解析. 本章将延续上一章的范例,步骤解析. 若是对Pathlistbox基本属性还不认识的朋友,请返回上一章,小猴子良心建议:先求精.再求广! 01 开启一个新专案後,在主要工作区放入一个Ellipse,并调整到适当的位置 接着,建立我们的文字「I Love Blend 4」 这边要注意的一点: 若要做出范例的排列方式,每个字元必独立在自

tomcat日志警告WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property &#39;debug&#39; to &#39;0&#39; did not find a matching property.

日志中有警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to '0' did not find a matching property. 跟踪后发现是连接池的配置问题: <Context path="/n" docBase="E:/xxx/war" debug="0" reloadable="true"

安装VisualSVN Server 报&quot; Service &#39;VisualSVN Server&#39; failed to start. &quot; 服务启动失败

安装VisualSVN Server 报"Service 'VisualSVN Server' failed to start. Please check VisualSVN Server log in Event Viewer for more details"错误.原因是启动"VisualSVN Server"失败 2 咱们先来看一下这个服务在哪,计算机-右键-管理或者系统服务-在服务里面可以看到一个"VisualSVN Server"项,状

SQL Server Reporting Service(SSRS) 第二篇 SSRS数据分组Parent Group

SQL Server Reporting Service(SSRS) 第一篇 我的第一个SSRS例子默认使用Table进行简单的数据显示,有时为了进行更加直观的数据显示,我们需要按照某个字段对列表进行分组.为了进行更加明确的说明,我特地新建了一个表(已经填充相应数据)和一个Report(已可以进行数据的展示),Report的显示效果如下所示,下面我将按照Year和Region进行分组: 第一步:准备 先从当前的设计环境中移除ID,Year和Country两列,如下所示: 第二步:添加按Count

SQL Server Reporting Service(SSRS) 学习初步

很早就知道SQL SERVER自带的报表工具SSRS,但一直没有用过,最近终于需要在工作中一展身手了,于是我特地按照自己的理解做了以下总结: 1. 安装软件结构 SSRS全称SQL Server Reporting Service,对于服务端,作为SQLServer的一个组件,我们在安装SQLServer可以选择安装Reporting Service: 对于客户端,因为我使用的是VS2015,所以需要安装SSDT(SQL Server Data Tools),因为其已经囊括了BI(Busines

初识SQL Server Integration Service

SSIS(SQL Server Integration Service)是Microsoft 从SQL Server2005 以后发布的,现在一直跟随每个SQL server版本.它是Microsoft BI 解决方案的一大利器,我们一般认为SSIS就是ETL(Extract Transform Load)工具,一般用来导入数据到数据库.SSIS比普通的ETL更进一步,它是可视化的,用Visual Studio来开发,包文件(*.dtsx)采用的是XML格式. SSIS提供控制流和数据流.控制流

Sql Server Report Service 的部署问题(Reporting Service 2014為什麼不需要IIS就可以運行)

http://www.cnblogs.com/syfblog/p/4651621.html Sql Server Report Service 的部署问题 近期在研究SSRS部署问题,因为以前也用到过SSRS报表,但当时开发的报表是有专 门的集成系统的,不需要我自己去部署,所以对这一块的部署也不熟悉,我记得当时我是直接开发出一个SSRS 报表,然后会通过自动上传的方式上传到微软Dynamic CRM系统中,它带有自带的集成部署.而现如今,看来又得重新回去恶补一下部署的信息了.经过无数的错误的再错