聊聊AsyncHttpClient的exception

蚀纹迭代
• 阅读 218

本文主要研究一下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<?> 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的时候,对于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。

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
科工人 科工人
4年前
聊聊golang的DDD项目结构
序本文主要研究一下golang的DDD项目结构interfacesfoodappserver/interfacesinterfacesgit:(master)tree.|____fileupload||____fileformat.go||____fileupload.go|____food_handler.go|__
Wesley13 Wesley13
3年前
jmxtrans+influxdb+grafana监控zookeeper实战
序本文主要研究一下如何使用jmxtransinfluxdbgranfa监控zookeeper配置zookeeperjmx在conf目录下新增zookeeperenv.sh,并使用chmodx赋予执行权限,内容如下JMXLOCALONLYfalseJMXDISABLEfals
Wesley13 Wesley13
3年前
java 面试知识点笔记(十四)异常体系
问:Error和Exception的区别?!(https://oscimg.oschina.net/oscnet/1decf8ddc93b072fe2734567ae446b42079.jpg)ps:Throwable上层是ObjectError:程序无法处理的系统错误,编译器不做检查Exception:程序可以处理
如何优雅的处理异常
Java语言按照错误严重性,从throwale根类衍生出Error和Exception两大派系。本文从异常的定义、处理异常的方式、如何优雅的抛出异常以及处理异常等方面来聊聊如何异常这件事
Wesley13 Wesley13
3年前
javaAPI_IO流基础_异常
异常1.异常的概述和分类java中的异常有一个超类Throwable,然后其有俩个子类接口Error和Exception,其中Error是严重问题,这一个是程序中无法解决的,而另一个Exception则是一般问题。Exception又可以分为俩个:(1).编译时期异常:不是RuntimeException的异常,这一个是必须
Wesley13 Wesley13
3年前
redis的HyperLogLog实战
序本文主要研究一下redis的HyperLogLog的用场相关命令pfadd每添加一个元素的复杂度为O(1)127.0.0.1:6379pfadduv0907uid1uid2uid3(integer)1添加元素到HyperLogLog中,如果内部有变动返回1,没有
Wesley13 Wesley13
3年前
HTTP面试题(二):HTTP请求报文和响应报文格式
!(https://oscimg.oschina.net/oscnet/0406894fb1274bee91fc53c84c516576.jpg)看都看了还不点个赞!(https://oscimg.oschina.net/oscnet/095d444dc9a449ee85afd19b00fdf52b.png)!(h
Wesley13 Wesley13
3年前
MYSQL主从同步故障解决(主键重复)
MYSQL主从同步故障解决(主键重复)转载2010年04月05日18:52:00标签:mysql(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fso.csdn.net%2Fso%2Fsearch%2Fs.do%3Fq%
Stella981 Stella981
3年前
RedisTemplate读取slowlog
序本文主要研究一下如何使用RedisTemplate(lettuce类库)读取slowlogmaven<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarterdata
蚀纹迭代
蚀纹迭代
Lv1
云物不殊乡国异,教儿且覆掌中杯。
文章
6
粉丝
0
获赞
0