public interface ChannelGroup extends Set<Channel>, Comparable<ChannelGroup>
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.
Channels
If you need to broadcast a message to more than one Channel, you can
add the Channels associated with the recipients and call write(Object):
ChannelGrouprecipients = newDefaultChannelGroup(); recipients.add(channelA); recipients.add(channelB); .. recipients.write(Unpooled.copiedBuffer( "Service will shut down for maintenance in 5 minutes.",CharsetUtil.UTF_8));
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:
ChannelGroupallChannels = newDefaultChannelGroup(); public static void main(String[] args) throws Exception {ServerBootstrapb = newServerBootstrap(..); ... // Start the server b.getPipeline().addLast("handler", new MyHandler());ChannelserverChannel = 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 extendsSimpleChannelUpstreamHandler{@Overridepublic void channelOpen(ChannelHandlerContextctx,ChannelStateEvente) { // Add all open channels to the global group so that they are // closed on shutdown. allChannels.add(e.getChannel()); } }
| Modifier and Type | Method and Description |
|---|---|
ChannelGroupFuture |
close()
Closes all
Channels in this group. |
ChannelGroupFuture |
disconnect()
Disconnects all
Channels in this group from their remote peers. |
Channel |
find(Integer id)
Returns the
Channel whose ID matches the specified integer. |
String |
name()
Returns the name of this group.
|
ChannelGroupFuture |
write(Object message)
Writes the specified
message to all Channels in this
group. |
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArraycompareToString name()
Channel find(Integer id)
Channel whose ID matches the specified integer.Channel if found. null otherwise.ChannelGroupFuture write(Object message)
message to all Channels in this
group. If the specified message is an instance of
ByteBuf, it is automatically
duplicated to avoid a race
condition. Please note that this operation is asynchronous as
ChannelOutboundInvoker.write(Object) is.ChannelGroupFuture instance that notifies when
the operation is done for all channelsChannelGroupFuture disconnect()
Channels in this group from their remote peers.ChannelGroupFuture instance that notifies when
the operation is done for all channelsChannelGroupFuture close()
Channels in this group. If the Channel is
connected to a remote peer or bound to a local address, it is
automatically disconnected and unbound.ChannelGroupFuture instance that notifies when
the operation is done for all channelsCopyright © 2008-2012 The Netty Project. All Rights Reserved.