W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
我們會碰到引導客戶端 Channel 從另一個 Channel的情況。如果您正在編寫一個代理或者要從其他系統(tǒng)需要檢索數(shù)據(jù)的時候,這可能發(fā)生。后一種情況是常見的,因為許多 Netty 的應用程序集成現(xiàn)有系統(tǒng),例如 web 服務或數(shù)據(jù)庫。
你當然可以創(chuàng)建一個新的 Bootstrap 并使用它如9.2.1節(jié)所述,這個解決方案不一定有效。至少,你需要創(chuàng)建另一個 EventLoop 給新客戶端 Channel 的,并且 Channel 將會需要在不同的 Thread 間進行上下文切換。
幸運的是,由于 EventLoop 繼承自 EventLoopGroup ,您可以通過傳遞 接收到的 Channel 的 EventLoop 到 Bootstrap 的 group() 方法。這允許客戶端 Channel 來操作 相同的 EventLoop,這樣就能消除了額外的線程創(chuàng)建和所有相關的上下文切換的開銷。
為什么共享 EventLoop 呢?
當你分享一個 EventLoop ,你保證所有 Channel 分配給 EventLoop 將使用相同的線程,消除上下文切換和相關的開銷。(請記住,一個EventLoop分配給一個線程執(zhí)行操作。)
共享一個 EventLoop 描述如下:
Figure 9.4 EventLoop shared between channels with ServerBootstrap and Bootstrap
實現(xiàn) EventLoop 共享,包括設置 EventLoop 引導通過Bootstrap.eventLoop() 方法。這是清單9.5所示。
ServerBootstrap bootstrap = new ServerBootstrap(); //1
bootstrap.group(new NioEventLoopGroup(), //2
new NioEventLoopGroup()).channel(NioServerSocketChannel.class) //3
.childHandler( //4
new SimpleChannelInboundHandler<ByteBuf>() {
ChannelFuture connectFuture;
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
Bootstrap bootstrap = new Bootstrap();//5
bootstrap.channel(NioSocketChannel.class) //6
.handler(new SimpleChannelInboundHandler<ByteBuf>() { //7
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
System.out.println("Reveived data");
}
});
bootstrap.group(ctx.channel().eventLoop()); //8
connectFuture = bootstrap.connect(new InetSocketAddress("www.manning.com", 80)); //9
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
if (connectFuture.isDone()) {
// do something with the data //10
}
}
});
ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080)); //11
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if (channelFuture.isSuccess()) {
System.out.println("Server bound");
} else {
System.err.println("Bound attempt failed");
channelFuture.cause().printStackTrace();
}
}
});
注意,新的 EventLoop 會創(chuàng)建一個新的 Thread。出于該原因,EventLoop 實例應該盡量重用?;蛘呦拗茖嵗臄?shù)量來避免耗盡系統(tǒng)資源。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: