forked from eclipse-ee4j/glassfish-concurro
/
ManagedFutureTask.java
258 lines (224 loc) · 9.17 KB
/
ManagedFutureTask.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
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 - 2024 [Payara Foundation and/or its affiliates].
*
* 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.enterprise.concurrent.internal;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import static jakarta.enterprise.concurrent.ManagedTask.IDENTITY_NAME;
import jakarta.enterprise.concurrent.AbortedException;
import jakarta.enterprise.concurrent.ContextService;
import jakarta.enterprise.concurrent.ManagedTask;
import jakarta.enterprise.concurrent.ManagedTaskListener;
import org.glassfish.enterprise.concurrent.AbstractManagedExecutorService;
import org.glassfish.enterprise.concurrent.spi.ContextHandle;
import org.glassfish.enterprise.concurrent.spi.ContextSetupProvider;
/**
* Future implementation to be returned by methods in ManagedExecutorSerivceImpl. This class is responsible for saving
* and restoring thread context, as well as invoking ManagedTaskListener methods.
*/
public class ManagedFutureTask<V> extends FutureTask<V> implements Future<V> {
final protected Object task;
final protected AbstractManagedExecutorService executor;
final protected ManagedTaskListener taskListener;
final boolean isContextualCallback;
protected ContextHandle contextHandleForSetup;
protected ContextHandle contextHandleForReset;
protected Throwable taskRunThrowable;
protected TaskDoneCallback<V> taskDoneCallback;
IllegalStateException contextSetupException;
public ManagedFutureTask(AbstractManagedExecutorService executor, Runnable runnable, V result) {
super(runnable, result);
this.task = runnable;
this.executor = executor;
this.taskListener = getManagedTaskListener(task);
this.isContextualCallback = isTaskContextualCallback(task) || executor.isContextualCallback();
captureContext(executor);
}
public ManagedFutureTask(AbstractManagedExecutorService executor, Callable<V> callable) {
super(callable);
this.task = callable;
this.executor = executor;
this.taskListener = getManagedTaskListener(task);
this.isContextualCallback = isTaskContextualCallback(task) || executor.isContextualCallback();
captureContext(executor);
}
private ManagedTaskListener getManagedTaskListener(Object task) {
if (task instanceof ManagedTask managedTask) {
return managedTask.getManagedTaskListener();
}
return null;
}
private boolean isTaskContextualCallback(Object task) {
// Contextual callback no longer specified through execution properties
return false;
}
protected final void captureContext(AbstractManagedExecutorService executor) {
ContextSetupProvider contextSetupProvider = executor.getContextSetupProvider();
ContextService contextService = executor.getContextService();
if (contextService != null && contextSetupProvider != null) {
contextHandleForSetup = contextSetupProvider.saveContext(contextService);
}
}
public void setupContext() {
ContextSetupProvider contextSetupProvider = executor.getContextSetupProvider();
if (contextSetupProvider != null) {
try {
contextHandleForReset = contextSetupProvider.setup(contextHandleForSetup);
} catch (IllegalStateException ex) {
// Context handle not in valid state. Do not run the task.
contextSetupException = ex;
}
}
}
public void resetContext() {
if (contextSetupException == null) {
// Only call reset() if setupContext() was called successfully
ContextSetupProvider contextSetupProvider = executor.getContextSetupProvider();
if (contextSetupProvider != null) {
contextSetupProvider.reset(contextHandleForReset);
}
}
}
@Override
public void run() {
if (contextSetupException == null) {
super.run();
} else {
abort();
}
}
@Override
public boolean runAndReset() {
if (contextSetupException == null) {
return super.runAndReset();
} else {
abort();
}
return false;
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
boolean result = super.cancel(mayInterruptIfRunning);
if (result && taskListener != null) {
try {
if (isContextualCallback) {
setupContext();
}
taskListener.taskAborted(this, executor.getExecutorForTaskListener(), task, new CancellationException());
} finally {
if (isContextualCallback) {
resetContext();
}
}
}
return result;
}
public void submitted() {
if (taskListener != null) {
try {
if (isContextualCallback) {
setupContext();
}
taskListener.taskSubmitted(this, executor.getExecutorForTaskListener(), task);
} finally {
if (isContextualCallback) {
resetContext();
}
}
}
}
@Override
protected void done() {
// For calling taskDone for cancelled tasks
super.done();
if (taskDoneCallback != null) {
taskDoneCallback.taskDone(this);
}
if (taskListener != null && isCancelled()) { // FIXME: only when cancelled?!?
try {
if (isContextualCallback) {
setupContext();
}
taskListener.taskDone(this, executor.getExecutorForTaskListener(), task, new CancellationException());
} finally {
if (isContextualCallback) {
resetContext();
}
}
}
}
@Override
protected void setException(Throwable t) {
super.setException(t);
taskRunThrowable = t;
}
public void starting(Thread t) {
if (executor.getManagedThreadFactory() != null) {
executor.getManagedThreadFactory().taskStarting(t, this);
}
if (taskListener != null) {
taskListener.taskStarting(this, executor.getExecutorForTaskListener(), task);
}
}
/**
* Call by ThreadPoolExecutor after a task is done execution. This is called on the thread where the task is run, so
* there is no need to set up thread context before calling the ManagedTaskListener callback method.
*
* @param t any runtime exception encountered during executing the task. But any Throwable thrown during executing of
* running the task would have been caught by FutureTask and would have been set by setException(). So t is ignored
* here.
*/
public void done(Throwable t) {
if (executor.getManagedThreadFactory() != null) {
executor.getManagedThreadFactory().taskDone(Thread.currentThread());
}
if (taskListener != null) {
taskListener.taskDone(this, executor.getExecutorForTaskListener(), task, t != null ? t : taskRunThrowable);
}
}
public void setTaskDoneCallback(TaskDoneCallback<V> taskDoneCallback) {
this.taskDoneCallback = taskDoneCallback;
}
public String getTaskIdentityName() {
if (task instanceof ManagedTask managedTask) {
Map<String, String> executionProperties = managedTask.getExecutionProperties();
if (executionProperties != null) {
String taskName = executionProperties.get(IDENTITY_NAME);
if (taskName != null) {
return taskName;
}
}
}
// if a name is not provided for the task, use toString() as the name
return task.toString();
}
private void abort() {
// Context handle not in valid state, throws AbortedException and
// do not run the task
AbortedException ex = new AbortedException(contextSetupException.getMessage(), contextSetupException);
setException(ex);
if (taskListener != null) {
// notify listener. No need to set context here as it wouldn't work
// anyway
taskListener.taskAborted(this, executor.getExecutorForTaskListener(), task, ex);
}
throw new RuntimeException(ex);
}
}