Tomcat类是整个tomcat的起点,负责加载所有的配置信息以及把配置信息解析转换成tomcat组件对象。

Context addWebapp(String contextPath, String baseDir) throws ServletException //添加context(这个可以理解为默认context)
Context addContext(String contextPath, String baseDir) //添加context (这个是真正的上层业务逻辑的context)
Wrapper addServlet(String contextPath, String servletName,String servletClass)
public void start() throws LifecycleException { //Server和Connector的加载
getServer();
getConnector();
server.start();
} public Host getHost() { //随着context的加载而加载
if (host == null) {
host = new StandardHost();
host.setName(hostname); getEngine().addChild( host );
}
return host;
} public Engine getEngine() {
if(engine == null ) {
getServer();
engine = new StandardEngine();
engine.setRealm(defaultRealm);
service.setContainer(engine);
}
return engine;
}

从接口上看:一个Server中包含了多个Service,一个Service中包含多个Connector以及一个Engine,一个Engine中包含多个Host,一个Host下有多个Context,一个Context下有多个Wrapper。

但是从实现上可以看到一个tomcat其实内部只包含了一个Server,一个Server包含了一个Service,一个Service包含了一个Connector

interface Server extends Lifecycle{
public void addService(Service service);
} public interface Service extends Lifecycle {
public void addConnector(Connector connector);
public void setContainer(Container container);
}
public class StandardService extends LifecycleMBeanBase implements Service {
protected Connector connectors[] = new Connector[0];
protected Container container = null;
}

连接器

class Connector extends LifecycleMBeanBase{
protected ProtocolHandler protocolHandler = null;
public Request createRequest() {
Request request = new Request();
request.setConnector(this);
return (request);
}
public Response createResponse() {
Response response = new Response();
response.setConnector(this);
return (response);
}
@Override
protected void initInternal() throws LifecycleException { super.initInternal(); // Initialize adapter
adapter = new CoyoteAdapter(this);
protocolHandler.setAdapter(adapter); try {
protocolHandler.init();
} catch (Exception e) {
throw new LifecycleException
(sm.getString
("coyoteConnector.protocolHandlerInitializationFailed"), e);
}
}
protected void startInternal() throws LifecycleException {
protocolHandler.start();
}
}
 
public interface ProtocolHandler {//协议托管
public void setAdapter(Adapter adapter);
public Adapter getAdapter();
其他组件生命周期的方法 start、init等等
}
AbstractEndpoint:整个tomcat I/O流的处理框架,内部实现了tomcat底层通讯的所需要的一些基本组件。
public abstract class AbstractEndpoint {
private volatile LimitLatch connectionLimitLatch = null;
public static interface Handler {
public Object getGlobal();
public void recycle();
}
public abstract static class Acceptor implements Runnable {
}
}

1.LimitLatch:用于限制tomcat并发数,当一个请求进来之后,只有等到了LimitLatch才能进行下一步的处理,否则只能等待

2.Aceptor:用于接受socket,并对socket进行封装

3.Handler:用于处理Aceptor处理后的Socket

public abstract class AbstractProtocol implements ProtocolHandler, MBeanRegistration {
protected AbstractEndpoint endpoint = null;
protected static class RecycledProcessors<P extends Processor<S>, S> extends ConcurrentLinkedQueue<Processor<S>>{
private transient AbstractConnectionHandler<S,P> handler;
public Processor<S> poll()
public boolean offer(Processor<S> processor)
}
protected abstract static class AbstractConnectionHandler<S,P extends Processor<S> implements AbstractEndpoint.Handler{
protected RecycledProcessors<P,S> recycledProcessors = new RecycledProcessors<P,S>(this);
public SocketState process(SocketWrapper<S> socket, SocketStatus status) {
...
state = processor.process(socket);
}
protected abstract P createProcessor();
}
}
 
public abstract class AbstractHttp11Protocol extends AbstractProtocol
public abstract class AbstractHttp11JsseProtocol extends AbstractHttp11Protocol
public class Http11NioProtocol extends AbstractHttp11JsseProtocol {
public Http11NioProtocol() {
endpoint=new NioEndpoint();
cHandler = new Http11ConnectionHandler(this);
((NioEndpoint) endpoint).setHandler(cHandler);
} protected static class Http11ConnectionHandler extends AbstractConnectionHandler<NioChannel,Http11NioProcessor> implements Handler {
public Http11NioProcessor createProcessor() {
Http11NioProcessor processor = new Http11NioProcessor(proto.getMaxHttpHeaderSize(), (NioEndpoint)proto.endpoint,proto.getMaxTrailerSize());
...
register(processor);
return processor;
}
}
}
public class NioEndpoint extends AbstractEndpoint {
protected class Acceptor extends AbstractEndpoint.Acceptor {
public void run(){
try {
countUpOrAwaitConnection();
socket = serverSock.accept();
} catch (IOException ioe) {
...
}
if (running && !paused) {
if (!setSocketOptions(socket)) {
countDownConnection();
closeSocket(socket);
}
}
protected boolean setSocketOptions(SocketChannel socket) {
// Process the connection
try {
socket.configureBlocking(false);
Socket sock = socket.socket();
socketProperties.setProperties(sock); NioChannel channel = nioChannels.poll();
if ( channel == null ) {
// SSL setup
if (sslContext != null) {

channel = new SecureNioChannel(socket, engine, bufhandler, selectorPool);
} else {

channel = new NioChannel(socket, bufhandler);
}
} else {
channel.setIOChannel(socket);
}
getPoller0().register(channel);
}
}
}
 
public class Poller implements Runnable {
public void run() {
Iterator<SelectionKey> iterator =
keyCount > 0 ? selector.selectedKeys().iterator() : null;
while (iterator != null && iterator.hasNext()) {
SelectionKey sk = iterator.next();
KeyAttachment attachment = (KeyAttachment) sk.attachment();
// Attachment may be null if another thread has called
// cancelledKey()
if (attachment == null) {
iterator.remove();
} else {
attachment.access();
iterator.remove();
processKey(sk, attachment);
}
}
} protected boolean processKey(SelectionKey sk, KeyAttachment attachment) {
if (!processSocket(channel, SocketStatus.OPEN, true)){
processSocket(channel, SocketStatus.DISCONNECT, true);
}
} public boolean processSocket(NioChannel socket, SocketStatus status, boolean dispatch) {
SocketProcessor sc = processorCache.poll();
if ( sc == null ) sc = new SocketProcessor(socket,status);
else sc.reset(socket,status);
if ( dispatch && getExecutor()!=null ) getExecutor().execute(sc);
else sc.run();
}
} protected class SocketProcessor implements Runnable {
public void run() {
if (status == null) {
state = handler.process(
(KeyAttachment) key.attachment(),
SocketStatus.OPEN);
} else {
state = handler.process(
(KeyAttachment) key.attachment(),
status);
}
}
}
}

Processor:协议处理器,把socket处理成request和response。

public interface Processor<S> {
SocketState process(SocketWrapper<S> socketWrapper) throws IOException;
}
public abstract class AbstractProcessor<S> implements ActionHook, Processor<S> {
protected Adapter adapter;
protected AbstractEndpoint endpoint;
public abstract SocketState process(SocketWrapper<S> socket)
}
 
public abstract class AbstractHttp11Processor<S> extends AbstractProcessor<S> {
Request request ;
Response response ;
public SocketState process(SocketWrapper<S> socketWrapper){
setSocketWrapper(socketWrapper);
prepareRequest(); //加一些Http的参数给request
adapter.service(request, response);
}
}
public class Http11NioProcessor extends AbstractHttp11Processor<NioChannel> {
}

Adaptor用于真正处理请求,也就是把Request和Response传递给上层的Web应用Container

public interface Adapter { //连接了底层的processor和上层的container
public void service(Request req, Response res)
}
public class CoyoteAdapter implements Adapter {
public void service(org.apache.coyote.Request req, org.apache.coyote.Response res){
Request request = (Request) req.getNote(ADAPTER_NOTES); //ServletRequest
Response response = (Response) res.getNote(ADAPTER_NOTES);//ServletResponse
boolean postParseSuccess = postParseRequest(req, request, res, response);
connector.getService().getContainer().getPipeline().getFirst().invoke(request, response); //进入容器
}
 
protected boolean postParseRequest(org.apache.coyote.Request req,
Request request,
org.apache.coyote.Response res,
Response response){
//cookie、session、requestLine等等Http组件
connector.getMapper().map(serverName, decodedURI, version,
request.getMappingData()); //设置本次请求的host、context、wrapper
request.setContext((Context) request.getMappingData().context);
request.setWrapper((Wrapper) request.getMappingData().wrapper);
parseSessionCookiesId(req, request);
parseSessionSslId(request);
}
}
 
public final class Mapper {
protected Host[] hosts = new Host[0];
protected static final class Host extends MapElement {
public ContextList contextList = null;
}
protected static final class ContextList {
public Context[] contexts = new Context[0];
}
public synchronized void addHost(String name, String[] aliases,Object host)
public void addContextVersion(String hostName, Object host, String path,String version, Object context, String[] welcomeResources,javax.naming.Context resources)
public void map(MessageBytes host, MessageBytes uri, String version, MappingData mappingData)
}
public class MapperListener extends LifecycleMBeanBase{
private Mapper mapper = null;
@Override
public void startInternal() throws LifecycleException {
setState(LifecycleState.STARTING);
Engine engine = (Engine) connector.getService().getContainer();
addListeners(engine);
Container[] conHosts = engine.findChildren();
for (Container conHost : conHosts) {
Host host = (Host) conHost;
if (!LifecycleState.NEW.equals(host.getState())) {
registerHost(host);
}
}
}
private void registerHost(Host host) {
mapper.addHost(host.getName(), aliases, host);
for (Container container : host.findChildren()) {
if (container.getState().isAvailable()) {
registerContext((Context) container);
}
}
}
 
private void registerContext(Context context) {
for (Container container : context.findChildren()) {
registerWrapper((Wrapper) container);
}
}
}

生命周期接口:

public interface Lifecycle {
LifeCycle所有需要的时间状态:BEFORE_INIT_EVENT、AFTER_INIT_EVENT、START_EVENT等等
public void init() throws LifecycleException;
public void start() throws LifecycleException;
public void stop() throws LifecycleException;
public void destroy() throws LifecycleException;
}
 
public abstract class LifecycleBase implements Lifecycle {
private LifecycleSupport lifecycle = new LifecycleSupport(this);
public final synchronized void init() throws LifecycleException {
if (!state.equals(LifecycleState.NEW)) {
invalidTransition(Lifecycle.BEFORE_INIT_EVENT);
}
setStateInternal(LifecycleState.INITIALIZING, null, false); try {
initInternal();
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
setStateInternal(LifecycleState.FAILED, null, false);
throw new LifecycleException(
sm.getString("lifecycleBase.initFail",toString()), t);
} setStateInternal(LifecycleState.INITIALIZED, null, false);
}
protected abstract void initInternal() throws LifecycleException;
protected abstract void stopInternal() throws LifecycleException;
...
}
 
public final class LifecycleSupport {
private LifecycleListener listeners[] = new LifecycleListener[0];
public void addLifecycleListener(LifecycleListener listener)
public void fireLifecycleEvent(String type, Object data) { LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
LifecycleListener interested[] = listeners;
for (int i = 0; i < interested.length; i++)
interested[i].lifecycleEvent(event); }
}
 
public abstract class LifecycleMBeanBase extends LifecycleBase implements MBeanRegistration

容器的接口:

public interface Container extends Lifecycle {
public void addChild(Container child);
public Pipeline getPipeline();
public void backgroundProcess();
public void addContainerListener(ContainerListener listener);
}
 
public abstract class ContainerBase extends LifecycleMBeanBase{
protected Pipeline pipeline = new StandardPipeline(this);
@Override
public void backgroundProcess() { cluster.backgroundProcess(); loader.backgroundProcess(); manager.backgroundProcess();
realm.backgroundProcess();
Valve current = pipeline.getFirst();
while (current != null) {
try {
current.backgroundProcess();
} catch (Exception e) {
log.warn(sm.getString("containerBase.backgroundProcess.valve", current), e);
}
current = current.getNext();
}
fireLifecycleEvent(Lifecycle.PERIODIC_EVENT, null);
}
public void invoke(Request request, Response response)
throws IOException, ServletException {
pipeline.getFirst().invoke(request, response);
}
 
protected class ContainerBackgroundProcessor implements Runnable {
public void run() {
Thread.sleep(backgroundProcessorDelay * 1000L);
processChildren(parent, cl);
}
protected void processChildren(Container container, ClassLoader cl) {
container.backgroundProcess();
Container[] children = container.findChildren();
for (int i = 0; i < children.length; i++) {
if (children[i].getBackgroundProcessorDelay() <= 0) {
processChildren(children[i], cl);
}
}
}
}
Container的默认实现为ContainerBase,然后对应的四个子类为StandardEngine、StandardHost、StandardContext、StandardWrapper。
public interface Pipeline {
public void addValve(Valve valve);
public Valve[] getValves();
}
class StandardPipeline extends LifecycleBase implements Pipeline, Contained{
protected Valve first = null;
protected Valve basic = null;
}
public interface Valve {
public Valve getNext();
public void backgroundProcess();
public void invoke(Request request, Response response)
} public abstract class ValveBase extends LifecycleMBeanBase
implements Contained, Valve {
protected Container container = null;
public abstract void invoke(Request request, Response response)throws IOException, ServletException;
}

阀门控制整个请求的流向,Valve则是真正对请求进行处理,Container存储了所需要的所有原始信息

public interface Wrapper extends Container {
public Servlet allocate() throws ServletException;
public void load() throws ServletException;
}
class StandardWrapper extends ContainerBase implements ServletConfig, Wrapper{
protected volatile Servlet instance = null;
protected InstanceSupport instanceSupport = new InstanceSupport(this);
}
final class StandardWrapperValve extends ValveBase {
public final void invoke(Request request, Response response){
if (!unavailable) {
servlet = wrapper.allocate();
}
ApplicationFilterChain filterChain = factory.createFilterChain(request, wrapper, servlet);
...
filterChain.doFilter(request.getRequest(), response.getResponse());
}
 
public final class ApplicationFilterFactory {
public ApplicationFilterChain createFilterChain(ServletRequest request, Wrapper wrapper, Servlet servlet){
filterChain = (ApplicationFilterChain) req.getFilterChain();
filterChain.setServlet(servlet);
filterChain.setSupport(((StandardWrapper)wrapper).getInstanceSupport());
}
}
 
public final class InstanceSupport {
private Wrapper wrapper = null;
private InstanceListener listeners[] = new InstanceListener[0];
public void fireInstanceEvent(String type, Filter filter,
ServletRequest request,
ServletResponse response) { InstanceEvent event = new InstanceEvent(wrapper, filter, type,
request, response);
InstanceListener interested[] = listeners;
for (int i = 0; i < interested.length; i++)
interested[i].instanceEvent(event);
}
}

tomcat 代码集的更多相关文章

  1. Apache+Tomcat +mod_proxy集群负载均衡及session

      序言: 在玩Apache+Tomcat +mod_jk集群负载均衡及session的时候发现,还有一种方式可以实现,就是网上各位大牛们说的mod_proxy反向代理. 实在弄的我的知识细胞洋洋.实 ...

  2. apache整合tomcat部署集群

    近日,由于公司项目需要,所以学习了apache整合tomcat以及集群的一些知识. 所以做下笔记日后回顾可以用到. apache只有处理静态事物的能力, 而tomcat的强项就是处理动态的请求,所以a ...

  3. Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群

    Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群 >>>>>>>>>>>> ...

  4. Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享

    Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享 ============================= 蕃薯耀 2017年11月27日 http: ...

  5. nginx+tomcat实现集群,redis实现session共享,软连接实现文件共享:http://blog.csdn.net/hua1586981/article/details/78132710

    转载 2017年02月08日 16:52:41 730 相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能 ...

  6. Tomcat学习总结(8)——Tomcat+Nginx集群解决均衡负载及生产环境热部署

    近日,为解决生产环境热部署问题,决定在服务器中增加一个tomcat组成集群,利用集群解决热部署问题. 这样既能解决高并发瓶颈问题,又能解决热部署(不影响用户使用的情况下平滑更新生产服务器)问题. 因为 ...

  7. 单机配置tomcat 8 集群

    如何能在集群中的多个节点之间保持数据的一致性,会话(Session)信息是这些数据中最重要的一块. 本文当采用tomcat默认集群配置(<Cluster className="org. ...

  8. nginx与tomcat搭建集群,负载均衡

    --------------------------------------------------- 搭建环境(在桌面上即可完成测试) 先准备2个tomcat服务器 解压tomcat压缩包 得到 把 ...

  9. FineReport如何部署Tomcat服务器集群

    环境准备 Tomcat服务器集群中需要进行环境准备: Apache:Apache是http服务器,利用其对Tomcat进行负载均衡,这里使用的版本是Apache HTTP Server2.0.64: ...

随机推荐

  1. redis学习(一)redis简介

    REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统,是一种NoSql数据库.Redis是一个开源的使用ANS ...

  2. 常用快捷键以及linux命令整理

    关于快捷键的使用,网上有很多.自己在使用过程中不断整理用到的知识点.一个项目完成了就把涉及用到的快捷键和命令介绍给大家,都是一些比较基础的,常用的命令.希望大家有好的知识点,命令可以及时交流整理. 一 ...

  3. LOJ#2302. 「NOI2017」整数

    $n \leq 1000000$个操作:一,给$x$加上$a*2^b$:二,问$x$的某个二进制位$k$.$b,k \leq 30n$,$|a| \leq 1e9$. 30暴露了一切..可以把30个二 ...

  4. 使用 PHPMailer 发邮件

    /** * 发邮件 * * @param array $receiver 接收人信息 * @param array $attachment_info 附件信息 * @param string $is_ ...

  5. Oracle For 循环添加数据

    自己亲自使用的,绝对OK --添加数据declare i number; --用for实现 begin for i in 0 .. 500 loop insert into cust(custsn,t ...

  6. AC日记——线段树练习5 codevs 4927

    4927 线段树练习5  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 有n个数和5种操作 add a b ...

  7. Reveal.js演讲幻灯片框架

    摘要 还需学习参考的链接  https://www.tuicool.com/articles/2AFFj2j 无意中看到这个插件,很喜欢,可以作用在演讲ppt,幻灯片,用户指引上等.代码简单,易维护 ...

  8. 串口VMIN VTIME 详解

    原文地址: 以前跟着做过VxWorks的开发,主要通信方式是串口,因为底层BSP包已经做好了,串口通信非常简单.后来接触Linux,在一块OK6410上跑Linux串口通信,才发现原来天真的以为甚是简 ...

  9. java布局(每个名字都是有意义的)

    一.FlowLayout 1.流水布局:从左至右,排满换行 2.构造函数有三种: (1)FlowLayout() (2)FlowLayout(align) (3)FlowLayout(align, h ...

  10. Java 正则表达式的使用

    Java 正则表达式的使用 java.util.regex 包主要包括以下三个类: Pattern 类: pattern 对象是一个正则表达式的编译表示.Pattern 类没有公共构造方法.要创建一个 ...