Interface ChannelGroup
- All Superinterfaces:
Collection<Channel>, Comparable<ChannelGroup>, Iterable<Channel>, Set<Channel>
- All Known Implementing Classes:
DefaultChannelGroup
A thread-safe Broadcast a message to multiple
Simplify shutdown process with
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.
Broadcast a message to multiple 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));
Simplify shutdown process with 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); } }
-
Method Summary
Modifier and TypeMethodDescriptionclose()Closes allChannels in this group.close(ChannelMatcher matcher) Closes allChannels in this group that are matched by the givenChannelMatcher.Deprecated.This method will be removed in the next major feature release.deregister(ChannelMatcher matcher) Deprecated.This method will be removed in the next major feature release.Disconnects allChannels in this group from their remote peers.disconnect(ChannelMatcher matcher) Disconnects allChannels in this group from their remote peers, that are matched by the givenChannelMatcher.flush()Flush allChannels in this group.flush(ChannelMatcher matcher) Flush allChannels in this group that are matched by the givenChannelMatcher.flushAndWrite(Object message) Deprecated.UsewriteAndFlush(Object)instead.flushAndWrite(Object message, ChannelMatcher matcher) Deprecated.UsewriteAndFlush(Object, ChannelMatcher)instead.name()Returns the name of this group.Returns theChannelGroupFuturewhich will be notified when allChannels that are part of thisChannelGroup, at the time of calling, are closed.newCloseFuture(ChannelMatcher matcher) Returns theChannelGroupFuturewhich will be notified when allChannels that are part of thisChannelGroup, at the time of calling, are closed.Writes the specifiedmessageto allChannels in this group.write(Object message, ChannelMatcher matcher) Writes the specifiedmessageto allChannels in this group that are matched by the givenChannelMatcher.write(Object message, ChannelMatcher matcher, boolean voidPromise) Writes the specifiedmessageto allChannels in this group that are matched by the givenChannelMatcher.writeAndFlush(Object message) Shortcut for callingwrite(Object)andflush().writeAndFlush(Object message, ChannelMatcher matcher) Shortcut for callingwrite(Object)andflush()and only act onChannels that are matched by theChannelMatcher.writeAndFlush(Object message, ChannelMatcher matcher, boolean voidPromise) Shortcut for callingwrite(Object, ChannelMatcher, boolean)andflush()and only act onChannels that are matched by theChannelMatcher.Methods inherited from interface Collection
parallelStream, removeIf, streamMethods inherited from interface Comparable
compareTo
-
Method Details
-
name
String name()Returns the name of this group. A group name is purely for helping you to distinguish one group from others. -
find
-
write
Writes the specifiedmessageto allChannels in this group. If the specifiedmessageis an instance ofByteBuf, it is automatically duplicated to avoid a race condition. The same is true forByteBufHolder. Please note that this operation is asynchronous asChannel.write(Object)is.- Returns:
- itself
-
write
Writes the specifiedmessageto allChannels in this group that are matched by the givenChannelMatcher. If the specifiedmessageis an instance ofByteBuf, it is automatically duplicated to avoid a race condition. The same is true forByteBufHolder. Please note that this operation is asynchronous asChannel.write(Object)is.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
write
Writes the specifiedmessageto allChannels in this group that are matched by the givenChannelMatcher. If the specifiedmessageis an instance ofByteBuf, it is automatically duplicated to avoid a race condition. The same is true forByteBufHolder. Please note that this operation is asynchronous asChannel.write(Object)is. IfvoidPromiseistrueChannel.voidPromise()is used for the writes and so the same restrictions to the returnedChannelGroupFutureapply as to a void promise.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
flush
ChannelGroup flush()Flush allChannels in this group. If the specifiedmessagesare an instance ofByteBuf, it is automatically duplicated to avoid a race condition. Please note that this operation is asynchronous asChannel.write(Object)is.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
flush
Flush allChannels in this group that are matched by the givenChannelMatcher. If the specifiedmessagesare an instance ofByteBuf, it is automatically duplicated to avoid a race condition. Please note that this operation is asynchronous asChannel.write(Object)is.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
writeAndFlush
Shortcut for callingwrite(Object)andflush(). -
flushAndWrite
Deprecated.UsewriteAndFlush(Object)instead. -
writeAndFlush
Shortcut for callingwrite(Object)andflush()and only act onChannels that are matched by theChannelMatcher. -
writeAndFlush
Shortcut for callingwrite(Object, ChannelMatcher, boolean)andflush()and only act onChannels that are matched by theChannelMatcher. -
flushAndWrite
Deprecated.UsewriteAndFlush(Object, ChannelMatcher)instead. -
disconnect
ChannelGroupFuture disconnect()Disconnects allChannels in this group from their remote peers.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
disconnect
Disconnects allChannels in this group from their remote peers, that are matched by the givenChannelMatcher.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
close
ChannelGroupFuture close()Closes allChannels in this group. If theChannelis connected to a remote peer or bound to a local address, it is automatically disconnected and unbound.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
close
Closes allChannels in this group that are matched by the givenChannelMatcher. If theChannelis connected to a remote peer or bound to a local address, it is automatically disconnected and unbound.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
deregister
Deprecated.This method will be removed in the next major feature release. Deregister allChannels in this group from theirEventLoop. Please note that this operation is asynchronous asChannel.deregister()is.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
deregister
Deprecated.This method will be removed in the next major feature release. Deregister allChannels in this group from theirEventLoopthat are matched by the givenChannelMatcher. Please note that this operation is asynchronous asChannel.deregister()is.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
newCloseFuture
ChannelGroupFuture newCloseFuture()Returns theChannelGroupFuturewhich will be notified when allChannels that are part of thisChannelGroup, at the time of calling, are closed. -
newCloseFuture
Returns theChannelGroupFuturewhich will be notified when allChannels that are part of thisChannelGroup, at the time of calling, are closed.
-