forked from eclipse-ee4j/glassfish-concurro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManagedCompletableFuture.java
145 lines (120 loc) · 5.78 KB
/
ManagedCompletableFuture.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.concurro.internal;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import jakarta.enterprise.concurrent.ManagedExecutorService;
/**
* Managed CompletableFuture, using the provided ManagedExecutorServiceImpl.
*
* @author Petr Aubrecht <aubrecht@asoftware.cz>
*/
public class ManagedCompletableFuture<T> extends CompletableFuture<T> {
private final ManagedExecutorService executor;
public ManagedCompletableFuture(ManagedExecutorService executor) {
this.executor = executor;
}
// default executor is our executor
@Override
public Executor defaultExecutor() {
return executor;
}
// this method is used to create a new CompletableFuture, provide the managed one
@Override
public <U> CompletableFuture<U> newIncompleteFuture() {
return new ManagedCompletableFuture<>(executor);
}
@Override
public <U> CompletableFuture<U> thenApply(Function<? super T, ? extends U> fn) {
return super.thenApply(executor.getContextService().contextualFunction(fn));
}
public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier, ManagedExecutorService executor) {
ManagedCompletableFuture<T> managedFuture = new ManagedCompletableFuture<>(executor);
executor.execute(() -> {
try {
managedFuture.complete(supplier.get());
} catch (Exception e) {
managedFuture.completeExceptionally(e);
}
});
return managedFuture;
}
@Override
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn) {
return thenApplyAsync(fn, executor);
}
@Override
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor) {
return super.thenApplyAsync(this.executor.getContextService().contextualFunction(fn), executor);
}
@Override
public <U, V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) {
return thenCombineAsync(other, executor.getContextService().contextualFunction(fn), executor);
}
@Override
public <U, V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn, Executor executor) {
return super.thenCombineAsync(other, this.executor.getContextService().contextualFunction(fn), executor);
}
@Override
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) {
// FIXME: implement correctly
return super.handleAsync(executor.getContextService().contextualFunction(fn));
}
@Override
public <U, V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) {
return super.thenCombine(other, executor.getContextService().contextualFunction(fn));
}
@Override
public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn) {
return applyToEitherAsync(other, fn, executor);
}
@Override
public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor) {
return super.applyToEitherAsync(other, this.executor.getContextService().contextualFunction(fn), executor); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
}
@Override
public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action) {
return super.thenAcceptBoth(other, executor.getContextService().contextualConsumer(action));
}
@Override
public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn) {
return super.exceptionally(this.executor.getContextService().contextualFunction(fn));
}
public static <U> CompletableFuture<U> completedFuture(U value, ManagedExecutorService executor) {
ManagedCompletableFuture<U> future = new ManagedCompletableFuture<>(executor);
future.complete(value);
return future;
}
public static <U> CompletionStage<U> completedStage(U value, ManagedExecutorService executor) {
ManagedCompletableFuture<U> future = new ManagedCompletableFuture<>(executor);
future.complete(value);
return future;
}
public static <U> CompletableFuture<U> failedFuture(Throwable ex, ManagedExecutorService executor) {
return executor.failedFuture(ex);
}
public static <U> CompletionStage<U> failedStage(Throwable ex, ManagedExecutorService executor) {
return executor.failedStage(ex);
}
public static CompletableFuture<Void> runAsync(Runnable runnable, ManagedExecutorService executor) {
return CompletableFuture.runAsync(runnable, executor);
}
}