AsyncHttpClient exception异常源码流程解析

 更新时间:2023年12月11日 08:36:16   作者:codecraft  
这篇文章主要为大家介绍了AsyncHttpClient的exception源码流程解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下AsyncHttpClient的exception

ChannelClosedException

org/asynchttpclient/exception/ChannelClosedException.java

public final class ChannelClosedException extends IOException {
  public static final ChannelClosedException INSTANCE = unknownStackTrace(new ChannelClosedException(), ChannelClosedException.class, "INSTANCE");
  private ChannelClosedException() {
    super("Channel closed");
  }
}
ChannelClosedException用于表示Channel closed的异常

handleUnexpectedClosedChannel

org/asynchttpclient/netty/request/NettyRequestSender.java

public void handleUnexpectedClosedChannel(Channel channel, NettyResponseFuture<?> future) {
    if (Channels.isActiveTokenSet(channel)) {
      if (future.isDone()) {
        channelManager.closeChannel(channel);
      } else if (future.incrementRetryAndCheck() && retry(future)) {
        future.pendingException = null;
      } else {
        abort(channel, future,
                future.pendingException != null ? future.pendingException : RemotelyClosedException.INSTANCE);
      }
    }
  }
NettyRequestSender定义了handleUnexpectedClosedChannel方法,它会关闭或abort当前的channel

PoolAlreadyClosedException

org/asynchttpclient/exception/PoolAlreadyClosedException.java

public class PoolAlreadyClosedException extends IOException {
  public static final PoolAlreadyClosedException INSTANCE = unknownStackTrace(new PoolAlreadyClosedException(), PoolAlreadyClosedException.class, "INSTANCE");
  private PoolAlreadyClosedException() {
    super("Pool is already closed");
  }
}
PoolAlreadyClosedException用于表示连接池已经关闭的异常

sendRequestWithNewChannel

org/asynchttpclient/netty/request/NettyRequestSender.java

private <T> ListenableFuture<T> sendRequestWithNewChannel(Request request,
                                                            ProxyServer proxy,
                                                            NettyResponseFuture<T> future,
                                                            AsyncHandler<T> asyncHandler) {
    // some headers are only set when performing the first request
    HttpHeaders headers = future.getNettyRequest().getHttpRequest().headers();
    Realm realm = future.getRealm();
    Realm proxyRealm = future.getProxyRealm();
    requestFactory.addAuthorizationHeader(headers, perConnectionAuthorizationHeader(request, proxy, realm));
    requestFactory.setProxyAuthorizationHeader(headers, perConnectionProxyAuthorizationHeader(request, proxyRealm));
    future.setInAuth(realm != null && realm.isUsePreemptiveAuth() && realm.getScheme() != AuthScheme.NTLM);
    future.setInProxyAuth(
            proxyRealm != null && proxyRealm.isUsePreemptiveAuth() && proxyRealm.getScheme() != AuthScheme.NTLM);
    try {
      if (!channelManager.isOpen()) {
        throw PoolAlreadyClosedException.INSTANCE;
      }
      // Do not throw an exception when we need an extra connection for a
      // redirect.
      future.acquirePartitionLockLazily();
    } catch (Throwable t) {
      abort(null, future, getCause(t));
      // exit and don't try to resolve address
      return future;
    }
    //......
}
sendRequestWithNewChannel在channelManager非open的时候会抛出PoolAlreadyClosedException

RemotelyClosedException

org/asynchttpclient/exception/RemotelyClosedException.java

public final class RemotelyClosedException extends IOException {
  public static final RemotelyClosedException INSTANCE = unknownStackTrace(new RemotelyClosedException(), RemotelyClosedException.class, "INSTANCE");
  RemotelyClosedException() {
    super("Remotely closed");
  }
}
RemotelyClosedException用于表示Remotely closed的异常

handleUnexpectedClosedChannel

org/asynchttpclient/netty/request/NettyRequestSender.java

public void handleUnexpectedClosedChannel(Channel channel, NettyResponseFuture&lt;?&gt; future) {
    if (Channels.isActiveTokenSet(channel)) {
      if (future.isDone()) {
        channelManager.closeChannel(channel);
      } else if (future.incrementRetryAndCheck() &amp;&amp; retry(future)) {
        future.pendingException = null;
      } else {
        abort(channel, future,
                future.pendingException != null ? future.pendingException : RemotelyClosedException.INSTANCE);
      }
    }
  }
NettyRequestSender的handleUnexpectedClosedChannel的时候,对于future未完成也没有重试的时候会执行abort,并抛出RemotelyClosedException

TooManyConnectionsException

org/asynchttpclient/exception/TooManyConnectionsException.java

public class TooManyConnectionsException extends IOException {
  public TooManyConnectionsException(int max) {
    super("Too many connections: " + max);
  }
}
TooManyConnectionsException用于表示全局连接超过限制的异常

TooManyConnectionsPerHostException

org/asynchttpclient/exception/TooManyConnectionsPerHostException.java

public class TooManyConnectionsPerHostException extends IOException {
  public TooManyConnectionsPerHostException(int max) {
    super("Too many connections: " + max);
  }
}
TooManyConnectionsPerHostException用于表示连接超出单host限制的异常

acquireChannelLock

org/asynchttpclient/netty/channel/ConnectionSemaphore.java

public void acquireChannelLock(Object partitionKey) throws IOException {
    if (!tryAcquireGlobal())
      throw tooManyConnections;
    if (!tryAcquirePerHost(partitionKey)) {
      freeChannels.release();
      throw tooManyConnectionsPerHost;
    }
  }
acquireChannelLock方法在全局连接超出限制时抛出tooManyConnections,在单host连接超出限制时抛出tooManyConnectionsPerHost

小结

AsyncHttpClient一共定义了五个异常,它们都继承了IOException,分别是ChannelClosedException、PoolAlreadyClosedException、RemotelyClosedException、TooManyConnectionsException、TooManyConnectionsPerHostException。

以上就是AsyncHttpClient的exception的详细内容,更多关于AsyncHttpClient的exception的资料请关注脚本之家其它相关文章!

相关文章

  • 一文教会Java新手使用Spring MVC中的查询字符串和查询参数

    一文教会Java新手使用Spring MVC中的查询字符串和查询参数

    在使用springMVC框架构建web应用,客户端常会请求字符串、整型、json等格式的数据,这篇文章主要给大家介绍了关于通过一文教会Java新手使用Spring MVC中的查询字符串和查询参数的相关资料,需要的朋友可以参考下
    2024-01-01
  • Spring中的Sentinel规则持久化解析

    Spring中的Sentinel规则持久化解析

    这篇文章主要介绍了Spring中的Sentinel规则持久化解析,具体内容包括,Sentinel规则推送三种模式介绍,原始模式,拉模式,推模式,并对基于Nacos配置中心控制台实现推送进行详尽介绍,需要的朋友可以参考下
    2023-09-09
  • Spring Bean生命周期源码原理图解

    Spring Bean生命周期源码原理图解

    这篇文章主要介绍了Spring Bean生命周期源码原理图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • MyBatis中的JdbcType映射使用介绍

    MyBatis中的JdbcType映射使用介绍

    这篇文章主要介绍了MyBatis中的JdbcType映射使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Java实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等

    Java实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等

    这篇文章主要介绍了Java如何实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序 、快速排序、归并排序、堆排序和LST基数排序,需要的朋友可以参考下
    2015-07-07
  • java使用筛选法求n以内的素数示例(java求素数)

    java使用筛选法求n以内的素数示例(java求素数)

    这篇文章主要介绍了java使用筛选法求n以内的素数示例(java求素数),需要的朋友可以参考下
    2014-04-04
  • Eclipse如何导入Maven项目详解(新手初学)

    Eclipse如何导入Maven项目详解(新手初学)

    这篇文章主要介绍了Eclipse如何导入Maven项目详解(新手初学),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • idea如何关闭右侧类显示方法

    idea如何关闭右侧类显示方法

    这篇文章主要介绍了idea如何关闭右侧类显示方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • idea 2024使用Maven创建Java Web项目详细图文教程

    idea 2024使用Maven创建Java Web项目详细图文教程

    这篇文章主要给大家介绍了关于idea 2024使用Maven创建Java Web项目的相关资料,介绍了如何使用Maven创建一个Spring MVC项目,并配置Tomcat服务器以运行一个简单的Helloworld JSP页面,需要的朋友可以参考下
    2024-12-12
  • Java编程简单应用

    Java编程简单应用

    本文主要介绍了三个简单Java小程序———1、HelloWorld(HelloWorld的来源);2、输出个人信息3、输出特殊图案。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02

最新评论