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(); recipients.add(channelA); recipients.add(channelB); .. recipients.write(ChannelBuffers.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(); 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()); } }
-
Method Summary
Modifier and TypeMethodDescriptionclose()Closes allChannels in this group.Disconnects allChannels in this group from their remote peers.Returns theChannelwhose ID matches the specified integer.getName()Returns the name of this group.setInterestOps(int interestOps) setReadable(boolean readable) CallsChannel.setReadable(boolean)for allChannels in this group with the specified boolean flag.unbind()Unbinds allChannels in this group from their local address.Writes the specifiedmessageto allChannels in this group.write(Object message, SocketAddress remoteAddress) Methods inherited from interface Collection
parallelStream, removeIf, streamMethods inherited from interface Comparable
compareTo
-
Method Details
-
getName
String getName()Returns the name of this group. A group name is purely for helping you to distinguish one group from others. -
find
-
setInterestOps
CallsChannel.setInterestOps(int)for allChannels in this group with the specifiedinterestOps. Please note that this operation is asynchronous asChannel.setInterestOps(int)is.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
setReadable
CallsChannel.setReadable(boolean)for allChannels in this group with the specified boolean flag. Please note that this operation is asynchronous asChannel.setReadable(boolean)is.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
write
Writes the specifiedmessageto allChannels in this group. If the specifiedmessageis an instance ofChannelBuffer, 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
-
write
Writes the specifiedmessagewith the specifiedremoteAddressto allChannels in this group. If the specifiedmessageis an instance ofChannelBuffer, it is automatically duplicated to avoid a race condition. Please note that this operation is asynchronous asChannel.write(Object, SocketAddress)is.- Returns:
- the
ChannelGroupFutureinstance that notifies when the operation is done for all channels
-
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
-
unbind
ChannelGroupFuture unbind()Unbinds allChannels in this group from their local address.- 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
-