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(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 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(GlobalEventExecutor.INSTANCE); public static void main(String[] args) throws Exception {ServerBootstrapb = newServerBootstrap(..); ... b.childHandler(new MyHandler()); // Start the server b.getPipeline().addLast("handler", new MyHandler());ChannelserverChannel = 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 extendsChannelInboundHandlerAdapter{@Overridepublic void channelActive(ChannelHandlerContextctx) { // closed on shutdown. allChannels.add(ctx.channel()); super.channelActive(ctx); } }
| Modifier and Type | Method and Description |
|---|---|
ChannelGroupFuture |
close()
Closes all
Channels in this group. |
ChannelGroupFuture |
close(ChannelMatcher matcher)
Closes all
Channels in this group that are matched by the given ChannelMatcher. |
ChannelGroupFuture |
deregister()
Deprecated.
This method will be removed in the next major feature release.
Deregister all
Channels in this group from their EventLoop.
Please note that this operation is asynchronous as ChannelOutboundInvoker.deregister() is. |
ChannelGroupFuture |
deregister(ChannelMatcher matcher)
Deprecated.
This method will be removed in the next major feature release.
Deregister all
Channels in this group from their EventLoop that are matched by the given
ChannelMatcher. Please note that this operation is asynchronous as ChannelOutboundInvoker.deregister() is. |
ChannelGroupFuture |
disconnect()
Disconnects all
Channels in this group from their remote peers. |
ChannelGroupFuture |
disconnect(ChannelMatcher matcher)
Disconnects all
Channels in this group from their remote peers,
that are matched by the given ChannelMatcher. |
Channel |
find(ChannelId id)
|
ChannelGroup |
flush()
Flush all
Channels in this
group. |
ChannelGroup |
flush(ChannelMatcher matcher)
Flush all
Channels in this group that are matched by the given ChannelMatcher. |
ChannelGroupFuture |
flushAndWrite(Object message)
Deprecated.
Use
writeAndFlush(Object) instead. |
ChannelGroupFuture |
flushAndWrite(Object message,
ChannelMatcher matcher)
Deprecated.
Use
writeAndFlush(Object, ChannelMatcher) instead. |
String |
name()
Returns the name of this group.
|
ChannelGroupFuture |
newCloseFuture()
Returns the
ChannelGroupFuture which will be notified when all Channels that are part of this
ChannelGroup, at the time of calling, are closed. |
ChannelGroupFuture |
newCloseFuture(ChannelMatcher matcher)
Returns the
ChannelGroupFuture which will be notified when all Channels that are part of this
ChannelGroup, at the time of calling, are closed. |
ChannelGroupFuture |
write(Object message)
Writes the specified
message to all Channels in this
group. |
ChannelGroupFuture |
write(Object message,
ChannelMatcher matcher)
Writes the specified
message to all Channels in this
group that are matched by the given ChannelMatcher. |
ChannelGroupFuture |
write(Object message,
ChannelMatcher matcher,
boolean voidPromise)
Writes the specified
message to all Channels in this
group that are matched by the given ChannelMatcher. |
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
Channels that are matched by the ChannelMatcher. |
ChannelGroupFuture |
writeAndFlush(Object message,
ChannelMatcher matcher,
boolean voidPromise)
Shortcut for calling
write(Object, ChannelMatcher, boolean) and flush() and only act on
Channels that are matched by the ChannelMatcher. |
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, spliterator, toArray, toArrayparallelStream, removeIf, streamcompareToString name()
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. The same is true for ByteBufHolder. Please note that this operation is asynchronous as
ChannelOutboundInvoker.write(Object) is.ChannelGroupFuture write(Object message, ChannelMatcher matcher)
message to all Channels in this
group that are matched by 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
ChannelOutboundInvoker.write(Object) is.ChannelGroupFuture instance that notifies when
the operation is done for all channelsChannelGroupFuture write(Object message, ChannelMatcher matcher, boolean voidPromise)
message to all Channels in this
group that are matched by 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
ChannelOutboundInvoker.write(Object) is.
If voidPromise is true ChannelOutboundInvoker.voidPromise() is used for the writes and so the same
restrictions to the returned ChannelGroupFuture apply as to a void promise.ChannelGroupFuture instance that notifies when
the operation is done for all channelsChannelGroup flush()
Channels 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
ChannelOutboundInvoker.write(Object) is.ChannelGroupFuture instance that notifies when
the operation is done for all channelsChannelGroup flush(ChannelMatcher matcher)
Channels in this group that are matched by 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
ChannelOutboundInvoker.write(Object) is.ChannelGroupFuture instance that notifies when
the operation is done for all channelsChannelGroupFuture writeAndFlush(Object message)
write(Object) and flush().@Deprecated ChannelGroupFuture flushAndWrite(Object message)
writeAndFlush(Object) instead.ChannelGroupFuture writeAndFlush(Object message, ChannelMatcher matcher)
write(Object) and flush() and only act on
Channels that are matched by the ChannelMatcher.ChannelGroupFuture writeAndFlush(Object message, ChannelMatcher matcher, boolean voidPromise)
write(Object, ChannelMatcher, boolean) and flush() and only act on
Channels that are matched by the ChannelMatcher.@Deprecated ChannelGroupFuture flushAndWrite(Object message, ChannelMatcher matcher)
writeAndFlush(Object, ChannelMatcher) instead.ChannelGroupFuture disconnect()
Channels in this group from their remote peers.ChannelGroupFuture instance that notifies when
the operation is done for all channelsChannelGroupFuture disconnect(ChannelMatcher matcher)
Channels in this group from their remote peers,
that are matched by the given ChannelMatcher.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 channelsChannelGroupFuture close(ChannelMatcher matcher)
Channels in this group that are matched by 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 channels@Deprecated ChannelGroupFuture deregister()
Channels in this group from their EventLoop.
Please note that this operation is asynchronous as ChannelOutboundInvoker.deregister() is.ChannelGroupFuture instance that notifies when
the operation is done for all channels@Deprecated ChannelGroupFuture deregister(ChannelMatcher matcher)
Channels in this group from their EventLoop that are matched by the given
ChannelMatcher. Please note that this operation is asynchronous as ChannelOutboundInvoker.deregister() is.ChannelGroupFuture instance that notifies when
the operation is done for all channelsChannelGroupFuture newCloseFuture()
ChannelGroupFuture which will be notified when all Channels that are part of this
ChannelGroup, at the time of calling, are closed.ChannelGroupFuture newCloseFuture(ChannelMatcher matcher)
ChannelGroupFuture which will be notified when all Channels that are part of this
ChannelGroup, at the time of calling, are closed.Copyright © 2008–2025 The Netty Project. All rights reserved.