本文主要是介绍读懂tomact源码三:Service,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
service是一个接口,在源码上有一段这样的话:
A Service is a group of one or more Connectors that share a single Container to process their incoming requests. This arrangement allows, for example,a non-SSL and SSL connector to share the same population of web apps.A given JVM can contain any number of Service instances; however, they are completely independent of each other and share only the basic JVM facilities and classes on the system class path.
Service是一组连接器共享单个容器,来解析他们收到的请求。这些请求可以是ssl或者不是ssl的。一个给定的jvm包含任何数量的service实例;但是,它们彼此相互独立,共享一份jvm虚拟机和系统变量的类。
Service的源码
public interface Service extends Lifecycle {// ----------------- Propertiespublic Container getContainer();public void setContainer(Container container);public String getInfo();public String getName();public void setName(String name);public Server getServer();public void setServer(Server server);public ClassLoader getParentClassLoader();public void setParentClassLoader(ClassLoader parent);// ----------------- Public Methodspublic void addConnector(Connector connector);public void removeConnector(Connector connector);public void addExecutor(Executor ex);public Executor[] findExecutors();public Executor getExecutor(String name);public void removeExecutor(Executor ex);
}
StandardService实现了Service这个接口
Property
- private final Object connectorsLock = new Object();
connectors的锁,主要是add、remove、restart操作 - protected PropertyChangeSupport support = new PropertyChangeSupport(this);
PropertyChangeSupport,我也不懂是什么鬼,看着是支持一些属性的更改 - protected Connector connectors[] = new Connector[0];
这个servce所关联的Connector - protected Container container = null;
The Container associated with this Service - private Server server = null;
这个service的所有者吧
method
- setContainer : 设置service的Container
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 (getState().isAvailable() && (this.container != null)) {try {this.container.start();} catch (LifecycleException e) {// Ignore}}if (getState().isAvailable() && (oldContainer != null)) {try {oldContainer.stop();} catch (LifecycleException e) {// Ignore}}// Report this property change to interested listenerssupport.firePropertyChange("container", oldContainer, this.container);}
- addConnector,添加Connector
public void addConnector(Connector connector) {synchronized (connectorsLock) {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 (getState().isAvailable()) {try {connector.start();} catch (LifecycleException e) {log.error(sm.getString("standardService.connector.startFailed",connector), e);}}// Report this property change to interested listenerssupport.firePropertyChange("connector", null, connector);}}
- removeConnector:删去一个Connector
public void removeConnector(Connector connector) {synchronized (connectorsLock) {int j = -1;for (int i = 0; i < connectors.length; i++) {if (connector == connectors[i]) {j = i;break;}}if (j < 0)return;if (connectors[j].getState().isAvailable()) {try {connectors[j].stop();} catch (LifecycleException e) {log.error(sm.getString("standardService.connector.stopFailed",connectors[j]), e);}}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 listenerssupport.firePropertyChange("connector", connector, null);}}
- 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 firstif (container != null) {synchronized (container) {container.start();}}synchronized (executors) {for (Executor executor : executors) {executor.start();}}// Start our defined Connectors secondsynchronized (connectorsLock) {for (Connector connector : connectors) {try {// If it has already failed, don't try and start itif (connector.getState() != LifecycleState.FAILED) {connector.start();}} catch (Exception e) {log.error(sm.getString("standardService.connector.startFailed",connector), e);}}}}
Start nested components : Executors, Connectors and Containers and implement the requirements of org.apache.catalina.util.LifecycleBase#startInternal().
5:initInternal:主要是初始化,Executor、Connector、container
protected void initInternal() throws LifecycleException {
super.initInternal();if (container != null) {container.init();}// Initialize any Executorsfor (Executor executor : findExecutors()) {if (executor instanceof LifecycleMBeanBase) {((LifecycleMBeanBase) executor).setDomain(getDomain());}executor.init();}// Initialize our defined Connectorssynchronized (connectorsLock) {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);}}}
}
6:startInternal:Executor、Connector、container开始执行
protected void startInternal() throws LifecycleException {if (log.isInfoEnabled())log.info(sm.getString("standardService.start.name", this.name));setState(LifecycleState.STARTING);// Start our defined Container firstif (container != null) {synchronized (container) {container.start();}}synchronized (executors) {for (Executor executor : executors) {executor.start();}}// Start our defined Connectors secondsynchronized (connectorsLock) {for (Connector connector : connectors) {try {// If it has already failed, don't try and start itif (connector.getState() != LifecycleState.FAILED) {connector.start();}} catch (Exception e) {log.error(sm.getString("standardService.connector.startFailed",connector), e);}}}}
7、stopInternal
protected void stopInternal() throws LifecycleException {
// Pause connectors firstsynchronized (connectorsLock) {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 secondif (container != null) {synchronized (container) {container.stop();}}// Now stop the connectorssynchronized (connectorsLock) {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();}}
}
**
note:注意Internal中Executor、Connector、container的次序
**
8.getParentClassLoader
public ClassLoader getParentClassLoader() {if (parentClassLoader != null)return (parentClassLoader);if (server != null) {return (server.getParentClassLoader());}return (ClassLoader.getSystemClassLoader());}
如果没有parentClassLoader和server,返回SystemClassLoader
这篇关于读懂tomact源码三:Service的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!