Package io.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(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
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(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
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description ChannelGroupFutureclose()Closes allChannels in this group.ChannelGroupFutureclose(ChannelMatcher matcher)Closes allChannels in this group that are matched by the givenChannelMatcher.ChannelGroupFuturederegister()Deprecated.This method will be removed in the next major feature release.ChannelGroupFuturederegister(ChannelMatcher matcher)Deprecated.This method will be removed in the next major feature release.ChannelGroupFuturedisconnect()Disconnects allChannels in this group from their remote peers.ChannelGroupFuturedisconnect(ChannelMatcher matcher)Disconnects allChannels in this group from their remote peers, that are matched by the givenChannelMatcher.Channelfind(ChannelId id)ChannelGroupflush()Flush allChannels in this group.ChannelGroupflush(ChannelMatcher matcher)Flush allChannels in this group that are matched by the givenChannelMatcher.ChannelGroupFutureflushAndWrite(java.lang.Object message)Deprecated.UsewriteAndFlush(Object)instead.ChannelGroupFutureflushAndWrite(java.lang.Object message, ChannelMatcher matcher)Deprecated.UsewriteAndFlush(Object, ChannelMatcher)instead.java.lang.Stringname()Returns the name of this group.ChannelGroupFuturenewCloseFuture()Returns theChannelGroupFuturewhich will be notified when allChannels that are part of thisChannelGroup, at the time of calling, are closed.ChannelGroupFuturenewCloseFuture(ChannelMatcher matcher)Returns theChannelGroupFuturewhich will be notified when allChannels that are part of thisChannelGroup, at the time of calling, are closed.ChannelGroupFuturewrite(java.lang.Object message)Writes the specifiedmessageto allChannels in this group.ChannelGroupFuturewrite(java.lang.Object message, ChannelMatcher matcher)Writes the specifiedmessageto allChannels in this group that are matched by the givenChannelMatcher.ChannelGroupFuturewrite(java.lang.Object message, ChannelMatcher matcher, boolean voidPromise)Writes the specifiedmessageto allChannels in this group that are matched by the givenChannelMatcher.ChannelGroupFuturewriteAndFlush(java.lang.Object message)Shortcut for callingwrite(Object)andflush().ChannelGroupFuturewriteAndFlush(java.lang.Object message, ChannelMatcher matcher)Shortcut for callingwrite(Object)andflush()and only act onChannels that are matched by theChannelMatcher.ChannelGroupFuturewriteAndFlush(java.lang.Object message, ChannelMatcher matcher, boolean voidPromise)Shortcut for callingwrite(Object, ChannelMatcher, boolean)andflush()and only act onChannels that are matched by theChannelMatcher.
-
-
-
Method Detail
-
name
java.lang.String name()
Returns the name of this group. A group name is purely for helping you to distinguish one group from others.
-
write
ChannelGroupFuture write(java.lang.Object message)
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
ChannelGroupFuture write(java.lang.Object message, ChannelMatcher matcher)
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
ChannelGroupFuture write(java.lang.Object message, ChannelMatcher matcher, boolean voidPromise)
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
ChannelGroup flush(ChannelMatcher matcher)
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
ChannelGroupFuture writeAndFlush(java.lang.Object message)
Shortcut for callingwrite(Object)andflush().
-
flushAndWrite
@Deprecated ChannelGroupFuture flushAndWrite(java.lang.Object message)
Deprecated.UsewriteAndFlush(Object)instead.
-
writeAndFlush
ChannelGroupFuture writeAndFlush(java.lang.Object message, ChannelMatcher matcher)
Shortcut for callingwrite(Object)andflush()and only act onChannels that are matched by theChannelMatcher.
-
writeAndFlush
ChannelGroupFuture writeAndFlush(java.lang.Object message, ChannelMatcher matcher, boolean voidPromise)
Shortcut for callingwrite(Object, ChannelMatcher, boolean)andflush()and only act onChannels that are matched by theChannelMatcher.
-
flushAndWrite
@Deprecated ChannelGroupFuture flushAndWrite(java.lang.Object message, ChannelMatcher matcher)
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
ChannelGroupFuture disconnect(ChannelMatcher matcher)
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
ChannelGroupFuture close(ChannelMatcher matcher)
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 ChannelGroupFuture 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 ChannelGroupFuture deregister(ChannelMatcher matcher)
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
ChannelGroupFuture newCloseFuture(ChannelMatcher matcher)
Returns theChannelGroupFuturewhich will be notified when allChannels that are part of thisChannelGroup, at the time of calling, are closed.
-
-