您好,登錄后才能下訂單哦!
本篇內容介紹了“netty channelPool連接池怎么使用”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
如果流數據推送【持續不斷的數據 向服務端進行推送 沒有必要使用channelPool。也沒有必要釋放channel】
核心接口:ChannelPool
public ChannelPool newPool(Bootstrap bootstrap) { return new FixedChannelPool(bootstrap, iridiumClientChannelHandler, protocolOptions.getMaxConnections()); }}
核心實現類:
public class FixedChannelPool extends SimpleChannelPool
一個抽象類
public abstract class AbstractChannelPoolHandler implements ChannelPoolHandler { @Override public void channelAcquired(@SuppressWarnings("unused") Channel ch) throws Exception { // NOOP } @Override public void channelReleased(@SuppressWarnings("unused") Channel ch) throws Exception { // NOOP }}
Channel.close() 不是斷開連接 是 從eventLoop 移除 注銷
關于channelPool 健康檢查:僅僅是獲取連接前
ChannelHealthChecker 這個類可以判定連接池中的chanel 是否有效
public interface ChannelPoolHandler {
/**
* Called once a {@link Channel} was released by calling {@link ChannelPool#release(Channel)} or
* {@link ChannelPool#release(Channel, Promise)}.
*
* This method will be called by the {@link EventLoop} of the {@link Channel}.
*/
void channelReleased(Channel ch) throws Exception;
/**
* Called once a {@link Channel} was acquired by calling {@link ChannelPool#acquire()} or
* {@link ChannelPool#acquire(Promise)}.
*
* This method will be called by the {@link EventLoop} of the {@link Channel}.
*/
void channelAcquired(Channel ch) throws Exception;
/**
* Called once a new {@link Channel} is created in the {@link ChannelPool}.
*
* This method will be called by the {@link EventLoop} of the {@link Channel}.
*/
void channelCreated(Channel ch) throws Exception;
}
/* 當鏈接創建的時候添加channelhandler,只有當channel不足時會創建,但不會超過限制的最大channel數*/
@Override
protected void initChannel(Channel ch) throws Exception {
channelPoolHandler.channelCreated(ch);
}
}
“netty channelPool連接池怎么使用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。