Chapter 14 offers the server and service components. A server provides an elegant start and stop mechanism for the whole servlet container, a service serves as a holder for a container and one or more connectors. The application accompanying this chapter shows how to use a server and a service.
前面的实验中,一个 container 只能和一个 connector 关联,并且启动和停止时通过多条指令完成的,Server + Service 可以帮助你更优雅的管理这些服务。
Server & Implementation
Server 表示的是 Catalina servlet container 以及所属的所有子 component。它提供了一种优雅的方式管理所有服务的开启和停止。
官方定义如下:A Server element represents the entire Catalina servlet container.
/** * Invoke a pre-startup initialization. This is used to allow connectors * to bind to restricted ports under Unix operating environments. */ publicvoidinitialize() throws LifecycleException { if (initialized) thrownew LifecycleException ( sm.getString("standardServer.initialize.initialized")); initialized = true;
// Initialize our defined Services for (int i = 0; i < services.length; i++) { services[i].initialize(); } }
类似的还有 start, stop 方法,处理的逻辑都是类似的,先发送对应的 event,然后 for 循环调用 service 对应的方法
Bootstrap 实现和前面基本一致,最大的区别是在 Engine 声明之后,声明了这节介绍的 Server 和 Service 作为管理 Engine 的容器
1 2 3 4 5
Service service = new StandardService(); service.setName("Stand-alone Service"); Server server = new StandardServer(); server.addService(service); service.addConnector(connector);