Top Description Methods
io.netty.channel.group

public Interface ChannelGroup

extends Set<Channel>, Comparable<ChannelGroup>
Known Direct Implementers
io.netty.channel.group.DefaultChannelGroup
Imports
io.netty.bootstrap.ServerBootstrap, io.netty.buffer.ByteBuf, .ByteBufHolder, .Unpooled, io.netty.channel.Channel, .ChannelHandlerContext, .ChannelId, .ChannelInboundHandlerAdapter, .EventLoop, .ServerChannel, io.netty.util.CharsetUtil, io.netty.util.concurrent.GlobalEventExecutor, java.util.Set

A thread-safe Set that contains open Channels and provides various bulk operations on them. Using ChannelGroup, you can categorize Channels 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 Channels

If you need to broadcast a message to more than one Channel, you can add the Channels associated with the recipients and call ChannelGroup#write(Object):

ChannelGroup recipients =
        new DefaultChannelGroup(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 ServerChannels and non-ServerChannels exist in the same ChannelGroup, any requested I/O operations on the group are performed for the ServerChannels first and then for the others.

This rule is very useful when you shut down a server in one shot:

ChannelGroup allChannels =
        new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

public static void main(String[] args) throws Exception {
    ServerBootstrap b = new ServerBootstrap(..);
    ...
    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 extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        // closed on shutdown.
        allChannels.add(ctx.channel());
        super.channelActive(ctx);
    }
}

Method Summary

Modifier and TypeMethod and Description
public ChannelGroupFuture

Returns:

the ChannelGroupFuture instance that notifies when the operation is done for all channels
close
()

Closes all Channels in this group.

public ChannelGroupFuture

Returns:

the ChannelGroupFuture instance that notifies when the operation is done for all channels
close
(ChannelMatcher matcher)

Closes all Channels in this group that are matched by the given ChannelMatcher.

public ChannelGroupFuture

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.
public ChannelGroupFuture

Returns:

the ChannelGroupFuture instance that notifies when the operation is done for all channels
deregister
(ChannelMatcher matcher)

Deprecated This method will be removed in the next major feature release.
public ChannelGroupFuture

Returns:

the ChannelGroupFuture instance that notifies when the operation is done for all channels
disconnect
()

Disconnects all Channels in this group from their remote peers.

public ChannelGroupFuture

Returns:

the ChannelGroupFuture instance that notifies when the operation is done for all channels
disconnect
(ChannelMatcher matcher)

Disconnects all Channels in this group from their remote peers, that are matched by the given ChannelMatcher.

public Channel

Returns:

the matching Channel if found. null otherwise.
find
(ChannelId id)

Returns the Channel which has the specified ChannelId.

public ChannelGroup

Returns:

the ChannelGroupFuture instance that notifies when the operation is done for all channels
flush
()

Flush all Channels in this group.

public ChannelGroup

Returns:

the ChannelGroupFuture instance that notifies when the operation is done for all channels
flush
(ChannelMatcher matcher)

Flush all Channels in this group that are matched by the given ChannelMatcher.

public ChannelGroupFuture
flushAndWrite(Object message)

Deprecated Use writeAndFlush(Object) instead.
public ChannelGroupFuture
public String
name()

Returns the name of this group.

public ChannelGroupFuture
newCloseFuture()

Returns the ChannelGroupFuture which will be notified when all Channels that are part of this ChannelGroup, at the time of calling, are closed.

public ChannelGroupFuture
newCloseFuture(ChannelMatcher matcher)

Returns the ChannelGroupFuture which will be notified when all Channels that are part of this ChannelGroup, at the time of calling, are closed.

public ChannelGroupFuture

Returns:

itself
write
(Object message)

Writes the specified message to all Channels in this group.

public ChannelGroupFuture

Returns:

the ChannelGroupFuture instance that notifies when the operation is done for all channels
write
(Object message, ChannelMatcher matcher)

Writes the specified message to all Channels in this group that are matched by the given ChannelMatcher.

public ChannelGroupFuture

Returns:

the ChannelGroupFuture instance that notifies when the operation is done for all channels
write
(Object message, ChannelMatcher matcher, boolean voidPromise)

Writes the specified message to all Channels in this group that are matched by the given ChannelMatcher.

public ChannelGroupFuture
writeAndFlush(Object message)

Shortcut for calling write(Object) and flush().

public ChannelGroupFuture
writeAndFlush(Object message, ChannelMatcher matcher)

Shortcut for calling write(Object) and flush() and only act on Channels that are matched by the ChannelMatcher.

public ChannelGroupFuture
writeAndFlush(Object message, ChannelMatcher matcher, boolean voidPromise)

Shortcut for calling write(Object, ChannelMatcher, boolean) and flush() and only act on Channels that are matched by the ChannelMatcher.

Inherited from java.lang.Comparable:
compareTo
Inherited from java.util.Set:
addaddAllclearcontainscontainsAllcopyOfequalshashCodeisEmptyiteratorofofofofofofofofofofofofremoveremoveAllretainAllsizespliteratortoArraytoArray

Method Detail

closeback to summary
public ChannelGroupFuture close()

Closes all Channels in this group. If the Channel is connected to a remote peer or bound to a local address, it is automatically disconnected and unbound.

Returns:ChannelGroupFuture

the ChannelGroupFuture instance that notifies when the operation is done for all channels

closeback to summary
public ChannelGroupFuture close(ChannelMatcher matcher)

Closes all Channels in this group that are matched by the given ChannelMatcher. If the Channel is connected to a remote peer or bound to a local address, it is automatically disconnected and unbound.

Returns:ChannelGroupFuture

the ChannelGroupFuture instance that notifies when the operation is done for all channels

deregisterback to summary
public ChannelGroupFuture deregister()

Deprecated

This method will be removed in the next major feature release. Deregister all Channels in this group from their EventLoop. Please note that this operation is asynchronous as Channel#deregister() is.

Returns:ChannelGroupFuture

the ChannelGroupFuture instance that notifies when the operation is done for all channels

Annotations
@Deprecated
deregisterback to summary
public ChannelGroupFuture deregister(ChannelMatcher matcher)

Deprecated

This method will be removed in the next major feature release. Deregister all Channels in this group from their EventLoop that are matched by the given ChannelMatcher. Please note that this operation is asynchronous as Channel#deregister() is.

Returns:ChannelGroupFuture

the ChannelGroupFuture instance that notifies when the operation is done for all channels

Annotations
@Deprecated
disconnectback to summary
public ChannelGroupFuture disconnect()

Disconnects all Channels in this group from their remote peers.

Returns:ChannelGroupFuture

the ChannelGroupFuture instance that notifies when the operation is done for all channels

disconnectback to summary
public ChannelGroupFuture disconnect(ChannelMatcher matcher)

Disconnects all Channels in this group from their remote peers, that are matched by the given ChannelMatcher.

Returns:ChannelGroupFuture

the ChannelGroupFuture instance that notifies when the operation is done for all channels

findback to summary
public Channel find(ChannelId id)

Returns the Channel which has the specified ChannelId.

Returns:Channel

the matching Channel if found. null otherwise.

flushback to summary
public ChannelGroup flush()

Flush all Channels in this group. If the specified messages are an instance of ByteBuf, it is automatically duplicated to avoid a race condition. Please note that this operation is asynchronous as Channel#write(Object) is.

Returns:ChannelGroup

the ChannelGroupFuture instance that notifies when the operation is done for all channels

flushback to summary
public ChannelGroup flush(ChannelMatcher matcher)

Flush all Channels in this group that are matched by the given ChannelMatcher. If the specified messages are an instance of ByteBuf, it is automatically duplicated to avoid a race condition. Please note that this operation is asynchronous as Channel#write(Object) is.

Returns:ChannelGroup

the ChannelGroupFuture instance that notifies when the operation is done for all channels

flushAndWriteback to summary
public ChannelGroupFuture flushAndWrite(Object message)

Deprecated

Use writeAndFlush(Object) instead.

Annotations
@Deprecated
flushAndWriteback to summary
public ChannelGroupFuture flushAndWrite(Object message, ChannelMatcher matcher)

Deprecated

Use writeAndFlush(Object, ChannelMatcher) instead.

Annotations
@Deprecated
nameback to summary
public String name()

Returns the name of this group. A group name is purely for helping you to distinguish one group from others.

newCloseFutureback to summary
public ChannelGroupFuture newCloseFuture()

Returns the ChannelGroupFuture which will be notified when all Channels that are part of this ChannelGroup, at the time of calling, are closed.

newCloseFutureback to summary
public ChannelGroupFuture newCloseFuture(ChannelMatcher matcher)

Returns the ChannelGroupFuture which will be notified when all Channels that are part of this ChannelGroup, at the time of calling, are closed.

writeback to summary
public ChannelGroupFuture write(Object message)

Writes the specified message to all Channels in this group. If the specified message is an instance of ByteBuf, it is automatically duplicated to avoid a race condition. The same is true for ByteBufHolder. Please note that this operation is asynchronous as Channel#write(Object) is.

Returns:ChannelGroupFuture

itself

writeback to summary
public ChannelGroupFuture write(Object message, ChannelMatcher matcher)

Writes the specified message to all Channels in this group that are matched by the given ChannelMatcher. If the specified message is an instance of ByteBuf, it is automatically duplicated to avoid a race condition. The same is true for ByteBufHolder. Please note that this operation is asynchronous as Channel#write(Object) is.

Returns:ChannelGroupFuture

the ChannelGroupFuture instance that notifies when the operation is done for all channels

writeback to summary
public ChannelGroupFuture write(Object message, ChannelMatcher matcher, boolean voidPromise)

Writes the specified message to all Channels in this group that are matched by the given ChannelMatcher. If the specified message is an instance of ByteBuf, it is automatically duplicated to avoid a race condition. The same is true for ByteBufHolder. Please note that this operation is asynchronous as Channel#write(Object) is. If voidPromise is true Channel#voidPromise() is used for the writes and so the same restrictions to the returned ChannelGroupFuture apply as to a void promise.

Returns:ChannelGroupFuture

the ChannelGroupFuture instance that notifies when the operation is done for all channels

writeAndFlushback to summary
public ChannelGroupFuture writeAndFlush(Object message)

Shortcut for calling write(Object) and flush().

writeAndFlushback to summary
public ChannelGroupFuture writeAndFlush(Object message, ChannelMatcher matcher)

Shortcut for calling write(Object) and flush() and only act on Channels that are matched by the ChannelMatcher.

writeAndFlushback to summary
public ChannelGroupFuture writeAndFlush(Object message, ChannelMatcher matcher, boolean voidPromise)

Shortcut for calling write(Object, ChannelMatcher, boolean) and flush() and only act on Channels that are matched by the ChannelMatcher.