Search in sources :

Example 16 with AsyncEvent

use of javax.servlet.AsyncEvent in project spring-framework by spring-projects.

the class StandardServletAsyncWebRequestTests method onTimeoutHandler.

@Test
public void onTimeoutHandler() throws Exception {
    Runnable timeoutHandler = mock(Runnable.class);
    this.asyncRequest.addTimeoutHandler(timeoutHandler);
    this.asyncRequest.onTimeout(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
    verify(timeoutHandler).run();
}
Also used : MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 17 with AsyncEvent

use of javax.servlet.AsyncEvent in project spring-framework by spring-projects.

the class StandardServletAsyncWebRequestTests method onCompletionHandler.

@Test
public void onCompletionHandler() throws Exception {
    Runnable handler = mock(Runnable.class);
    this.asyncRequest.addCompletionHandler(handler);
    this.asyncRequest.startAsync();
    this.asyncRequest.onComplete(new AsyncEvent(this.request.getAsyncContext()));
    verify(handler).run();
    assertTrue(this.asyncRequest.isAsyncComplete());
}
Also used : AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 18 with AsyncEvent

use of javax.servlet.AsyncEvent in project spring-framework by spring-projects.

the class StandardServletAsyncWebRequestTests method onTimeoutDefaultBehavior.

@Test
public void onTimeoutDefaultBehavior() throws Exception {
    this.asyncRequest.onTimeout(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
    assertEquals(200, this.response.getStatus());
}
Also used : MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 19 with AsyncEvent

use of javax.servlet.AsyncEvent in project spring-framework by spring-projects.

the class StandardServletAsyncWebRequestTests method onCompletionHandlerAfterOnErrorEvent.

// SPR-13292
@Test
public void onCompletionHandlerAfterOnErrorEvent() throws Exception {
    Runnable handler = mock(Runnable.class);
    this.asyncRequest.addCompletionHandler(handler);
    this.asyncRequest.startAsync();
    this.asyncRequest.onError(new AsyncEvent(this.request.getAsyncContext()));
    verify(handler).run();
    assertTrue(this.asyncRequest.isAsyncComplete());
}
Also used : AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 20 with AsyncEvent

use of javax.servlet.AsyncEvent in project rest.li by linkedin.

the class AbstractAsyncR2Servlet method service.

@Override
public void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    RequestContext requestContext = readRequestContext(req);
    RestRequest restRequest;
    try {
        restRequest = readFromServletRequest(req);
    } catch (URISyntaxException e) {
        writeToServletError(resp, RestStatus.BAD_REQUEST, e.toString());
        return;
    }
    final AsyncContext ctx = req.startAsync(req, resp);
    ctx.setTimeout(_timeout);
    ctx.addListener(new AsyncListener() {

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {
            AsyncContext ctx = event.getAsyncContext();
            writeToServletError((HttpServletResponse) ctx.getResponse(), RestStatus.INTERNAL_SERVER_ERROR, "Server Timeout");
            ctx.complete();
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
        // Nothing to do here
        }

        @Override
        public void onError(AsyncEvent event) throws IOException {
            writeToServletError((HttpServletResponse) event.getSuppliedResponse(), RestStatus.INTERNAL_SERVER_ERROR, "Server Error");
            ctx.complete();
        }

        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            Object exception = req.getAttribute(TRANSPORT_CALLBACK_IOEXCEPTION);
            if (exception != null)
                throw new IOException((IOException) exception);
        }
    });
    TransportCallback<RestResponse> callback = new TransportCallback<RestResponse>() {

        @Override
        public void onResponse(final TransportResponse<RestResponse> response) {
            // TransportCallback is usually invoked by non-servlet threads; hence we cannot assume that it's ok to
            // do blocking IO there. As a result, we should use AsyncContext.start() to do blocking IO using the
            // container/servlet threads. This still maintains the advantage of Async, meaning servlet thread is not
            // blocking-wait when the response is not ready.
            ctx.start(new Runnable() {

                @Override
                public void run() {
                    try {
                        writeToServletResponse(response, (HttpServletResponse) ctx.getResponse());
                    } catch (IOException e) {
                        req.setAttribute(TRANSPORT_CALLBACK_IOEXCEPTION, e);
                    } finally {
                        ctx.complete();
                    }
                }
            });
        }
    };
    getDispatcher().handleRequest(restRequest, requestContext, callback);
}
Also used : TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestResponse(com.linkedin.r2.message.rest.RestResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent) TransportResponse(com.linkedin.r2.transport.common.bridge.common.TransportResponse) RestRequest(com.linkedin.r2.message.rest.RestRequest) AsyncListener(javax.servlet.AsyncListener) RequestContext(com.linkedin.r2.message.RequestContext)

Aggregations

AsyncEvent (javax.servlet.AsyncEvent)39 IOException (java.io.IOException)27 Test (org.junit.Test)21 AsyncListener (javax.servlet.AsyncListener)20 AsyncContext (javax.servlet.AsyncContext)19 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)16 ServletException (javax.servlet.ServletException)12 HttpServlet (javax.servlet.http.HttpServlet)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 OutputStream (java.io.OutputStream)4 Request (org.eclipse.jetty.server.Request)4 Socket (java.net.Socket)3 URISyntaxException (java.net.URISyntaxException)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 ServletResponse (javax.servlet.ServletResponse)3 HttpTester (org.eclipse.jetty.http.HttpTester)3 MockAsyncContext (org.springframework.mock.web.test.MockAsyncContext)3 RequestContext (com.linkedin.r2.message.RequestContext)2