
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(ChannelBuffers.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 |
getName()
Returns the name of this group.
|
ChannelGroupFuture |
setInterestOps(int interestOps)
|
ChannelGroupFuture |
setReadable(boolean readable)
Calls
Channel.setReadable(boolean) for all Channels in
this group with the specified boolean flag. |
ChannelGroupFuture |
unbind()
Unbinds all
Channels in this group from their local address. |
ChannelGroupFuture |
write(Object message)
Writes the specified
message to all Channels in this
group. |
ChannelGroupFuture |
write(Object message,
SocketAddress remoteAddress)
|
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArraycompareToString getName()
Channel find(Integer id)
Channel whose ID matches the specified integer.Channel if found. null otherwise.ChannelGroupFuture setInterestOps(int interestOps)
Channel.setInterestOps(int) for all Channels in
this group with the specified interestOps. Please note that
this operation is asynchronous as Channel.setInterestOps(int) is.ChannelGroupFuture instance that notifies when
the operation is done for all channelsChannelGroupFuture setReadable(boolean readable)
Channel.setReadable(boolean) for all Channels in
this group with the specified boolean flag. Please note that this
operation is asynchronous as Channel.setReadable(boolean) is.ChannelGroupFuture instance that notifies when
the operation is done for all channelsChannelGroupFuture write(Object message)
message to all Channels in this
group. If the specified message is an instance of
ChannelBuffer, it is automatically
duplicated to avoid a race
condition. Please note that this operation is asynchronous as
Channel.write(Object) is.ChannelGroupFuture instance that notifies when
the operation is done for all channelsChannelGroupFuture write(Object message, SocketAddress remoteAddress)
message with the specified
remoteAddress to all Channels in this group. If the
specified message is an instance of ChannelBuffer, it is
automatically duplicated to avoid
a race condition. Please note that this operation is asynchronous as
Channel.write(Object, SocketAddress) 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 unbind()
Channels in this group from their local address.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-2013 The Netty Project. All Rights Reserved.