public interface ChannelGroup extends java.util.Set<Channel>, java.lang.Comparable<ChannelGroup>
Set
that contains open Channel
s and provides
various bulk operations on them. Using ChannelGroup
, you can
categorize Channel
s 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
.
Channel
s
If you need to broadcast a message to more than one Channel
, you can
add the Channel
s associated with the recipients and call write(Object)
:
ChannelGroup
recipients = 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 ServerChannel
s and non-ServerChannel
s exist in the
same ChannelGroup
, any requested I/O operations on the group are
performed for the ServerChannel
s first and then for the others.
This rule is very useful when you shut down a server in one shot:
ChannelGroup
allChannels = newDefaultChannelGroup
(); public static void main(String[] args) throws Exception {ServerBootstrap
b = newServerBootstrap
(..); ... // 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 extendsSimpleChannelUpstreamHandler
{@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()); } }
限定符和类型 | 方法和说明 |
---|---|
ChannelGroupFuture |
close()
Closes all
Channel s in this group. |
ChannelGroupFuture |
disconnect()
Disconnects all
Channel s in this group from their remote peers. |
Channel |
find(java.lang.Integer id)
Returns the
Channel whose ID matches the specified integer. |
java.lang.String |
getName()
Returns the name of this group.
|
ChannelGroupFuture |
setInterestOps(int interestOps)
|
ChannelGroupFuture |
setReadable(boolean readable)
Calls
Channel.setReadable(boolean) for all Channel s in
this group with the specified boolean flag. |
ChannelGroupFuture |
unbind()
Unbinds all
Channel s in this group from their local address. |
ChannelGroupFuture |
write(java.lang.Object message)
Writes the specified
message to all Channel s in this
group. |
ChannelGroupFuture |
write(java.lang.Object message,
java.net.SocketAddress remoteAddress)
|
java.lang.String getName()
Channel find(java.lang.Integer id)
Channel
whose ID matches the specified integer.Channel
if found. null
otherwise.ChannelGroupFuture setInterestOps(int interestOps)
Channel.setInterestOps(int)
for all Channel
s 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 Channel
s 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(java.lang.Object message)
message
to all Channel
s 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(java.lang.Object message, java.net.SocketAddress remoteAddress)
message
with the specified
remoteAddress
to all Channel
s 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()
Channel
s in this group from their remote peers.ChannelGroupFuture
instance that notifies when
the operation is done for all channelsChannelGroupFuture unbind()
Channel
s in this group from their local address.ChannelGroupFuture
instance that notifies when
the operation is done for all channelsChannelGroupFuture close()
Channel
s 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 channels