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<?> 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。
以上就是AsyncHttpClient的exception的详细内容,更多关于AsyncHttpClient的exception的资料请关注脚本之家其它相关文章!
相关文章
Java输出通过InetAddress获得的IP地址数组详细解析
由于byte被认为是unsigned byte,所以最高位的1将会被解释为符号位,另外Java中存储是按照补码存储,所以1000 0111会被认为是补码形式,转换成原码便是1111 0001,转换成十进制数便是-1212013-09-09Spring Boot集成JavaMailSender发送邮件功能的实现
spring提供了发送邮件的接口JavaMailSender,通过JavaMailSender可以实现后端发送邮件,下面这篇文章主要给大家介绍了关于Spring Boot集成JavaMailSender发送邮件功能的相关资料,需要的朋友可以参考下2022-05-05springboot扫描引入jar包的service等组件方式
这篇文章主要介绍了springboot扫描引入jar包的service等组件方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-07-07
最新评论