ChannelGroup详解

1.介绍

ChannelGroup是一个线程安全的集合,它提供了打开一个Channel和不同批量的方法。可以使用ChannelGroup来讲Channel分类到一个有特别意义的组中。当组中的channel关闭时会自动从组中移除,因此我们不需要担心添加进去的channel的生命周期。一个channel可以属于多个ChannelGroup。

当一个ChannelGroup中即存在ServerChanel又存在non-ServerChannel时,执行IO操作时会优先执行ServerChannel中的方法。

2.方法说明

    //返回ChannelGroup的名称
    String name();

    //通过channelId查找相应的Channel
    Channel find(ChannelId id);
    
    //异步地将消息写到所有的Channel中
    ChannelGroupFuture write(Object message);

    ChannelGroupFuture write(Object message, ChannelMatcher matcher);

    ChannelGroupFuture write(Object message, ChannelMatcher matcher, boolean voidPromise);

    ChannelGroup flush();

    ChannelGroup flush(ChannelMatcher matcher);

    ChannelGroupFuture writeAndFlush(Object message);

    @Deprecated
    ChannelGroupFuture flushAndWrite(Object message);

    ChannelGroupFuture writeAndFlush(Object message, ChannelMatcher matcher);

    ChannelGroupFuture writeAndFlush(Object message, ChannelMatcher matcher, boolean voidPromise);

    @Deprecated
    ChannelGroupFuture flushAndWrite(Object message, ChannelMatcher matcher);

    //将所有通道与远程端断开连接
    ChannelGroupFuture disconnect();

    ChannelGroupFuture disconnect(ChannelMatcher matcher);

    //关闭所有的通道
    ChannelGroupFuture close();

    ChannelGroupFuture close(ChannelMatcher matcher);

    @Deprecated
    ChannelGroupFuture deregister();

    @Deprecated
    ChannelGroupFuture deregister(ChannelMatcher matcher);

    //返回一个ChannelGroupFuture,当所有的channel关闭时,会得到相应的通知
    ChannelGroupFuture newCloseFuture();

    ChannelGroupFuture newCloseFuture(ChannelMatcher matcher);

3.示例

  public class MyHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    //channel对象数组
    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        System.out.println("channelRead0执行了");
        Channel channel = ctx.channel();

        channelGroup.forEach(ch -> {
            if(channel != ch){
                ch.writeAndFlush(new TextWebSocketFrame(channel.remoteAddress() + " 发送的消息 :"+msg.text()));
            }else{
                ch.writeAndFlush(new TextWebSocketFrame("[自己] "+msg.text()));
            }
        });
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println("handlerAdded执行了");
        Channel channel = ctx.channel();

        //广播消息
        channelGroup.writeAndFlush(new TextWebSocketFrame("[服务器] - "+channel.remoteAddress()+"加入"));
        channelGroup.add(channel);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        channelGroup.writeAndFlush(new TextWebSocketFrame("[服务器] -"+ channel.remoteAddress() +"离开"));
    }


    //该方法发出的消息,自身channel还未形成所以不能收到消息
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        channelGroup.writeAndFlush(new TextWebSocketFrame(channel.remoteAddress() + "上线了"));
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        channelGroup.writeAndFlush(new TextWebSocketFrame(channel.remoteAddress() + "下线了"));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("异常发生");
        ctx.close();
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,161评论 1 32
  • Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java I...
    JackChen1024阅读 7,615评论 1 143
  • 必备的理论基础 1.操作系统作用: 隐藏丑陋复杂的硬件接口,提供良好的抽象接口。 管理调度进程,并将多个进程对硬件...
    drfung阅读 3,607评论 0 5
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,875评论 0 10
  • Chapter 8 Goroutines and Channels Go enable two styles of...
    SongLiang阅读 1,641评论 0 3