netty获取玩家chanel_netty channelgroup的广播信息,channel怎样获取

Netty是一个高效的Java NIO网络框架,用于构建消息平台。优雅关闭Netty涉及unbind、close所有channel及shutdown线程执行器。通过ChannelGroup管理channel,方便关闭。具体步骤包括:解除所有channel绑定,关闭所有channel,以及关闭线程执行器。示例代码展示了如何在ChannelGroup中添加并关闭channel。
摘要由CSDN通过智能技术生成

懂得

1级

2017-05-14 回答

netty是一个java nio的网络框架,它屏蔽了底层网络细节,并且非常的高效。如果你是最近要开发一个消息平台,使用netty最好不过了。

一个好的消息平台有很多需要注意的细节和应该遵守的约定准则。其中平台的优雅关闭必不可少。这个主要是避免消息丢失。那么如何做到netty的优雅关闭呢?

在netty中,接受连接请求和对请求进行业务处理分别有两个线程执行器bossexecutor 和 workerexecutor,除了关闭这两个外还需要关闭channel。

netty文档说优雅关闭需要三步:

1. unbind netty创建的所有channel。channel.unbind()

2. close netty创建的所有channel。channel.close()

3. shutdown netty的线程执行器。factory.releaseexternalresources()

对于netty生成的channel,可以使用channelgroup管理,很方便。

具体的代码如下:(可以参看channelgroup的注释)

[java] view plain copy

channelgroup allchannels = new defaultchannelgroup();

public static void main(string[] args) throws exception {

serverbootstrap b = new serverbootstrap(..);

...

// start the server

b.getpipeline().addlast("handler", new myhandler());

channel serverchannel = b.bind(..);

allchannels.add(serverchannel);

... wait until the shutdown signal reception ...

// close the serverchannel and then all accepted connections.

allchannels.close().awaituninterruptibly();

b.releaseexternalresources();

}

public class myhandler extends simplechannelupstreamhandler {

@override

public void channelopen(channelhandlercontext ctx, channelstateevent e) {

// add all open channels to the global group so that they are

// closed on shutdown.

allchannels.add(e.getchannel());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值