Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 3cefff8

Browse files
author
Marek Potociar
committed
OWLS-20819: Servlet AsyncContext not reused.
- updated the Jersey Servlet 3 integration code to reuse an existing AsyncContext, if available. Change-Id: I60d581dc24f0ab6786c65a0d194a1d01b0d8df4f Signed-off-by: Marek Potociar <[email protected]>
1 parent d32a467 commit 3cefff8

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

containers/jersey-servlet/src/main/java/org/glassfish/jersey/servlet/async/AsyncContextDelegateProviderImpl.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@
4141

4242
import java.util.concurrent.atomic.AtomicBoolean;
4343
import java.util.concurrent.atomic.AtomicReference;
44+
import java.util.logging.Level;
45+
import java.util.logging.Logger;
4446

4547
import javax.servlet.AsyncContext;
4648
import javax.servlet.http.HttpServletRequest;
4749
import javax.servlet.http.HttpServletResponse;
4850

51+
import org.glassfish.jersey.servlet.init.internal.LocalizationMessages;
4952
import org.glassfish.jersey.servlet.spi.AsyncContextDelegate;
5053
import org.glassfish.jersey.servlet.spi.AsyncContextDelegateProvider;
5154

52-
5355
/**
5456
* Servlet 3.x container response writer async extension and related extension factory implementation.
5557
*
@@ -58,6 +60,8 @@
5860
*/
5961
public class AsyncContextDelegateProviderImpl implements AsyncContextDelegateProvider {
6062

63+
private static final Logger LOGGER = Logger.getLogger(AsyncContextDelegateProviderImpl.class.getName());
64+
6165
@Override
6266
public final AsyncContextDelegate createDelegate(final HttpServletRequest request, final HttpServletResponse response) {
6367
return new ExtensionImpl(request, response);
@@ -75,7 +79,7 @@ private static final class ExtensionImpl implements AsyncContextDelegate {
7579
/**
7680
* Create a Servlet 3.x {@link AsyncContextDelegate} with given {@code request} and {@code response}.
7781
*
78-
* @param request request to create {@link AsyncContext} for.
82+
* @param request request to create {@link AsyncContext} for.
7983
* @param response response to create {@link AsyncContext} for.
8084
*/
8185
private ExtensionImpl(final HttpServletRequest request, final HttpServletResponse response) {
@@ -89,13 +93,27 @@ private ExtensionImpl(final HttpServletRequest request, final HttpServletRespons
8993
public void suspend() throws IllegalStateException {
9094
// Suspend only if not completed and not suspended before.
9195
if (!completed.get() && asyncContextRef.get() == null) {
92-
final AsyncContext asyncContext = request.startAsync(request, response);
96+
asyncContextRef.set(getAsyncContext());
97+
}
98+
}
9399

100+
private AsyncContext getAsyncContext() {
101+
final AsyncContext asyncContext;
102+
if (request.isAsyncStarted()) {
103+
asyncContext = request.getAsyncContext();
104+
try {
105+
asyncContext.setTimeout(NEVER_TIMEOUT_VALUE);
106+
} catch (IllegalStateException ex) {
107+
// Let's hope the time out is set properly, otherwise JAX-RS AsyncResponse time-out support
108+
// may not work as expected... At least we can log this at fine level...
109+
LOGGER.log(Level.FINE, LocalizationMessages.SERVLET_ASYNC_CONTEXT_ALREADY_STARTED(), ex);
110+
}
111+
} else {
112+
asyncContext = request.startAsync(request, response);
94113
// Tell underlying asyncContext to never time out.
95114
asyncContext.setTimeout(NEVER_TIMEOUT_VALUE);
96-
97-
asyncContextRef.set(asyncContext);
98115
}
116+
return asyncContext;
99117
}
100118

101119
@Override

containers/jersey-servlet/src/main/resources/org/glassfish/jersey/servlet/init/internal/localization.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
#
4-
# Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved.
4+
# Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
55
#
66
# The contents of this file are subject to the terms of either the GNU
77
# General Public License Version 2 only ("GPL") or the Common Development
@@ -44,3 +44,5 @@ jersey.app.no.mapping.or.annotation=The Jersey servlet application, named {0}, i
4444
jersey.app.registered.classes=Registering the Jersey servlet application, named {0}, with the following root resource and provider classes: {1}
4545
jersey.app.registered.mapping=Registering the Jersey servlet application, named {0}, at the servlet mapping {1}, with the Application class of the same name.
4646
jersey.app.registered.application=Registering the Jersey servlet application, named {0}, with the Application class of the same name.
47+
servlet.async.context.already.started=Servlet request has been put into asynchronous mode by an external force. \
48+
Proceeding with the existing AsyncContext instance, but cannot guarantee the correct behavior of JAX-RS AsyncResponse time-out support.

0 commit comments

Comments
 (0)