/
ThreadContext.java
317 lines (301 loc) · 15.2 KB
/
ThreadContext.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed 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 org.eclipse.microprofile.concurrent;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* This interface offers various methods for capturing the context of the current thread
* and applying it to various interfaces that are commonly used with completion stages
* and executor services. This allows you to contextualize specific actions that need
* access to the context of the creator/submitter of the stage/task.
*
* <p>Example usage:</p>
* <pre>
* <code>@Inject</code> ThreadContext threadContext;
* ...
* CompletableFuture<Integer> stage2 = stage1.thenApply(threadContext.withCurrentContext(function));
* ...
* Future<Integer> future = executor.submit(threadContext.withCurrentContext(callable));
* </pre>
*
* <p>This interface is intentionally kept compatible with ContextService,
* with the hope that its methods might one day be contributed to that specification.</p>
*/
public interface ThreadContext {
/**
* <p>Identifier for all available thread context types which are
* not specified individually under <code>cleared</code>,
* <code>propagated</code>, or <code>unchanged</code>.</p>
*
* <p>When using this constant, be aware that bringing in a new
* context provider or updating levels of an existing context provider
* might change the set of available thread context types.</p>
*
* @see ManagedExecutorBuilder#cleared
* @see ManagedExecutorBuilder#propagated
* @see ManagedExecutorConfig#cleared
* @see ManagedExecutorConfig#propagated
* @see ThreadContextBuilder
* @see ThreadContextConfig
*/
static final String ALL_OTHER = "All other";
/**
* Identifier for application context. Application context controls the
* application component that is associated with a thread. It can determine
* the thread context class loader as well as the set of resource references
* that are available for lookup or resource injection. An empty/default
* application context means that the thread is not associated with any
* application.
*
* @see ManagedExecutorBuilder#cleared
* @see ManagedExecutorBuilder#propagated
* @see ManagedExecutorConfig#cleared
* @see ManagedExecutorConfig#propagated
* @see ThreadContextBuilder
* @see ThreadContextConfig
*/
static final String APPLICATION = "Application";
/**
* Identifier for CDI context. CDI context controls the availability of CDI
* scopes. An empty/default CDI context means that the thread does not have
* access to the scope of the session, request, and so forth that created the
* contextualized action.
*
* @see ManagedExecutorBuilder#cleared
* @see ManagedExecutorBuilder#propagated
* @see ManagedExecutorConfig#cleared
* @see ManagedExecutorConfig#propagated
* @see ThreadContextBuilder
* @see ThreadContextConfig
*/
static final String CDI = "CDI";
/**
* Identifier for security context. Security context controls the credentials
* that are associated with the thread. An empty/default security context
* means that the thread is unauthenticated.
*
* @see ManagedExecutorBuilder#cleared
* @see ManagedExecutorBuilder#propagated
* @see ManagedExecutorConfig#cleared
* @see ManagedExecutorConfig#propagated
* @see ThreadContextBuilder
* @see ThreadContextConfig
*/
static final String SECURITY = "Security";
/**
* Identifier for transaction context. Transaction context controls the
* active transaction scope that is associated with the thread.
* Implementations are not expected to propagate transaction context across
* threads. Instead, the concept of transaction context is provided for its
* cleared context, which means the active transaction on the thread
* is suspended such that a new transaction can be started if so desired.
* In most cases, the most desirable behavior will be to leave transaction
* context defaulted to cleared (suspended),
* in order to prevent dependent actions and tasks from accidentally
* enlisting in transactions that are on the threads where they happen to
* run.
*
* @see ManagedExecutorBuilder#cleared
* @see ManagedExecutorBuilder#propagated
* @see ManagedExecutorConfig#cleared
* @see ManagedExecutorConfig#propagated
* @see ThreadContextBuilder
* @see ThreadContextConfig
*/
static final String TRANSACTION = "Transaction";
/**
* <p>Creates an <code>Executor</code>that runs tasks on the same thread from which
* <code>execute</code>is invoked but with context that is captured from the thread
* that invokes <code>currentContextExecutor</code>.</p>
*
* <p>Example usage:</p>
* <pre>
* <code>Executor contextSnapshot = threadContext.currentContextExecutor();
* ...
* // from another thread, or after thread context has changed,
* contextSnapshot.execute(() -> obj.doSomethingThatNeedsContext());
* contextSnapshot.execute(() -> doSomethingElseThatNeedsContext(x, y));
* </code></pre>
*
* @return an executor that wraps the <code>execute</code> method with context.
*/
Executor currentContextExecutor();
/**
* <p>Wraps a <code>BiConsumer</code> with context that is captured from the thread that invokes
* <code>withCurrentContext</code>.</p>
*
* <p>When <code>accept</code> is invoked on the proxy instance,
* context is first established on the thread that will run the <code>accept</code> method,
* then the <code>accept</code> method of the provided <code>BiConsumer</code> is invoked.
* Finally, the previous context is restored on the thread, and control is returned to the invoker.</p>
*
* @param <T> type of first parameter to consumer.
* @param <U> type of second parameter to consumer.
* @param consumer instance to contextualize.
* @return contextualized proxy instance that wraps execution of the <code>accept</code> method with context.
*/
<T, U> BiConsumer<T, U> withCurrentContext(BiConsumer<T, U> consumer);
/**
* <p>Wraps a <code>BiFunction</code> with context that is captured from the thread that invokes
* <code>withCurrentContext</code>.</p>
*
* <p>When <code>apply</code> is invoked on the proxy instance,
* context is first established on the thread that will run the <code>apply</code> method,
* then the <code>apply</code> method of the provided <code>BiFunction</code> is invoked.
* Finally, the previous context is restored on the thread, and the result of the
* <code>BiFunction</code> is returned to the invoker.</p>
*
* @param <T> type of first parameter to function.
* @param <U> type of second parameter to function.
* @param <R> function result type.
* @param function instance to contextualize.
* @return contextualized proxy instance that wraps execution of the <code>apply</code> method with context.
*/
<T, U, R> BiFunction<T, U, R> withCurrentContext(BiFunction<T, U, R> function);
/**
* <p>Wraps a <code>Callable</code> with context that is captured from the thread that invokes
* <code>withCurrentContext</code>.</p>
*
* <p>When <code>call</code> is invoked on the proxy instance,
* context is first established on the thread that will run the <code>call</code> method,
* then the <code>call</code> method of the provided <code>Callable</code> is invoked.
* Finally, the previous context is restored on the thread, and the result of the
* <code>Callable</code> is returned to the invoker.</p>
*
* @param <R> callable result type.
* @param callable instance to contextualize.
* @return contextualized proxy instance that wraps execution of the <code>call</code> method with context.
*/
<R> Callable<R> withCurrentContext(Callable<R> callable);
/**
* <p>Wraps a <code>Consumer</code> with context that is captured from the thread that invokes
* <code>withCurrentContext</code>.</p>
*
* <p>When <code>accept</code> is invoked on the proxy instance,
* context is first established on the thread that will run the <code>accept</code> method,
* then the <code>accept</code> method of the provided <code>Consumer</code> is invoked.
* Finally, the previous context is restored on the thread, and control is returned to the invoker.</p>
*
* @param <T> type of parameter to consumer.
* @param consumer instance to contextualize.
* @return contextualized proxy instance that wraps execution of the <code>accept</code> method with context.
*/
<T> Consumer<T> withCurrentContext(Consumer<T> consumer);
/**
* <p>Wraps a <code>Function</code> with context that is captured from the thread that invokes
* <code>withCurrentContext</code>.</p>
*
* <p>When <code>apply</code> is invoked on the proxy instance,
* context is first established on the thread that will run the <code>apply</code> method,
* then the <code>apply</code> method of the provided <code>Function</code> is invoked.
* Finally, the previous context is restored on the thread, and the result of the
* <code>Function</code> is returned to the invoker.</p>
*
* @param <T> type of parameter to function.
* @param <R> function result type.
* @param function instance to contextualize.
* @return contextualized proxy instance that wraps execution of the <code>apply</code> method with context.
*/
<T, R> Function<T, R> withCurrentContext(Function<T, R> function);
/**
* <p>Wraps a <code>Runnable</code> with context that is captured from the thread that invokes
* <code>withCurrentContext</code>.</p>
*
* <p>When <code>run</code> is invoked on the proxy instance,
* context is first established on the thread that will run the <code>run</code> method,
* then the <code>run</code> method of the provided <code>Runnable</code> is invoked.
* Finally, the previous context is restored on the thread, and control is returned to the invoker.</p>
*
* @param runnable instance to contextualize.
* @return contextualized proxy instance that wraps execution of the <code>run</code> method with context.
*/
Runnable withCurrentContext(Runnable runnable);
/**
* <p>Wraps a <code>Supplier</code> with context captured from the thread that invokes
* <code>withCurrentContext</code>.</p>
*
* <p>When <code>supply</code> is invoked on the proxy instance,
* context is first established on the thread that will run the <code>supply</code> method,
* then the <code>supply</code> method of the provided <code>Supplier</code> is invoked.
* Finally, the previous context is restored on the thread, and the result of the
* <code>Supplier</code> is returned to the invoker.</p>
*
* @param <R> supplier result type.
* @param supplier instance to contextualize.
* @return contextualized proxy instance that wraps execution of the <code>supply</code> method with context.
*/
<R> Supplier<R> withCurrentContext(Supplier<R> supplier);
/**
* <p>Returns a new <code>CompletableFuture</code> that is completed by the completion of the
* specified stage.</p>
*
* <p>The container supplies the default asynchronous execution facility for the new completable
* future that is returned by this method and all dependent stages that are created from it,
* and all dependent stages that are created from those, and so forth.</p>
*
* <p>When dependent stages are created from the new completable future, thread context is captured
* from the thread that creates the dependent stage and is applied to the thread that runs the
* action, being removed afterward. When dependent stages are created from these dependent stages,
* and likewise from any dependent stages created from those, and so on, thread context is captured
* from the respective thread that creates each dependent stage. This guarantees that the action
* performed by each stage always runs under the thread context of the code that creates the stage.
* </p>
*
* <p>Invocation of this method does not impact thread context propagation for the supplied
* completable future or any dependent stages created from it, other than the new dependent
* completable future that is created by this method.</p>
*
* @param stage a completable future whose completion triggers completion of the new completable
* future that is created by this method.
* @return the new completable future.
*/
<T> CompletableFuture<T> withContextCapture(CompletableFuture<T> stage);
/**
* <p>Returns a new <code>CompletionStage</code> that is completed by the completion of the
* specified stage.</p>
*
* <p>The container supplies the default asynchronous execution facility for the new completion
* stage that is returned by this method and all dependent stages that are created from it,
* and all dependent stages that are created from those, and so forth.</p>
*
* <p>When dependent stages are created from the new completion stage, thread context is captured
* from the thread that creates the dependent stage and is applied to the thread that runs the
* action, being removed afterward. When dependent stages are created from these dependent stages,
* and likewise from any dependent stages created from those, and so on, thread context is captured
* from the respective thread that creates each dependent stage. This guarantees that the action
* performed by each stage always runs under the thread context of the code that creates the stage.
* </p>
*
* <p>Invocation of this method does not impact thread context propagation for the supplied
* stage or any dependent stages created from it, other than the new dependent
* completion stage that is created by this method.</p>
*
* @param stage a completion stage whose completion triggers completion of the new stage
* that is created by this method.
* @return the new completion stage.
*/
<T> CompletionStage<T> withContextCapture(CompletionStage<T> stage);
}