Package io.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
(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 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
(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().awaitUninterruptibly(); } public class MyHandler extendsChannelInboundHandlerAdapter
{@Override
public void channelActive(ChannelHandlerContext
ctx) { // closed on shutdown. allChannels.add(ctx.channel()); super.channelActive(ctx); } }
-
Method Summary
Modifier and TypeMethodDescriptionclose()
Closes allChannel
s in this group.close
(ChannelMatcher matcher) Closes allChannel
s in this group that are matched by the givenChannelMatcher
.Deprecated.This method will be removed in the next major feature release.deregister
(ChannelMatcher matcher) Deprecated.This method will be removed in the next major feature release.Disconnects allChannel
s in this group from their remote peers.disconnect
(ChannelMatcher matcher) Disconnects allChannel
s in this group from their remote peers, that are matched by the givenChannelMatcher
.flush()
Flush allChannel
s in this group.flush
(ChannelMatcher matcher) Flush allChannel
s in this group that are matched by the givenChannelMatcher
.flushAndWrite
(Object message) Deprecated.UsewriteAndFlush(Object)
instead.flushAndWrite
(Object message, ChannelMatcher matcher) Deprecated.UsewriteAndFlush(Object, ChannelMatcher)
instead.name()
Returns the name of this group.Returns theChannelGroupFuture
which will be notified when allChannel
s that are part of thisChannelGroup
, at the time of calling, are closed.newCloseFuture
(ChannelMatcher matcher) Returns theChannelGroupFuture
which will be notified when allChannel
s that are part of thisChannelGroup
, at the time of calling, are closed.Writes the specifiedmessage
to allChannel
s in this group.write
(Object message, ChannelMatcher matcher) Writes the specifiedmessage
to allChannel
s in this group that are matched by the givenChannelMatcher
.write
(Object message, ChannelMatcher matcher, boolean voidPromise) Writes the specifiedmessage
to allChannel
s in this group that are matched by the givenChannelMatcher
.writeAndFlush
(Object message) Shortcut for callingwrite(Object)
andflush()
.writeAndFlush
(Object message, ChannelMatcher matcher) Shortcut for callingwrite(Object)
andflush()
and only act onChannel
s that are matched by theChannelMatcher
.writeAndFlush
(Object message, ChannelMatcher matcher, boolean voidPromise) Shortcut for callingwrite(Object, ChannelMatcher, boolean)
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 Details
-
name
String name()Returns the name of this group. A group name is purely for helping you to distinguish one group from others. -
find
- Returns:
- the matching
Channel
if found.null
otherwise.
-
write
Writes the specifiedmessage
to allChannel
s in this group. If the specifiedmessage
is an instance ofByteBuf
, it is automatically duplicated to avoid a race condition. The same is true forByteBufHolder
. Please note that this operation is asynchronous asChannelOutboundInvoker.write(Object)
is.- Returns:
- itself
-
write
Writes the specifiedmessage
to allChannel
s in this group that are matched by the givenChannelMatcher
. If the specifiedmessage
is an instance ofByteBuf
, it is automatically duplicated to avoid a race condition. The same is true forByteBufHolder
. Please note that this operation is asynchronous asChannelOutboundInvoker.write(Object)
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 that are matched by the givenChannelMatcher
. If the specifiedmessage
is an instance ofByteBuf
, it is automatically duplicated to avoid a race condition. The same is true forByteBufHolder
. Please note that this operation is asynchronous asChannelOutboundInvoker.write(Object)
is. IfvoidPromise
istrue
ChannelOutboundInvoker.voidPromise()
is used for the writes and so the same restrictions to the returnedChannelGroupFuture
apply as to a void promise.- 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 specifiedmessages
are an instance ofByteBuf
, it is automatically duplicated to avoid a race condition. Please note that this operation is asynchronous asChannelOutboundInvoker.write(Object)
is.- Returns:
- the
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
flush
Flush allChannel
s in this group that are matched by the givenChannelMatcher
. If the specifiedmessages
are an instance ofByteBuf
, it is automatically duplicated to avoid a race condition. Please note that this operation is asynchronous asChannelOutboundInvoker.write(Object)
is.- Returns:
- the
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
writeAndFlush
Shortcut for callingwrite(Object)
andflush()
. -
flushAndWrite
Deprecated.UsewriteAndFlush(Object)
instead. -
writeAndFlush
Shortcut for callingwrite(Object)
andflush()
and only act onChannel
s that are matched by theChannelMatcher
. -
writeAndFlush
Shortcut for callingwrite(Object, ChannelMatcher, boolean)
andflush()
and only act onChannel
s that are matched by theChannelMatcher
. -
flushAndWrite
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
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
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.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 asChannelOutboundInvoker.deregister()
is.- Returns:
- the
ChannelGroupFuture
instance that notifies when the operation is done for all channels
-
deregister
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 asChannelOutboundInvoker.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
Returns theChannelGroupFuture
which will be notified when allChannel
s that are part of thisChannelGroup
, at the time of calling, are closed.
-