1. 程式人生 > >rocketmq中的NettyRemotingClient類的簡單分析

rocketmq中的NettyRemotingClient類的簡單分析

oot soc p s log exc 分析 簡單 對象 err


rocketmq中的NettyRemotingClient類的簡單分析
Bootstrap handler = this.bootstrap.group(this.eventLoopGroupWorker).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_KEEPALIVE, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, nettyClientConfig.getConnectTimeoutMillis())
.option(ChannelOption.SO_SNDBUF, nettyClientConfig.getClientSocketSndBufSize())
.option(ChannelOption.SO_RCVBUF, nettyClientConfig.getClientSocketRcvBufSize())
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (nettyClientConfig.isUseTLS()) {
if (null != sslContext) {
pipeline.addFirst(defaultEventExecutorGroup, "sslHandler", sslContext.newHandler(ch.alloc()));
log.info("Prepend SSL handler");
} else {
log.warn("Connections are insecure as SSLContext is null!");
}
}
pipeline.addLast(
defaultEventExecutorGroup,
new NettyEncoder(),
new NettyDecoder(),
new IdleStateHandler(0, 0, nettyClientConfig.getClientChannelMaxIdleTimeSeconds()),
new NettyConnectManageHandler(),
new NettyClientHandler());
}
});

分析:一個是initChannel是抽象方法,ChannelInitializer是一個抽象類,這裏是對抽象類進行實現,創建了一個匿名類;
這裏的把匿名類通過handle方法進行賦值給AbstractBootstrap的handle屬性,後面會使用到該對象,並調用該對象的initChannnel方法;
其實,這裏你可以看做是一種註冊的結構,這樣給客戶端更多的開放接口,讓客戶端自己來控制一些內部的邏輯;

rocketmq中的NettyRemotingClient類的簡單分析