Tomcat学习笔记(十三)
服务器组件和服务组件
服务器组件
org.apache.catalina.Server接口的实例表示Catalina的整个servlet引擎,包含了所有的组件。使用一种优雅的方法来启动/关闭整个系统,不需要再对连接器和容器分别启动/关闭。
关闭/启动的工作原理。当启动服务器组件时,他会启动其中所有的组件,然后它就一直等待关闭命令,如果要关闭系统,可以向指定端口发送一条关闭命令。服务器收到关闭命令后,就会关闭其中所有的组件。
看一下Server接口的几个方法。
public interface Server extends Lifecycle {
public int getPort();//返回监听关闭的端口
public void setPort(int port);//设置关闭端口
public Catalina getCatalina();//返回catalina组件
public void addService(Service service);//添加服务
public Service findService(String name);//查询服务
public Service[] findServices();//查询所有服务
public void removeService(Service service);//移除服务
}
StandardServer类
StandardServer类是Server接口的标准实现。
一个服务器组件可以有0个或者多个服务组件。StandardServer类提供了addService()方法、removeService()方法和findService方法的实现。
addService将服务组件添加到服务器组件中,服务器组件中包含一个数组来保存服务组件。
public void addService(Service service) { service.setServer(this); synchronized (services) {
Service results[] = new Service[services.length + 1];
System.arraycopy(services, 0, results, 0, services.length);
results[services.length] = service;
services = results; if (getState().isAvailable()) { //服务可用,就启动服务
try {
service.start();
} catch (LifecycleException e) {
// Ignore
}
} // Report this property change to interested listeners
support.firePropertyChange("service", null, service);
} }
removeService从服务组件数组中移除,并停止服务。
public void removeService(Service service) { synchronized (services) {
int j = -1;
for (int i = 0; i < services.length; i++) {
if (service == services[i]) {
j = i;
break;
}
}
if (j < 0)
return;
try {
services[j].stop();
} catch (LifecycleException e) {
// Ignore
}
int k = 0;
Service results[] = new Service[services.length - 1];
for (int i = 0; i < services.length; i++) {
if (i != j)
results[k++] = services[i];
}
services = results; // Report this property change to interested listeners
support.firePropertyChange("service", service, null);
}
}
StandardServer类的4个生命周期相关的方法,是由LifecycleBase中的init调用initInternal(StandardServer),start调用startInternal(StandardServer),stop调用stopInternal(StandardServer),await(StandardServer)方法。
initInternal主要是对资源的初始化和服务组件的初始化。
protected void initInternal() throws LifecycleException {
super.initInternal();
...
// Register the naming resources
globalNamingResources.init();
...
// Initialize our defined Services
for (int i = 0; i < services.length; i++) {
services[i].init();
}
}
startInternal设置生命周期,设置服务器组件的状态,启动服务组件。
@Override
protected void startInternal() throws LifecycleException {
fireLifecycleEvent(CONFIGURE_START_EVENT, null);
setState(LifecycleState.STARTING);
globalNamingResources.start();
// Start our defined Services
synchronized (services) {
for (int i = 0; i < services.length; i++) {
services[i].start();
}
}
}
await一直等待停止命令。
Service接口
主要与连接器,服务器组件,执行器相关的信息
public interface Service extends Lifecycle {
public void addConnector(Connector connector);
public Connector[] findConnectors();
public void removeConnector(Connector connector);
public void addExecutor(Executor ex);
public Executor[] findExecutors();
public Executor getExecutor(String name);
public void removeExecutor(Executor ex);
public Server getServer();
public void setServer(Server server);
}
ServiceStandard对Service的接口实现,同时也有生命周期方法
startInternal
protected void startInternal() throws LifecycleException { if(log.isInfoEnabled())
log.info(sm.getString("standardService.start.name", this.name));
setState(LifecycleState.STARTING); // Start our defined Container first
if (container != null) {
synchronized (container) {
container.start();
}
} synchronized (executors) {
for (Executor executor: executors) {
executor.start();
}
} // Start our defined Connectors second
synchronized (connectors) {
for (Connector connector: connectors) {
try {
// If it has already failed, don't try and start it
if (connector.getState() != LifecycleState.FAILED) {
connector.start();
}
} catch (Exception e) {
log.error(sm.getString(
"standardService.connector.startFailed",
connector), e);
}
}
}
}
initInternal初始化容器,执行器以及连接器。
protected void initInternal() throws LifecycleException { super.initInternal(); if (container != null) {
container.init();
} // Initialize any Executors
for (Executor executor : findExecutors()) {
if (executor instanceof LifecycleMBeanBase) {
((LifecycleMBeanBase) executor).setDomain(getDomain());
}
executor.init();
} // Initialize our defined Connectors
synchronized (connectors) {
for (Connector connector : connectors) {
try {
connector.init();
} catch (Exception e) {
String message = sm.getString(
"standardService.connector.initFailed", connector);
log.error(message, e); if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE"))
throw new LifecycleException(message);
}
}
}
}
stopInternal停止连接器和执行器。
protected void stopInternal() throws LifecycleException { // Pause connectors first
synchronized (connectors) {
for (Connector connector: connectors) {
try {
connector.pause();
} catch (Exception e) {
log.error(sm.getString(
"standardService.connector.pauseFailed",
connector), e);
}
}
} if(log.isInfoEnabled())
log.info(sm.getString("standardService.stop.name", this.name));
setState(LifecycleState.STOPPING); // Stop our defined Container second
if (container != null) {
synchronized (container) {
container.stop();
}
} // Now stop the connectors
synchronized (connectors) {
for (Connector connector: connectors) {
if (!LifecycleState.STARTED.equals(
connector.getState())) {
// Connectors only need stopping if they are currently
// started. They may have failed to start or may have been
// stopped (e.g. via a JMX call)
continue;
}
try {
connector.stop();
} catch (Exception e) {
log.error(sm.getString(
"standardService.connector.stopFailed",
connector), e);
}
}
} synchronized (executors) {
for (Executor executor: executors) {
executor.stop();
}
}
}
最新文章
- UImenuController
- 一个简单算法题引发的思考<;DNA sorting>;(about cin/template/new etc)
- 【POJ】2449 Remmarguts&#39; Date(k短路)
- 数据结构与算法课程作业--1014. Translation
- 关于网络连接方式的总结(HostOnly,NAT....)
- symfony2-不同bundle的entity的一对多关系
- Oracle分区知识
- 转:说说angularjs中的$parse和$eval
- 在linux ubuntu下搭建深度学习/机器学习开发环境
- Ext.override
- C高级第二次作业
- sentiwordnet的简单使用
- django模板系统(上)
- osx升级到10.10后,使用pod install报错解决的方法
- [USACO08NOV]Mixed Up Cows
- Python day1 ---python基础1.1
- linux centos7 erlang rabbitmq安装
- [iOS微博项目 - 4.1] - cell的frame模型
- 《ArcGIS Runtime SDK for Android开发笔记》——数据制作篇:紧凑型切片制作(Server缓存切片)
- 【MVC5】First AngularJS
热门文章
- motto - question - bodyParser.urlencoded 中设置 extended 为 true 和 false 有什么区别吗?
- tcl之控制流-for
- hasattr() &; getattr() &; setattr()
- Samba和NFS文件共享
- setBackgroundResource和setImageResource的区别
- swoole创建websocket服务器
- 干货100+ 最超全的web开发工具和资源大集合
- 云计算之路-阿里云上:OCS问题的进展以及11:30-11:50遇到的问题
- 一个Objective-C对象如何进行内存布局?(考虑有父类的情况)
- 【活动】参加葡萄城控件主办的“谁是报表达人”知识评测活动,赢取iPad Mini2