@@ -185,10 +185,10 @@ public void service(ServletRequest request, ServletResponse response) throws Ser
185
185
}
186
186
187
187
AtomicBoolean isCompleted = new AtomicBoolean ();
188
- HandlerResultAsyncListener listener = new HandlerResultAsyncListener (isCompleted , httpRequest );
189
- asyncContext .addListener (listener );
190
-
191
188
HandlerResultSubscriber subscriber = new HandlerResultSubscriber (asyncContext , isCompleted , httpRequest );
189
+ HandlerResultAsyncListener listener = new HandlerResultAsyncListener (isCompleted , httpRequest , subscriber );
190
+
191
+ asyncContext .addListener (listener );
192
192
this .httpHandler .handle (httpRequest , httpResponse ).subscribe (subscriber );
193
193
}
194
194
@@ -222,10 +222,6 @@ public void destroy() {
222
222
}
223
223
224
224
225
- /**
226
- * We cannot combine ERROR_LISTENER and HandlerResultSubscriber due to:
227
- * https://issues.jboss.org/browse/WFLY-8515.
228
- */
229
225
private static void runIfAsyncNotComplete (AsyncContext asyncContext , AtomicBoolean isCompleted , Runnable task ) {
230
226
try {
231
227
if (asyncContext .getRequest ().isAsyncStarted () && isCompleted .compareAndSet (false , true )) {
@@ -254,24 +250,41 @@ private static class HandlerResultAsyncListener implements AsyncListener {
254
250
255
251
private final String logPrefix ;
256
252
257
- public HandlerResultAsyncListener (AtomicBoolean isCompleted , ServletServerHttpRequest request ) {
253
+ // We cannot have AsyncListener and HandlerResultSubscriber until WildFly 12+:
254
+ // https://issues.jboss.org/browse/WFLY-8515
255
+ private final Runnable handlerDisposeTask ;
256
+
257
+ public HandlerResultAsyncListener (
258
+ AtomicBoolean isCompleted , ServletServerHttpRequest request , Runnable handlerDisposeTask ) {
259
+
258
260
this .isCompleted = isCompleted ;
259
261
this .logPrefix = request .getLogPrefix ();
262
+ this .handlerDisposeTask = handlerDisposeTask ;
260
263
}
261
264
262
265
@ Override
263
266
public void onTimeout (AsyncEvent event ) {
264
267
logger .debug (this .logPrefix + "Timeout notification" );
265
- AsyncContext context = event .getAsyncContext ();
266
- runIfAsyncNotComplete (context , this .isCompleted , context ::complete );
268
+ handleTimeoutOrError (event );
267
269
}
268
270
269
271
@ Override
270
272
public void onError (AsyncEvent event ) {
271
273
Throwable ex = event .getThrowable ();
272
274
logger .debug (this .logPrefix + "Error notification: " + (ex != null ? ex : "<no Throwable>" ));
275
+ handleTimeoutOrError (event );
276
+ }
277
+
278
+ private void handleTimeoutOrError (AsyncEvent event ) {
273
279
AsyncContext context = event .getAsyncContext ();
274
- runIfAsyncNotComplete (context , this .isCompleted , context ::complete );
280
+ runIfAsyncNotComplete (context , this .isCompleted , () -> {
281
+ try {
282
+ this .handlerDisposeTask .run ();
283
+ }
284
+ finally {
285
+ context .complete ();
286
+ }
287
+ });
275
288
}
276
289
277
290
@ Override
@@ -286,14 +299,17 @@ public void onComplete(AsyncEvent event) {
286
299
}
287
300
288
301
289
- private static class HandlerResultSubscriber implements Subscriber <Void > {
302
+ private static class HandlerResultSubscriber implements Subscriber <Void >, Runnable {
290
303
291
304
private final AsyncContext asyncContext ;
292
305
293
306
private final AtomicBoolean isCompleted ;
294
307
295
308
private final String logPrefix ;
296
309
310
+ @ Nullable
311
+ private volatile Subscription subscription ;
312
+
297
313
public HandlerResultSubscriber (
298
314
AsyncContext asyncContext , AtomicBoolean isCompleted , ServletServerHttpRequest httpRequest ) {
299
315
@@ -304,6 +320,7 @@ public HandlerResultSubscriber(
304
320
305
321
@ Override
306
322
public void onSubscribe (Subscription subscription ) {
323
+ this .subscription = subscription ;
307
324
subscription .request (Long .MAX_VALUE );
308
325
}
309
326
@@ -339,6 +356,14 @@ public void onComplete() {
339
356
logger .trace (this .logPrefix + "Handling completed" );
340
357
runIfAsyncNotComplete (this .asyncContext , this .isCompleted , this .asyncContext ::complete );
341
358
}
359
+
360
+ @ Override
361
+ public void run () {
362
+ Subscription s = this .subscription ;
363
+ if (s != null ) {
364
+ s .cancel ();
365
+ }
366
+ }
342
367
}
343
368
344
369
}
0 commit comments