Package org.jboss.netty.channel.group
Interface ChannelGroup
-
- All Superinterfaces:
java.util.Collection<Channel>,java.lang.Comparable<ChannelGroup>,java.lang.Iterable<Channel>,java.util.Set<Channel>
- All Known Implementing Classes:
DefaultChannelGroup
public interface ChannelGroup extends java.util.Set<Channel>, java.lang.Comparable<ChannelGroup>
A thread-safeSetthat contains openChannels and provides various bulk operations on them. UsingChannelGroup, you can categorizeChannels into a meaningful group (e.g. on a per-service or per-state basis.) A closedChannelis automatically removed from the collection, so that you don't need to worry about the life cycle of the addedChannel. AChannelcan belong to more than oneChannelGroup.Broadcast a message to multiple
ChannelsIf you need to broadcast a message to more than one
Channel, you can add theChannels associated with the recipients and callwrite(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
ChannelGroupIf both
ServerChannels and non-ServerChannels exist in the sameChannelGroup, any requested I/O operations on the group are performed for theServerChannels 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
All Methods Instance Methods Abstract Methods Modifier and Type Method Description ChannelGroupFutureclose()Closes allChannels in this group.ChannelGroupFuturedisconnect()Disconnects allChannels in this group from their remote peers.Channelfind(java.lang.Integer id)Returns theChannelwhose ID matches the specified integer.java.lang.StringgetName()Returns the name of this group.ChannelGroupFuturesetInterestOps(int interestOps)ChannelGroupFuturesetReadable(boolean readable)CallsChannel.setReadable(boolean)for allChannels in this group with the specified boolean flag.ChannelGroupFutureunbind()Unbinds allChannels in this group from their local address.ChannelGroupFuturewrite(java.lang.Object message)Writes the specifiedmessageto allChannels in this group.ChannelGroupFuturewrite(java.lang.Object message, java.net.SocketAddress remoteAddress)
-
-
-
Method Detail
-
getName
java.lang.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(java.lang.Integer id)
Returns theChannelwhose ID matches the specified integer.- Returns:
- the matching
Channelif found.nullotherwise.
-
setInterestOps
ChannelGroupFuture setInterestOps(int interestOps)
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
ChannelGroupFuture setReadable(boolean readable)
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
ChannelGroupFuture write(java.lang.Object message)
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
ChannelGroupFuture write(java.lang.Object message, java.net.SocketAddress remoteAddress)
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
-
-