1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.util.concurrent;
17
18 import io.netty.util.internal.PromiseNotificationUtil;
19 import io.netty.util.internal.logging.InternalLogger;
20 import io.netty.util.internal.logging.InternalLoggerFactory;
21
22 import static io.netty.util.internal.ObjectUtil.checkNotNull;
23
24
25
26
27
28
29
30
31 public class PromiseNotifier<V, F extends Future<V>> implements GenericFutureListener<F> {
32
33 private static final InternalLogger logger = InternalLoggerFactory.getInstance(PromiseNotifier.class);
34 private final Promise<? super V>[] promises;
35 private final boolean logNotifyFailure;
36
37
38
39
40
41
42 @SafeVarargs
43 public PromiseNotifier(Promise<? super V>... promises) {
44 this(true, promises);
45 }
46
47
48
49
50
51
52
53 @SafeVarargs
54 public PromiseNotifier(boolean logNotifyFailure, Promise<? super V>... promises) {
55 checkNotNull(promises, "promises");
56 for (Promise<? super V> promise: promises) {
57 if (promise == null) {
58 throw new IllegalArgumentException("promises contains null Promise");
59 }
60 }
61 this.promises = promises.clone();
62 this.logNotifyFailure = logNotifyFailure;
63 }
64
65 @Override
66 public void operationComplete(F future) throws Exception {
67 InternalLogger internalLogger = logNotifyFailure ? logger : null;
68 if (future.isSuccess()) {
69 V result = future.get();
70 for (Promise<? super V> p: promises) {
71 PromiseNotificationUtil.trySuccess(p, result, internalLogger);
72 }
73 } else if (future.isCancelled()) {
74 for (Promise<? super V> p: promises) {
75 PromiseNotificationUtil.tryCancel(p, internalLogger);
76 }
77 } else {
78 Throwable cause = future.cause();
79 for (Promise<? super V> p: promises) {
80 PromiseNotificationUtil.tryFailure(p, cause, internalLogger);
81 }
82 }
83 }
84 }