public interface ChannelGroup extends Set<Channel>, 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
(GlobalEventExecutor
.INSTANCE); 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 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
(GlobalEventExecutor
.INSTANCE); public static void main(String[] args) throws Exception {ServerBootstrap
b = newServerBootstrap
(..); ... 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 extendsChannelHandlerAdapter
{@Override
public void channelActive(ChannelHandlerContext
ctx) { // closed on shutdown. allChannels.add(ctx.channel()); super.channelActive(ctx); } }
Modifier and Type | Method and Description |
---|---|
ChannelGroupFuture |
close()
Closes all
Channel s in this group. |
ChannelGroupFuture |
close(ChannelMatcher matcher)
Closes all
Channel s in this group that match the given ChannelMatcher . |
ChannelGroupFuture |
deregister()
|
ChannelGroupFuture |
deregister(ChannelMatcher matcher)
|
ChannelGroupFuture |
disconnect()
Disconnects all
Channel s in this group from their remote peers. |
ChannelGroupFuture |
disconnect(ChannelMatcher matcher)
Disconnects all
Channel s in this group from their remote peers,
that match the given ChannelMatcher . |
Channel |
find(ChannelId id)
|
ChannelGroup |
flush()
Flush all
Channel s in this
group. |
ChannelGroup |
flush(ChannelMatcher matcher)
Flush all
Channel s in this group that match the given ChannelMatcher . |
String |
name()
Returns the name of this group.
|
ChannelGroupFuture |
write(Object message)
Writes the specified
message to all Channel s in this
group. |
ChannelGroupFuture |
write(Object message,
ChannelMatcher matcher)
|
ChannelGroupFuture |
writeAndFlush(Object message)
Shortcut for calling
write(Object) and flush() . |
ChannelGroupFuture |
writeAndFlush(Object message,
ChannelMatcher matcher)
Shortcut for calling
write(Object) and flush() and only act on
Channel s that match the ChannelMatcher . |
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
compareTo
String name()
ChannelGroupFuture write(Object message)
message
to all Channel
s in this
group. If the specified message
is an instance of
ByteBuf
, it is automatically
duplicated to avoid a race
condition. The same is true for ByteBufHolder
. Please note that this operation is asynchronous as
Channel.write(Object)
is.ChannelGroupFuture write(Object message, ChannelMatcher matcher)
message
to all Channel
s in this
group that match the given ChannelMatcher
. If the specified message
is an instance of
ByteBuf
, it is automatically
duplicated to avoid a race
condition. The same is true for ByteBufHolder
. Please note that this operation is asynchronous as
Channel.write(Object)
is.ChannelGroupFuture
instance that notifies when
the operation is done for all channelsChannelGroup flush()
Channel
s in this
group. If the specified messages
are an instance of
ByteBuf
, 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 channelsChannelGroup flush(ChannelMatcher matcher)
Channel
s in this group that match the given ChannelMatcher
.
If the specified messages
are an instance of
ByteBuf
, 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 writeAndFlush(Object message)
write(Object)
and flush()
.ChannelGroupFuture writeAndFlush(Object message, ChannelMatcher matcher)
write(Object)
and flush()
and only act on
Channel
s that match the ChannelMatcher
.ChannelGroupFuture disconnect()
Channel
s in this group from their remote peers.ChannelGroupFuture
instance that notifies when
the operation is done for all channelsChannelGroupFuture disconnect(ChannelMatcher matcher)
Channel
s in this group from their remote peers,
that match the given ChannelMatcher
.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 channelsChannelGroupFuture close(ChannelMatcher matcher)
Channel
s in this group that match the given ChannelMatcher
.
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 channelsChannelGroupFuture deregister()
Channel
s in this group from their EventLoop
.
Please note that this operation is asynchronous as Channel.deregister()
is.ChannelGroupFuture
instance that notifies when
the operation is done for all channelsChannelGroupFuture deregister(ChannelMatcher matcher)
Channel
s in this group from their EventLoop
that match the given
ChannelMatcher
. Please note that this operation is asynchronous as Channel.deregister()
is.ChannelGroupFuture
instance that notifies when
the operation is done for all channelsCopyright © 2008–2015 The Netty Project. All rights reserved.