Search in sources :

Example 1 with TimeoutHandler

use of org.glassfish.grizzly.http.server.TimeoutHandler in project Payara by payara.

the class Request method startAsync.

/**
 * Starts async processing on this request.
 *
 * @param servletRequest the ServletRequest with which to initialize
 * the AsyncContext
 * @param servletResponse the ServletResponse with which to initialize
 * the AsyncContext
 * @param isStartAsyncWithZeroArg true if the zero-arg version of
 * startAsync was called, false otherwise
 */
private AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse, boolean isStartAsyncWithZeroArg) throws IllegalStateException {
    if (servletRequest == null || servletResponse == null) {
        throw new IllegalArgumentException("Null request or response");
    }
    if (!isAsyncSupported()) {
        throw new IllegalStateException(rb.getString(LogFacade.REQUEST_WITHIN_SCOPE_OF_FILTER_OR_SERVLET_EXCEPTION));
    }
    final AsyncContextImpl asyncContextLocal = asyncContext;
    if (asyncContextLocal != null) {
        if (isAsyncStarted()) {
            throw new IllegalStateException(rb.getString(LogFacade.START_ASYNC_CALLED_AGAIN_EXCEPTION));
        }
        if (asyncContextLocal.isAsyncComplete()) {
            throw new IllegalStateException(rb.getString(LogFacade.ASYNC_ALREADY_COMPLETE_EXCEPTION));
        }
        if (!asyncContextLocal.isStartAsyncInScope()) {
            throw new IllegalStateException(rb.getString(LogFacade.START_ASYNC_CALLED_OUTSIDE_SCOPE_EXCEPTION));
        }
        // Reinitialize existing AsyncContext
        asyncContextLocal.reinitialize(servletRequest, servletResponse, isStartAsyncWithZeroArg);
    } else {
        final AsyncContextImpl asyncContextFinal = new AsyncContextImpl(this, servletRequest, (Response) getResponse(), servletResponse, isStartAsyncWithZeroArg);
        asyncContext = asyncContextFinal;
        final CompletionHandler<org.glassfish.grizzly.http.server.Response> requestCompletionHandler = new EmptyCompletionHandler<org.glassfish.grizzly.http.server.Response>() {

            @Override
            public void completed(org.glassfish.grizzly.http.server.Response response) {
                asyncContextFinal.notifyAsyncListeners(AsyncContextImpl.AsyncEventType.COMPLETE, null);
            }
        };
        final TimeoutHandler timeoutHandler = new TimeoutHandler() {

            @Override
            public boolean onTimeout(final org.glassfish.grizzly.http.server.Response response) {
                return processTimeout();
            }
        };
        coyoteRequest.getResponse().suspend(-1, TimeUnit.MILLISECONDS, requestCompletionHandler, timeoutHandler);
        asyncStartedThread = Thread.currentThread();
    }
    asyncStarted.set(true);
    return asyncContext;
}
Also used : HttpResponse(org.apache.catalina.HttpResponse) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) TimeoutHandler(org.glassfish.grizzly.http.server.TimeoutHandler) EmptyCompletionHandler(org.glassfish.grizzly.EmptyCompletionHandler)

Aggregations

ServletResponse (javax.servlet.ServletResponse)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpResponse (org.apache.catalina.HttpResponse)1 EmptyCompletionHandler (org.glassfish.grizzly.EmptyCompletionHandler)1 TimeoutHandler (org.glassfish.grizzly.http.server.TimeoutHandler)1