Package org.jboss.netty.channel.group
Interface ChannelGroup
-
- All Superinterfaces:
Collection<Channel>
,Comparable<ChannelGroup>
,Iterable<Channel>
,Set<Channel>
- All Known Implementing Classes:
DefaultChannelGroup
public interface ChannelGroup extends Set<Channel>, Comparable<ChannelGroup>
A thread-safeSet
that contains openChannel
s and provides various bulk operations on them. UsingChannelGroup
, you can categorizeChannel
s into a meaningful group (e.g. on a per-service or per-state basis.) A closedChannel
is automatically removed from the collection, so that you don't need to worry about the life cycle of the addedChannel
. AChannel
can belong to more than oneChannelGroup
.Broadcast a message to multiple
Channel
sIf you need to broadcast a message to more than one
Channel
, you can add theChannel
s associated with the recipients and callwrite(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 sameChannelGroup
, any requested I/O operations on the group are performed for theServerChannel
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
All Methods Instance Methods Abstract Methods Modifier and Type Method Description ChannelGroupFuture
close()
Closes allChannel
s in this group.ChannelGroupFuture
disconnect()
Disconnects allChannel
s in this group from their remote peers.Channel
find(Integer id)
Returns theChannel
whose ID matches the specified integer.String
getName()
Returns the name of this group.ChannelGroupFuture
setInterestOps(int interestOps)
ChannelGroupFuture
setReadable(boolean readable)
CallsChannel.setReadable(boolean)
for allChannel
s in this group with the specified boolean flag.ChannelGroupFuture
unbind()
Unbinds allChannel
s in this group from their local address.ChannelGroupFuture
write(Object message)
Writes the specifiedmessage
to allChannel
s in this group.ChannelGroupFuture
write(Object message, SocketAddress remoteAddress)
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.lang.Comparable
compareTo
-
-
-
-
Method Detail
-
getName
String getName()
Returns the name of this group. A group name is purely for helping you to distinguish one group from others.
-
find
Channel find(Integer id)
Returns theChannel
whose ID matches the specified integer.- Returns:
- the matching
Channel
if found.null
otherwise.
-
setInterestOps
ChannelGroupFuture setInterestOps(int interestOps)
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
ChannelGroupFuture setReadable(boolean readable)
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
ChannelGroupFuture write(Object message)
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
ChannelGroupFuture write(Object message, SocketAddress remoteAddress)
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
-
-