2019独角兽企业重金招聘Python工程师标准>>>

一、概述

查看Jetty-io包,清单如下:

接口类:
ByteBufferPool
ClientConnectionFactory
Connection
Connection.Listener
Connection.UpgradeFrom
Connection.UpgradeTo
EndPoint
ManagedSelector.SelectableEndPoint
NetworkTrafficListener
实体类:
AbstractConnection
AbstractEndPoint
ArrayByteBufferPool
ArrayByteBufferPool.Bucket
ByteArrayEndPoint
ByteBufferPool.Lease
ChannelEndPoint
ClientConnectionFactory.Helper
Connection.Listener.Adapter
FillInterest
IdleTimeout
LeakTrackingByteBufferPool
ManagedSelector
MappedByteBufferPool
MappedByteBufferPool.Tagged
NegotiatingClientConnection
NegotiatingClientConnectionFactory
NetworkTrafficListener.Adapter
NetworkTrafficSelectChannelEndPoint
SelectChannelEndPoint
SelectorManager
WriteFlusher
WriterOutputStream
异常类:
EofException
RuntimeIOException

从名字看几个主要的类可能为:Connection、ByteBufferPool、SelectorManager、EndPoint,因为其他类应该是从中延伸出来的。

二、类分析

首先看Connection接口:

public interface Connection extends Closeable
{public void addListener(Listener listener);public void onOpen();/*** <p>Callback method invoked when this {@link Connection} is closed.</p>* <p>Creators of the connection implementation are responsible for calling this method.</p>*/public void onClose();/*** @return the {@link EndPoint} associated with this {@link Connection}*/public EndPoint getEndPoint();/*** <p>Performs a logical close of this connection.</p>* <p>For simple connections, this may just mean to delegate the close to the associated* {@link EndPoint} but, for example, SSL connections should write the SSL close message* before closing the associated {@link EndPoint}.</p>*/@Overridepublic void close();public int getMessagesIn();public int getMessagesOut();public long getBytesIn();public long getBytesOut();public long getCreatedTimeStamp();public interface UpgradeFrom extends Connection{/* ------------------------------------------------------------ *//** Take the input buffer from the connection on upgrade.* <p>This method is used to take any unconsumed input from* a connection during an upgrade.* @return A buffer of unconsumed input. The caller must return the buffer* to the bufferpool when consumed and this connection must not.*/ByteBuffer onUpgradeFrom();}public interface UpgradeTo extends Connection{/*** <p>Callback method invoked when this {@link Connection} is upgraded.</p>* <p>This must be called before {@link #onOpen()}.</p>* @param prefilled An optional buffer that can contain prefilled data. Typically this* results from an upgrade of one protocol to the other where the old connection has buffered* data destined for the new connection.  The new connection must take ownership of the buffer* and is responsible for returning it to the buffer pool*/void onUpgradeTo(ByteBuffer prefilled);}/* ------------------------------------------------------------ *//** * <p>A Listener for connection events.</p>* <p>Listeners can be added to a {@link Connection} to get open and close events.* The AbstractConnectionFactory implements a pattern where objects implement* this interface that have been added via {@link Container#addBean(Object)} to* the Connector or ConnectionFactory are added as listeners to all new connections* </p>*/public interface Listener{public void onOpened(Connection connection);public void onClosed(Connection connection);public static class Adapter implements Listener{@Overridepublic void onOpened(Connection connection){}@Overridepublic void onClosed(Connection connection){}}}
}

Connection接口主要用来添加监听,并定义监听接口Listener。

再看一个实现了Connection接口的抽象类AbstractConnection:

public abstract class AbstractConnection implements Connection
{private static final Logger LOG = Log.getLogger(AbstractConnection.class);private final List<Listener> listeners = new CopyOnWriteArrayList<>();private final long _created=System.currentTimeMillis();private final EndPoint _endPoint;private final Executor _executor;private final Callback _readCallback;private int _inputBufferSize=2048;protected AbstractConnection(EndPoint endp, Executor executor){if (executor == null)throw new IllegalArgumentException("Executor must not be null!");_endPoint = endp;_executor = executor;_readCallback = new ReadCallback();}@Overridepublic void addListener(Listener listener){listeners.add(listener);}public int getInputBufferSize(){return _inputBufferSize;}public void setInputBufferSize(int inputBufferSize){_inputBufferSize = inputBufferSize;}protected Executor getExecutor(){return _executor;}@Deprecatedpublic boolean isDispatchIO(){return false;}protected void failedCallback(final Callback callback, final Throwable x){if (callback.isNonBlocking()){try{callback.failed(x);}catch (Exception e){LOG.warn(e);}}else{try{getExecutor().execute(new Runnable(){@Overridepublic void run(){try{callback.failed(x);}catch (Exception e){LOG.warn(e);}}});}catch(RejectedExecutionException e){LOG.debug(e);callback.failed(x);}}}/*** <p>Utility method to be called to register read interest.</p>* <p>After a call to this method, {@link #onFillable()} or {@link #onFillInterestedFailed(Throwable)}* will be called back as appropriate.</p>* @see #onFillable()*/public void fillInterested(){if (LOG.isDebugEnabled())LOG.debug("fillInterested {}",this);getEndPoint().fillInterested(_readCallback);}public boolean isFillInterested(){return getEndPoint().isFillInterested();}/*** <p>Callback method invoked when the endpoint is ready to be read.</p>* @see #fillInterested()*/public abstract void onFillable();/*** <p>Callback method invoked when the endpoint failed to be ready to be read.</p>* @param cause the exception that caused the failure*/protected void onFillInterestedFailed(Throwable cause){if (LOG.isDebugEnabled())LOG.debug("{} onFillInterestedFailed {}", this, cause);if (_endPoint.isOpen()){boolean close = true;if (cause instanceof TimeoutException)close = onReadTimeout();if (close){if (_endPoint.isOutputShutdown())_endPoint.close();else{_endPoint.shutdownOutput();fillInterested();}}}}/*** <p>Callback method invoked when the endpoint failed to be ready to be read after a timeout</p>* @return true to signal that the endpoint must be closed, false to keep the endpoint open*/protected boolean onReadTimeout(){return true;}@Overridepublic void onOpen(){if (LOG.isDebugEnabled())LOG.debug("onOpen {}", this);for (Listener listener : listeners)listener.onOpened(this);}@Overridepublic void onClose(){if (LOG.isDebugEnabled())LOG.debug("onClose {}",this);for (Listener listener : listeners)listener.onClosed(this);}@Overridepublic EndPoint getEndPoint(){return _endPoint;}@Overridepublic void close(){getEndPoint().close();}@Overridepublic int getMessagesIn(){return -1;}@Overridepublic int getMessagesOut(){return -1;}@Overridepublic long getBytesIn(){return -1;}@Overridepublic long getBytesOut(){return -1;}@Overridepublic long getCreatedTimeStamp(){return _created;}@Overridepublic String toString(){return String.format("%s@%x[%s]",getClass().getSimpleName(),hashCode(),_endPoint);}private class ReadCallback implements Callback{@Overridepublic void succeeded(){onFillable();}@Overridepublic void failed(final Throwable x){onFillInterestedFailed(x);}@Overridepublic String toString(){return String.format("AC.ReadCB@%x{%s}", AbstractConnection.this.hashCode(),AbstractConnection.this);}}
}

转载于:https://my.oschina.net/daidetian/blog/522976

Jetty9 源码初解(2)——IO之Connection相关推荐

  1. Rxjava源码分析之IO.Reactivex.Observer

    Android 中的观察者模式,Rxjava中有两个重要的类Observable和Observer,函数响应式编程具体表现为一个观察者(Observer)订阅一个可观察对象(Observable).通 ...

  2. Rxjava源码分析之IO.Reactivex.Observable

    Rxjava 源码系列目录 Rxjava源码分析之IO.Reactivex.Observer Rxjava源码分析之IO.Reactivex.CompositeDisposable Rxjava源码分 ...

  3. Rxjava源码分析之IO.Reactivex.CompositeDisposable

    Rxjava 源码系列目录 Rxjava源码分析之IO.Reactivex.Observer Rxjava源码分析之IO.Reactivex.CompositeDisposable Rxjava源码分 ...

  4. Java源码解析——Java IO包

    一.基础知识: 1. Java IO一般包含两个部分:1)java.io包中阻塞型IO:2)java.nio包中的非阻塞型IO,通常称为New IO.这里只考虑到java.io包中堵塞型IO: 2. ...

  5. 【JDK源码】java.io包常用类详解

    看完java.io的JDK源码,在网上发现一篇关于java.io中的类使用的文章总结的很全面,看完之后在原文的基础上加了一些自己的总结如下构成了本篇文章.原文地址 一.Java Io流 1. Java ...

  6. spring源码分析-core.io包里面的类

    前些日子看<深入理解javaweb开发>时,看到第一章java的io流,发觉自己对io流真的不是很熟悉.然后看了下JDK1.7中io包的一点点代码,又看了org.springframewo ...

  7. java io源码解读_Java IO源码分析(五)——CharArrayReader 和 CharArrayWriter

    简介 CharArrayReader 是字符数组的输入流,它和我们之前讲的ByteArrayInputStream十分类似,顾名思义,区别在于一个用于字符数组,一个用于字节数组,在Java中字符是16 ...

  8. nginx源码分析之IO多路复用流程

    一.             主流程 几乎所有的服务器程序的工作模式都是: (1)      初始化一些参数: (2)      开启监听socket: (3)      在主线程的死循环(一般都是死 ...

  9. Flink 源码解析--Async IO的实现

    在使用Flink处理实时数据流时,经常需要和外部系统进行交互.例如,在构建实时数据仓库的时候,通常需要将消息和外部维表进行关联,以获得额外的维度数据.由于外部系统的响应时间和网络延迟可能会很高,如果采 ...

最新文章

  1. [agc016e]poor turkeys
  2. ActiveMQ集成Spring
  3. OpenGL ES之深入解析如何实现“手写绘画板”
  4. sql语句中动态变量中间含有单引号
  5. Spring.NET 1.3.1 新特性探索系列2——WCF命名空间解析器
  6. 算法题复习(快排、链表、二分、哈希、双指针)
  7. Sql Server 2005 获取表结构信息
  8. R2CNN 算法笔记
  9. 两表联合查询,求TOP100商品。。。。。。。。。。感激不尽!
  10. tableau中创建一个默认为今天日期的日期字段
  11. 实测 CSDN开发助手-Chrome插件
  12. php base64 转 amr,base64转amr文件
  13. python, numpy中的.tile方法解释
  14. 硬盘测试软件cry,CrystalDiskMark测试硬盘官方版
  15. 将照片p成素描画——ps
  16. 让div中的p标签文字垂直居中的方法
  17. ts没有与此调用匹配的重载
  18. Python表格数据处理方法
  19. 【51Nod1386】双马尾机器人(分块+dp)
  20. android平板灵敏度,和平精英iPad最稳灵敏度怎么设置

热门文章

  1. mysql 生成xml 表头_Spring Boot + MySql + Mybatis generator 自动生成mapper/entity/xml文件
  2. CMSimple内容管理系统
  3. 关于集合类的做法示例 实体类赋值 cnblogs
  4. Window服务器可安装的live messager最新版本-20090826
  5. JS针对图片加载及404处理
  6. WordPress:如何判断登录用户的角色
  7. 让所有IE支持HTML5
  8. Word文档显示标题3
  9. Visual Studio 2013 详细安装教程(安装+注册)
  10. LeetCode 283. Move Zeroes