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方法,它會關(guān)閉或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用于表示連接池已經(jīng)關(guān)閉的異常
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未完成也沒有重試的時候會執(zhí)行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
小結(jié)
AsyncHttpClient一共定義了五個異常,它們都繼承了IOException,分別是ChannelClosedException、PoolAlreadyClosedException、RemotelyClosedException、TooManyConnectionsException、TooManyConnectionsPerHostException。
以上就是AsyncHttpClient的exception的詳細內(nèi)容,更多關(guān)于AsyncHttpClient的exception的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在controller中如何設(shè)置接收參數(shù)的默認值
這篇文章主要介紹了在controller中如何設(shè)置接收參數(shù)的默認值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
http basic authentication通過post方式訪問api示例分享 basic認證示例
在HTTP中,基本認證是一種用來允許Web瀏覽器或其他客戶端程序在請求時提供以用戶名和口令形式的憑證,這篇文章主要介紹了http basic authentication通過post方式訪問api示例,大家參考使用吧2014-01-01
Java 使用Socket正確讀取數(shù)據(jù)姿勢
這篇文章主要介紹了Java 使用Socket正確讀取數(shù)據(jù)姿勢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot 集成 ShedLock 分布式鎖的示例詳解
ShedLock是一個在分布式環(huán)境中使用的定時任務(wù)框架,用于解決在分布式環(huán)境中的多個實例的相同定時任務(wù)在同一時間點重復(fù)執(zhí)行的問題,本文重點給大家介紹SpringBoot 分布式鎖ShedLock的相關(guān)知識,感興趣的朋友一起看看吧2021-08-08
SpringBoot返回前端Long型丟失精度后兩位變成00的解決
在后端開發(fā)中,當Long類型的ID超過19位時,前端JavaScript可能會出現(xiàn)精度問題,導(dǎo)致最后兩位變成00,本文提出了三種解決方案:將ID轉(zhuǎn)換為字符串、使用@JsonSerialize注解和使用@JsonFormat注解,通過這些方法,可以確保ID在前后端傳輸過程中不會發(fā)生精度丟失2026-01-01
Java Apache POI實現(xiàn)導(dǎo)出Excel的實戰(zhàn)指南
在企業(yè)級開發(fā)中,Excel 數(shù)據(jù)導(dǎo)出是高頻需求,本文基于 Apache POI 實現(xiàn)災(zāi)情速報員數(shù)據(jù) Excel 導(dǎo)出功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2026-04-04
mybatis連接MySQL8出現(xiàn)的問題解決方法
這篇文章主要介紹了mybatis連接MySQL8出現(xiàn)的問題解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10

