Search in sources :

Example 16 with AsyncListener

use of javax.servlet.AsyncListener in project undertow by undertow-io.

the class OnCompleteServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext ctx = req.startAsync();
    ctx.addListener(new AsyncListener() {

        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            QUEUE.add("onComplete");
        }

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {
            QUEUE.add("onTimeout");
        }

        @Override
        public void onError(AsyncEvent event) throws IOException {
            QUEUE.add("onError");
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
            QUEUE.add("onStartAsync");
        }
    });
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            ctx.dispatch("/message");
        }
    });
    thread.start();
}
Also used : AsyncListener(javax.servlet.AsyncListener) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent)

Example 17 with AsyncListener

use of javax.servlet.AsyncListener in project undertow by undertow-io.

the class CompleteAsyncServlet method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    req.startAsync();
    req.getAsyncContext().addListener(new AsyncListener() {

        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            TestListener.addMessage("onComplete");
        }

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {
        }

        @Override
        public void onError(AsyncEvent event) throws IOException {
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
            TestListener.addMessage("onStartAsync");
        }
    });
    req.getAsyncContext().complete();
    resp.getWriter().write("asynccomplete");
}
Also used : AsyncListener(javax.servlet.AsyncListener) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent)

Example 18 with AsyncListener

use of javax.servlet.AsyncListener in project tomee by apache.

the class OpenEJBAsyncContext method onError.

private void onError(final Throwable ignored) {
    for (final AsyncListener listener : listeners) {
        try {
            listener.onError(event);
        } catch (final IOException t) {
            throw new OpenEJBRuntimeException(t);
        }
    }
    try {
        HttpServletResponse.class.cast(response).sendError(HttpURLConnection.HTTP_INTERNAL_ERROR);
        commit();
    } catch (final IOException e) {
    // no-op
    }
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) AsyncListener(javax.servlet.AsyncListener) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 19 with AsyncListener

use of javax.servlet.AsyncListener in project tomee by apache.

the class OpenEJBAsyncContext method destroy.

public static void destroy() {
    if (es == null) {
        return;
    }
    es.shutdownNow();
    for (final OpenEJBAsyncContext ctx : new ArrayList<>(INITIALIZED)) {
        if (ctx.lastTouch + ctx.getTimeout() < System.currentTimeMillis()) {
            for (final AsyncListener listener : ctx.listeners) {
                try {
                    listener.onTimeout(ctx.event);
                } catch (final IOException t) {
                    throw new OpenEJBRuntimeException(t);
                }
            }
            ctx.complete();
        }
    }
    INITIALIZED.clear();
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) AsyncListener(javax.servlet.AsyncListener) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 20 with AsyncListener

use of javax.servlet.AsyncListener 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 = ServletHelper.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

AsyncListener (javax.servlet.AsyncListener)45 AsyncContext (javax.servlet.AsyncContext)32 AsyncEvent (javax.servlet.AsyncEvent)30 IOException (java.io.IOException)28 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 ServletException (javax.servlet.ServletException)12 Test (org.junit.Test)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 CountDownLatch (java.util.concurrent.CountDownLatch)5 HttpServlet (javax.servlet.http.HttpServlet)5 OutputStream (java.io.OutputStream)4 Socket (java.net.Socket)3 URISyntaxException (java.net.URISyntaxException)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 Request (org.eclipse.jetty.server.Request)3 Locker (org.eclipse.jetty.util.thread.Locker)3 RequestContext (com.linkedin.r2.message.RequestContext)2 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)2 TransportResponse (com.linkedin.r2.transport.common.bridge.common.TransportResponse)2