Package org.jboss.netty.channel.group
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 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
.
Broadcast a message to multiple 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));
Simplify shutdown process with 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()); } }
-
Method Summary
Modifier and TypeMethodDescriptionclose()
Closes allChannel
s in this group.Disconnects allChannel
s in this group from their remote peers.Returns theChannel
whose ID matches the specified integer.getName()
Returns the name of this group.setInterestOps
(int interestOps) setReadable
(boolean readable) CallsChannel.setReadable(boolean)
for allChannel
s in this group with the specified boolean flag.unbind()
Unbinds allChannel
s in this group from their local address.Writes the specifiedmessage
to allChannel
s in this group.write
(Object message, SocketAddress remoteAddress) Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream
Methods inherited from interface java.lang.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
Returns theChannel
whose ID matches the specified integer.- Returns:
- the matching
Channel
if found.null
otherwise.
-
setInterestOps
CallsChannel.setInterestOps(int)
for allChannel
s in this group with the specifiedinterestOps
. Please note that this operation is asynchronous asChannel.setInterestOps(int)
is.- Returns:
- the
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
setReadable
CallsChannel.setReadable(boolean)
for allChannel
s in this group with the specified boolean flag. Please note that this operation is asynchronous asChannel.setReadable(boolean)
is.- Returns:
- the
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
write
Writes the specifiedmessage
to allChannel
s in this group. If the specifiedmessage
is 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
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
write
Writes the specifiedmessage
with the specifiedremoteAddress
to allChannel
s in this group. If the specifiedmessage
is 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
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
disconnect
ChannelGroupFuture disconnect()Disconnects allChannel
s in this group from their remote peers.- Returns:
- the
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
unbind
ChannelGroupFuture unbind()Unbinds allChannel
s in this group from their local address.- Returns:
- the
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
close
ChannelGroupFuture close()Closes allChannel
s in this group. If theChannel
is connected to a remote peer or bound to a local address, it is automatically disconnected and unbound.- Returns:
- the
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-