1. public abstract class PoolEntry<T, C> {
  2.  
  3. private final String id;
  4. private final T route;  //路由
  5. private final C conn;  //http连接
  6. private final long created;  //创建时间
  7. private final long validityDeadline;
  8.  
  9. private long updated;
  10.  
  11. private long expiry;
  12.  
  13. private volatile Object state;
  14.  
  15. }

构造方法:

  1.   public PoolEntry(final String id, final T route, final C conn,
  2. final long timeToLive, final TimeUnit tunit) {
  3. super();
  4. Args.notNull(route, "Route");
  5. Args.notNull(conn, "Connection");
  6. Args.notNull(tunit, "Time unit");
  7. this.id = id;
  8. this.route = route;
  9. this.conn = conn;
  10. this.created = System.currentTimeMillis();
  11. this.updated = this.created;
  12. if (timeToLive > 0) {
  13. final long deadline = this.created + tunit.toMillis(timeToLive);
  14. // If the above overflows then default to Long.MAX_VALUE
  15. this.validityDeadline = deadline > 0 ? deadline : Long.MAX_VALUE;
  16. } else {
  17. this.validityDeadline = Long.MAX_VALUE;
  18. }
  19. this.expiry = this.validityDeadline;
  20. }

更新过期时间:

  1.   public synchronized void updateExpiry(final long time, final TimeUnit tunit) {
  2. Args.notNull(tunit, "Time unit");
  3. this.updated = System.currentTimeMillis();
  4. final long newExpiry;
  5. if (time > 0) {
  6. newExpiry = this.updated + tunit.toMillis(time);
  7. } else {
  8. newExpiry = Long.MAX_VALUE;
  9. }
  10. this.expiry = Math.min(newExpiry, this.validityDeadline);
  11. }

1、http连接池管理一个连接对象,其实管理的是一个PoolEntry实例;

2、什么时候更新PoolEntry实例信息,比如过期时间,状态等?

  在释放连接的时候会更新实例信息;

  具体在:PoolingHttpClientConnectionManager.releaseConnection(final HttpClientConnection managedConn, final Object state, final long keepalive, final TimeUnit tunit) 方法中调用;但是这个方法是我们在释放response是调用的。

  1.   public void releaseConnection(
  2. final HttpClientConnection managedConn,
  3. final Object state,
  4. final long keepalive, final TimeUnit tunit) {
  5. Args.notNull(managedConn, "Managed connection");
  6. synchronized (managedConn) {
  7. final CPoolEntry entry = CPoolProxy.detach(managedConn);
  8. if (entry == null) {
  9. return;
  10. }
  11. final ManagedHttpClientConnection conn = entry.getConnection();
  12. try {
  13. if (conn.isOpen()) {
  14. final TimeUnit effectiveUnit = tunit != null ? tunit : TimeUnit.MILLISECONDS;
  15. entry.setState(state);
  16. entry.updateExpiry(keepalive, effectiveUnit);  //keepalive 参数表示长连接的过期时间,在客户端通过keepAliveStrategy参数设置
  17. if (this.log.isDebugEnabled()) {
  18. final String s;
  19. if (keepalive > 0) {
  20. s = "for " + (double) effectiveUnit.toMillis(keepalive) / 1000 + " seconds";
  21. } else {
  22. s = "indefinitely";
  23. }
  24. this.log.debug("Connection " + format(entry) + " can be kept alive " + s);
  25. }
  26. conn.setSocketTimeout(0);
  27. }
  28. } finally {
  29. this.pool.release(entry, conn.isOpen() && entry.isRouteComplete());
  30. if (this.log.isDebugEnabled()) {
  31. this.log.debug("Connection released: " + format(entry) + formatStats(entry.getRoute()));
  32. }
  33. }
  34. }
  35. }

3、PoolingHttpClientConnectionManager 连接池参数 validateAfterInactivity说明:

  当从连接池中拿到一个poolEntry时,如果validateAfterInactivity参数大于0 且 这个PoolEntry实例的updated加validateAfterInactivity小于等于当前时间,会检查这个连接(httpEntry实例中的con)的连接状态 state 值;

  1. org.apache.http.impl.BHttpConnectionBase
  2.   public boolean isStale() {
  3. if (!isOpen()) {
  4. return true;
  5. }
  6. try {
  7. final int bytesRead = fillInputBuffer(1);
  8. return bytesRead < 0;
  9. } catch (final SocketTimeoutException ex) {
  10. return false;
  11. } catch (final IOException ex) {
  12. return true;
  13. }
  14. }

4、PoolingHttpClientConnectionManager 的关闭空闲超时连接方法:

  指PoolEntry实例的更新时间加 空闲时间(设置)大于等于 当前时间,表示这个连接超过空闲时间

  1.   public void closeIdleConnections(final long idleTimeout, final TimeUnit tunit) {
  2. if (this.log.isDebugEnabled()) {
  3. this.log.debug("Closing connections idle longer than " + idleTimeout + " " + tunit);
  4. }
  5. this.pool.closeIdle(idleTimeout, tunit);
  6. }

5、PoolingHttpClientConnectionManager 的关闭过期连接方法:

  指当前时间大于PoolEntry实例的expiry值

  1. public void closeExpiredConnections() {
  2. this.log.debug("Closing expired connections");
  3. this.pool.closeExpired();
  4. }

6、closeIdleConnections() 和 closeExpiredConnections() 对外提供,需要用户自己去调用,我们一般维护一个独立的线程去调用,清除空闲的连接和过期连接。

PoolEntry 参数讲解的更多相关文章

  1. android ui界面设计参数讲解

    百度文库: http://wenku.baidu.com/link?url=s66Hw6byBEzmjL77doYL1YQN4Y_39F7MovaHKs5mVGrzTDOQCAmiM-1N_6Cdm- ...

  2. libsvm 训练后的模型参数讲解(转)

    主要就是讲解利用libsvm-mat工具箱建立分类(回归模型)后,得到的模型model里面参数的意义都是神马?以及如果通过model得到相应模型的表达式,这里主要以分类问题为例子.测试数据使用的是li ...

  3. [转]libsvm 训练后的模型参数讲解

    http://blog.sina.com.cn/s/blog_6646924501018fqc.html 主要就是讲解利用libsvm-mat工具箱建立分类(回归模型)后,得到的模型model里面参数 ...

  4. Log4J入门教程(二) 参数讲解

    继续接着Log4J入门教程(一)中的例子进行讲解,其中log4j.properties中的内容为    Log4j的三个重要组件—— Loggers, Appenders, Layouts ,这三个组 ...

  5. Java Socket重要参数讲解

    (转自http://www.cnblogs.com/ggjucheng/archive/2012/01/06/2314679.html) ( http://docs.oracle.com/javase ...

  6. job_queue_processes参数讲解

    http://blog.sina.com.cn/s/blog_62defbef0101opv0.html http://blog.163.com/donfang_jianping/blog/stati ...

  7. Java可变参数讲解

    如果实现的多个方法,这些方法里面逻辑基本相同,唯一不同的是传递的参数的个数,可以使用可变参数可变参数的定义方法 数据类型...数组的名称,这个数组存储传递过来的参数,类似JavaScript注意点:  ...

  8. arm-linux-gcc 常用参数讲解 gcc编译器使用方法

    我们需要编译出运行在ARM平台上的代码,所使用的交叉编译器为 arm-linux-gcc.下面将arm-linux-gcc编译工具的一些常用命令参数介绍给大家.在此之前首先介绍下编译器的工作过程,在使 ...

  9. droppable的详细参数讲解

    jQuery-Draggable参数介绍     默认设置值: $.extend($.ui.draggable, { version: “1.7.1″, eventPrefix: “drag”, de ...

随机推荐

  1. Django入门三之urls.py重构及参数传递

    1. 内部重构 2. 外部重构 website/blog/urls.py website/website/urls.py 3. 两种参数处理方式 -1. blog/index/?id=1234& ...

  2. jq slideToggle()坑

    jQuery slideToggle() 方法 jQuery slideToggle() 方法可以在 slideDown() 与 slideUp() 方法之间进行切换. 如果元素向下滑动,则 slid ...

  3. SQL之left join,inner join,right join

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  4. Spring Boot常用注解总结

    Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...

  5. vue技术分享-你可能不知道的7个秘密

    前言 本文是vue源码贡献值Chris Fritz在公共场合的一场分享,觉得分享里面有不少东西值得借鉴,虽然有些内容我在工作中也是这么做的,还是把大神的ppt在这里翻译一下,希望给朋友带来一些帮助. ...

  6. Zookeeper学习

    http://www.cnblogs.com/caosiyang/archive/2012/11/09/2763190.html   http://www.cnblogs.com/haippy/tag ...

  7. java线程间通信之通过管道进行通信

    管道流PipeStream是一种特殊的流,用于在不同线程间直接传送数据,而不需要借助临时文件之类的东西. jdk中提供了四个类来使线程间可以通信: 1)PipedInputStream和PipedOu ...

  8. 功能强大的swagger-editor的介绍与使用

    一.Swagger Editor简介 Swagger Editor是一个开源的编辑器,并且它也是一个基于Angular的成功案例.在Swagger Editor中,我们可以基于YAML等语法定义我们的 ...

  9. Android TagFlowLayout完全解析 一款针对Tag的布局

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/48393217: 本文出自:[张鸿洋的博客] 一.概述 本文之前,先提一下关于上 ...

  10. Android监测手指上下左右滑动屏幕

    在开发android程序时,有时会需要监测手指滑动屏幕,当手指朝上下左右不同方向滑动时做出不同的响应,那怎么去实现呢? 利用Android提供的手势监测器就可以很方便的实现,直接上代码(已测试通过) ...