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的资料请关注脚本之家其它相关文章!

相关文章

  • 在controller中如何设置接收参数的默认值

    在controller中如何设置接收参数的默认值

    这篇文章主要介绍了在controller中如何设置接收参数的默认值,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • http basic authentication通过post方式访问api示例分享 basic认证示例

    http basic authentication通过post方式访问api示例分享 basic认证示例

    在HTTP中,基本认证是一种用来允许Web浏览器或其他客户端程序在请求时提供以用户名和口令形式的凭证,这篇文章主要介绍了http basic authentication通过post方式访问api示例,大家参考使用吧
    2014-01-01
  • java运行windows的cmd命令简单代码

    java运行windows的cmd命令简单代码

    这篇文章主要介绍了java运行windows的cmd命令简单代码,有需要的朋友可以参考一下
    2013-12-12
  • Java 使用Socket正确读取数据姿势

    Java 使用Socket正确读取数据姿势

    这篇文章主要介绍了Java 使用Socket正确读取数据姿势,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Java中的15种锁

    Java中的15种锁

    在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章小编将向大家介绍是各种锁的分类,感兴趣的小伙伴可以参考下面文章的具体内容
    2021-09-09
  • SpringBoot 集成 ShedLock 分布式锁的示例详解

    SpringBoot 集成 ShedLock 分布式锁的示例详解

    ShedLock是一个在分布式环境中使用的定时任务框架,用于解决在分布式环境中的多个实例的相同定时任务在同一时间点重复执行的问题,本文重点给大家介绍SpringBoot 分布式锁ShedLock的相关知识,感兴趣的朋友一起看看吧
    2021-08-08
  • SpringBoot返回前端Long型丢失精度后两位变成00的解决

    SpringBoot返回前端Long型丢失精度后两位变成00的解决

    在后端开发中,当Long类型的ID超过19位时,前端JavaScript可能会出现精度问题,导致最后两位变成00,本文提出了三种解决方案:将ID转换为字符串、使用@JsonSerialize注解和使用@JsonFormat注解,通过这些方法,可以确保ID在前后端传输过程中不会发生精度丢失
    2026-01-01
  • Java实现一行一行读取文本的多种方法详解

    Java实现一行一行读取文本的多种方法详解

    这篇文章主要为大家详细介绍了Java实现一行一行读取文本的多种方法,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下
    2025-10-10
  • Java Apache POI实现导出Excel的实战指南

    Java Apache POI实现导出Excel的实战指南

    在企业级开发中,Excel 数据导出是高频需求,本文基于 Apache POI 实现灾情速报员数据 Excel 导出功能,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下
    2026-04-04
  • mybatis连接MySQL8出现的问题解决方法

    mybatis连接MySQL8出现的问题解决方法

    这篇文章主要介绍了mybatis连接MySQL8出现的问题解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10

最新评论