From 3308510bc94d86b45a4acacd90485002a129bbcd Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 5 Jun 2015 12:44:55 +0200 Subject: [PATCH] Allow to receive a ChannelGroupFuture that will be notified once all Channels are closed. Motivation: It's useful to be able to be notified once all Channels that are part of the ChannelGroup are notified. This can for example be useful if you want to do a graceful shutdown. Modifications: - Add ChannelGroup.newCloseFuture(...) which will be notified once all Channels are notified that are part of the ChannelGroup at the time of calling. Result: Easier to be notified once all Channels within a ChannelGroup are closed. --- .../io/netty/channel/group/ChannelGroup.java | 12 ++++++++++ .../channel/group/DefaultChannelGroup.java | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/transport/src/main/java/io/netty/channel/group/ChannelGroup.java b/transport/src/main/java/io/netty/channel/group/ChannelGroup.java index 42fa39f1c5..3072e98f7d 100644 --- a/transport/src/main/java/io/netty/channel/group/ChannelGroup.java +++ b/transport/src/main/java/io/netty/channel/group/ChannelGroup.java @@ -223,4 +223,16 @@ public interface ChannelGroup extends Set, Comparable { * the operation is done for all channels */ ChannelGroupFuture deregister(ChannelMatcher matcher); + + /** + * Returns the {@link ChannelGroupFuture} which will be notified when all {@link Channel}s that are part of this + * {@link ChannelGroup}, at the time of calling, are closed. + */ + ChannelGroupFuture newCloseFuture(); + + /** + * Returns the {@link ChannelGroupFuture} which will be notified when all {@link Channel}s that are part of this + * {@link ChannelGroup}, at the time of calling, are closed. + */ + ChannelGroupFuture newCloseFuture(ChannelMatcher matcher); } diff --git a/transport/src/main/java/io/netty/channel/group/DefaultChannelGroup.java b/transport/src/main/java/io/netty/channel/group/DefaultChannelGroup.java index d3c20d7645..34029f7a3f 100644 --- a/transport/src/main/java/io/netty/channel/group/DefaultChannelGroup.java +++ b/transport/src/main/java/io/netty/channel/group/DefaultChannelGroup.java @@ -339,6 +339,30 @@ public class DefaultChannelGroup extends AbstractSet implements Channel return new DefaultChannelGroupFuture(this, futures, executor); } + @Override + public ChannelGroupFuture newCloseFuture() { + return newCloseFuture(ChannelMatchers.all()); + } + + @Override + public ChannelGroupFuture newCloseFuture(ChannelMatcher matcher) { + Map futures = + new LinkedHashMap(size()); + + for (Channel c: serverChannels.values()) { + if (matcher.matches(c)) { + futures.put(c, c.closeFuture()); + } + } + for (Channel c: nonServerChannels.values()) { + if (matcher.matches(c)) { + futures.put(c, c.closeFuture()); + } + } + + return new DefaultChannelGroupFuture(this, futures, executor); + } + @Override public int hashCode() { return System.identityHashCode(this);