-
- 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
(GlobalEventExecutor
.INSTANCE); recipients.add(channelA); recipients.add(channelB); .. recipients.write(DefaultBufferAllocators.preferredAllocator()
.copyOf( "Service will shut down for maintenance in 5 minutes.",StandardCharsets.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
(GlobalEventExecutor
.INSTANCE); public static void main(String[] args) throws Exception {ServerBootstrap
b = newServerBootstrap
(..); ... b.childHandler(new MyHandler()); // Start the server b.getPipeline().addLast("handler", new MyHandler());Channel
serverChannel = b.bind(..).sync(); allChannels.add(serverChannel); ... Wait until the shutdown signal reception ... // Close the serverChannel and then all accepted connections. allChannels.close().await(); } public class MyHandler implementsChannelHandler
{@Override
public void channelActive(ChannelHandlerContext
ctx) { // closed on shutdown. allChannels.add(ctx.channel()); ctx.fireChannelActive(); } }
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description ChannelGroupFuture
close()
Closes allChannel
s in this group.ChannelGroupFuture
close(ChannelMatcher matcher)
Closes allChannel
s in this group that are matched by the givenChannelMatcher
.ChannelGroupFuture
deregister()
Deprecated.This method will be removed in the next major feature release.ChannelGroupFuture
deregister(ChannelMatcher matcher)
Deprecated.This method will be removed in the next major feature release.ChannelGroupFuture
disconnect()
Disconnects allChannel
s in this group from their remote peers.ChannelGroupFuture
disconnect(ChannelMatcher matcher)
Disconnects allChannel
s in this group from their remote peers, that are matched by the givenChannelMatcher
.Channel
find(ChannelId id)
ChannelGroup
flush()
Flush allChannel
s in this group.ChannelGroup
flush(ChannelMatcher matcher)
Flush allChannel
s in this group that are matched by the givenChannelMatcher
.ChannelGroupFuture
flushAndWrite(Object message)
Deprecated.UsewriteAndFlush(Object)
instead.ChannelGroupFuture
flushAndWrite(Object message, ChannelMatcher matcher)
Deprecated.UsewriteAndFlush(Object, ChannelMatcher)
instead.String
name()
Returns the name of this group.ChannelGroupFuture
newCloseFuture()
Returns theChannelGroupFuture
which will be notified when allChannel
s that are part of thisChannelGroup
, at the time of calling, are closed.ChannelGroupFuture
newCloseFuture(ChannelMatcher matcher)
Returns theChannelGroupFuture
which will be notified when allChannel
s that are part of thisChannelGroup
, at the time of calling, are closed.ChannelGroupFuture
write(Object message)
Writes the specifiedmessage
to allChannel
s in this group.ChannelGroupFuture
write(Object message, ChannelMatcher matcher)
Writes the specifiedmessage
to allChannel
s in this group that are matched by the givenChannelMatcher
.ChannelGroupFuture
writeAndFlush(Object message)
Shortcut for callingwrite(Object)
andflush()
.ChannelGroupFuture
writeAndFlush(Object message, ChannelMatcher matcher)
Shortcut for callingwrite(Object)
andflush()
and only act onChannel
s that are matched by theChannelMatcher
.-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.lang.Comparable
compareTo
-
-
-
-
Method Detail
-
name
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(Object message)
Writes the specifiedmessage
to allChannel
s in this group. If the specifiedmessage
is an instance ofBuffer
, it is automatically copied to avoid a race condition. Please note that this operation is asynchronous asChannel.write(Object)
is.- Returns:
- itself
-
write
ChannelGroupFuture write(Object message, ChannelMatcher matcher)
Writes the specifiedmessage
to allChannel
s in this group that are matched by the givenChannelMatcher
. If the specifiedmessage
is an instance ofBuffer
, it is automatically copied 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
-
flush
ChannelGroup flush()
Flush allChannel
s in this group. If the specifiedmessage
is an instance ofBuffer
, it is automatically copied to avoid a race condition. Please note that this operation is asynchronous asChannel.flush()
is.- Returns:
- the
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
flush
ChannelGroup flush(ChannelMatcher matcher)
Flush allChannel
s in this group that are matched by the givenChannelMatcher
. If the specifiedmessage
is an instance ofBuffer
, it is automatically copied to avoid a race condition. Please note that this operation is asynchronous asChannel.flush()
is.- Returns:
- the
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
writeAndFlush
ChannelGroupFuture writeAndFlush(Object message)
Shortcut for callingwrite(Object)
andflush()
.
-
flushAndWrite
@Deprecated ChannelGroupFuture flushAndWrite(Object message)
Deprecated.UsewriteAndFlush(Object)
instead.
-
writeAndFlush
ChannelGroupFuture writeAndFlush(Object message, ChannelMatcher matcher)
Shortcut for callingwrite(Object)
andflush()
and only act onChannel
s that are matched by theChannelMatcher
.
-
flushAndWrite
@Deprecated ChannelGroupFuture flushAndWrite(Object message, ChannelMatcher matcher)
Deprecated.UsewriteAndFlush(Object, ChannelMatcher)
instead.
-
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
-
disconnect
ChannelGroupFuture disconnect(ChannelMatcher matcher)
Disconnects allChannel
s in this group from their remote peers, that are matched by the givenChannelMatcher
.- 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
-
close
ChannelGroupFuture close(ChannelMatcher matcher)
Closes allChannel
s in this group that are matched by the givenChannelMatcher
. 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
-
deregister
@Deprecated ChannelGroupFuture deregister()
Deprecated.This method will be removed in the next major feature release. Deregister allChannel
s in this group from theirEventLoop
. Please note that this operation is asynchronous asChannel.deregister()
is.- Returns:
- the
ChannelGroupFuture
instance 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 allChannel
s in this group from theirEventLoop
that are matched by the givenChannelMatcher
. Please note that this operation is asynchronous asChannel.deregister()
is.- Returns:
- the
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
newCloseFuture
ChannelGroupFuture newCloseFuture()
Returns theChannelGroupFuture
which will be notified when allChannel
s that are part of thisChannelGroup
, at the time of calling, are closed.
-
newCloseFuture
ChannelGroupFuture newCloseFuture(ChannelMatcher matcher)
Returns theChannelGroupFuture
which will be notified when allChannel
s that are part of thisChannelGroup
, at the time of calling, are closed.
-
-