Netty ChannelGroup

io.netty.channel.group.ChannelGroup

A thread-safe Set
that contains open Channels
and provides various bulk operations on them. Using ChannelGroup,
you can categorize Channels
into a meaningful group (e.g. on a per-service or per-state basis.) A closed
Channel
is automatically removed from the collection, so that you don‘t need to worry
about the life cycle of the added Channel.
A Channel
can belong to more than one ChannelGroup.

Broadcast a message to multiple Channels

If you need to broadcast a message to more than one Channel,
you can add the Channels
associated with the recipients and call ChannelGroup.write(Object):

 ChannelGroup recipients =
         new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
 recipients.add(channelA);
 recipients.add(channelB);
 ..
 recipients.write(Unpooled.copiedBuffer(
         "Service will shut down for maintenance in 5 minutes.",
         CharsetUtil.UTF_8));
 

Simplify shutdown process with ChannelGroup

If both ServerChannels and non-ServerChannels exist in the same ChannelGroup, any requested I/O operations on the group are performed for the ServerChannels first and then for the others.

This rule is very useful when you shut down a server in one shot:

 ChannelGroup allChannels =
         new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

 public static void main(String[] args) throws Exception {
     ServerBootstrap b = new ServerBootstrap(..);
     ...
     b.childHandler(new MyHandler());

     // Start the server
     b.getPipeline().addLast("handler", new MyHandler());
     Channel serverChannel = b.bind(..).sync();
     allChannels.add(serverChannel);

     ... Wait until the shutdown signal reception ...

     // Close the serverChannel and then all accepted connections.
     allChannels.close().awaitUninterruptibly();
 }

 public class MyHandler extends ChannelInboundHandlerAdapter {
     @Override
     public void channelActive(ChannelHandlerContext ctx) {
         // closed on shutdown.
         allChannels.add(ctx.channel());
         super.channelActive(ctx);
     }
 }
时间: 2024-10-29 19:11:09

Netty ChannelGroup的相关文章

Netty利用ChannelGroup广播消息

在Netty中提供了ChannelGroup接口,该接口继承Set接口,因此可以通过ChannelGroup可管理服务器端所有的连接的Channel,然后对所有的连接Channel广播消息. Server端: public class BroadCastServer { public static void run(int port) { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioE

Netty Channel 接口名词理解

1.Channel channel 是负责数据读,写的对象,有点类似于老的io里面的stream.它和stream的区别,channel是双向的,既可以write 也可以read,而stream要分outstream和inputstream.而且在NIO中用户不应该直接从channel中读写数据,而是应该通过buffer,通过buffer再将数据读写到channel中. 一个channel 可以提供给用户下面几个信息 (1)channel的当前状态,比如open 还是closed (2)Chan

netty Getting Started--reference

reference from:http://docs.jboss.org/netty/3.1/guide/html/start.html 1.1. Before Getting Started 1.2. Writing a Discard Server 1.3. Looking into the Received Data 1.4. Writing an Echo Server 1.5. Writing a Time Server 1.6. Writing a Time Client 1.7.

基于Netty与RabbitMQ的消息服务

Netty作为一个高性能的异步网络开发框架,可以作为各种服务的开发框架. 前段时间的一个项目涉及到硬件设备实时数据的采集,采用Netty作为采集服务的实现框架,同时使用RabbitMQ作为采集服务和各个其他模块的通信消息队列,整个服务框架图如下: 将业务代码和实际协议解析部分的代码抽离,得到以上一个简单的设计图,代码开源在GitHub上,简单介绍下NettyMQServer采集服务涉及到的几个关键技术点: 1.设备TCP消息解析: NettyMQServer和采集设备Device之间采用TCP通

netty解决channel管理,可广播消息

在Netty中提供了ChannelGroup接口,该接口继承Set接口,因此可以通过ChannelGroup可管理服务器端所有的连接的Channel,然后对所有的连接Channel广播消息. Server端: public class BroadCastServer { public static void run(int port) { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioE

Netty 实现 WebSocket 聊天功能

准备 JDK 7+ Maven 3.2.x Netty 4.x Eclipse 4.x WebSocket WebSocket 通过"Upgrade handshake(升级握手)"从标准的 HTTP 或HTTPS 协议转为 WebSocket.因此,使用 WebSocket 的应用程序将始终以 HTTP/S 开始,然后进行升级.在什么时候发生这种情况取决于具体的应用;它可以是在启动时,或当一个特定的 URL 被请求时. 在我们的应用中,当 URL 请求以"/ws"

Netty 实现聊天功能

Netty 是一个 Java NIO 客户端服务器框架,使用它可以快速简单地开发网络应用程序,比如服务器和客户端的协议.Netty 大大简化了网络程序的开发过程比如 TCP 和 UDP 的 socket 服务的开发.更多关于 Netty 的知识,可以参阅<Netty 4.x 用户指南>(https://github.com/waylau/netty-4-user-guide) 下面,就基于 Netty 快速实现一个聊天小程序. 准备 JDK 7+ Maven 3.2.x Netty 4.x E

netty websocket协议开发

websocket的好处我们就不用多说了,就是用于解决长连接.服务推送等需要的一种技术. 以下我们来看一个例子: 1 package com.ming.netty.http.websocket; 2 3 import java.net.InetSocketAddress; 4 5 import io.netty.bootstrap.ServerBootstrap; 6 import io.netty.channel.ChannelFuture; 7 import io.netty.channel

Netty入门实例及分析

什么是netty?以下是官方文档的简单介绍: The Netty project  is an effort to provide an asynchronous event-driven network application framework and tools for rapid development of maintainable high performance and high scalability protocol servers and clients. In other