io.netty.channel.group.ChannelGroup Maven / Gradle / Ivy
/*
* Copyright 2013 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.group;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelStateHandlerAdapter;
import io.netty.channel.EventLoop;
import io.netty.channel.FileRegion;
import io.netty.channel.ServerChannel;
import io.netty.util.CharsetUtil;
import java.util.Set;
/**
* A thread-safe {@link Set} that contains open {@link Channel}s and provides
* various bulk operations on them. Using {@link ChannelGroup}, you can
* categorize {@link Channel}s into a meaningful group (e.g. on a per-service
* or per-state basis.) A closed {@link Channel} is automatically removed from
* the collection, so that you don't need to worry about the life cycle of the
* added {@link Channel}. A {@link Channel} can belong to more than one
* {@link ChannelGroup}.
*
* Broadcast a message to multiple {@link Channel}s
*
* If you need to broadcast a message to more than one {@link Channel}, you can
* add the {@link Channel}s associated with the recipients and call {@link ChannelGroup#write(Object)}:
*
* {@link ChannelGroup} recipients = new {@link DefaultChannelGroup}();
* recipients.add(channelA);
* recipients.add(channelB);
* ..
* recipients.write({@link Unpooled}.copiedBuffer(
* "Service will shut down for maintenance in 5 minutes.",
* {@link CharsetUtil}.UTF_8));
*
*
* Simplify shutdown process with {@link ChannelGroup}
*
* If both {@link ServerChannel}s and non-{@link ServerChannel}s exist in the
* same {@link ChannelGroup}, any requested I/O operations on the group are
* performed for the {@link ServerChannel}s first and then for the others.
*
* This rule is very useful when you shut down a server in one shot:
*
*
* {@link ChannelGroup} allChannels = new {@link DefaultChannelGroup}();
*
* public static void main(String[] args) throws Exception {
* {@link ServerBootstrap} b = new {@link ServerBootstrap}(..);
* ...
*
* // Start the server
* b.getPipeline().addLast("handler", new MyHandler());
* {@link Channel} serverChannel = 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 extends {@link ChannelStateHandlerAdapter} {
* {@code @Override}
* public void channelActive({@link ChannelHandlerContext} ctx) {
* // closed on shutdown.
* allChannels.add(e.getChannel());
* super.channelActive(ctx);
* }
* }
*
*/
public interface ChannelGroup extends Set, Comparable {
/**
* Returns the name of this group. A group name is purely for helping
* you to distinguish one group from others.
*/
String name();
/**
* Returns the {@link Channel} whose ID matches the specified integer.
*
* @return the matching {@link Channel} if found. {@code null} otherwise.
*/
Channel find(Integer id);
/**
* Writes the specified {@code message} to all {@link Channel}s in this
* group. If the specified {@code message} is an instance of
* {@link ByteBuf}, it is automatically
* {@linkplain ByteBuf#duplicate() duplicated} to avoid a race
* condition. Please note that this operation is asynchronous as
* {@link Channel#write(Object)} is.
*
* @return the {@link ChannelGroupFuture} instance that notifies when
* the operation is done for all channels
*/
ChannelGroupFuture write(Object message);
/**
* Writes the specified {@link FileRegion} to all {@link Channel}s in this
* group. Please note that this operation is asynchronous as
* {@link Channel#sendFile(FileRegion)} is.
*
* @return the {@link ChannelGroupFuture} instance that notifies when
* the operation is done for all channels
*/
ChannelGroupFuture sendFile(FileRegion region);
/**
* Flush all {@link Channel} in this group. Please note that this operation
* is asynchronous as {@link Channel#flush()} is.
*
* @return the {@link ChannelGroupFuture} instance that notifies when
* the operation is done for all channels
*/
ChannelGroupFuture flush();
/**
* Disconnects all {@link Channel}s in this group from their remote peers.
*
* @return the {@link ChannelGroupFuture} instance that notifies when
* the operation is done for all channels
*/
ChannelGroupFuture disconnect();
/**
* Closes all {@link Channel}s in this group. If the {@link Channel} is
* connected to a remote peer or bound to a local address, it is
* automatically disconnected and unbound.
*
* @return the {@link ChannelGroupFuture} instance that notifies when
* the operation is done for all channels
*/
ChannelGroupFuture close();
/**
* Deregister all {@link Channel}s in this group from their {@link EventLoop}.
* Please note that this operation is asynchronous as {@link Channel#deregister()} is.
*
* @return the {@link ChannelGroupFuture} instance that notifies when
* the operation is done for all channels
*/
ChannelGroupFuture deregister();
}