Search in sources :

Example 61 with AsyncContext

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

the class ConcurrencyServlet method service.

@Override
protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext asyncCtx = req.startAsync();
    execService.execute(new Runnable() {

        @Override
        public void run() {
            try {
                req.login("test", "secret");
                resp.getWriter().println(user.getUser());
            } catch (final Exception e) {
                try {
                    e.printStackTrace(resp.getWriter());
                } catch (final IOException e1) {
                    throw new IllegalStateException(e);
                }
            } finally {
                asyncCtx.complete();
            }
        }
    });
}
Also used : AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 62 with AsyncContext

use of javax.servlet.AsyncContext in project AngularBeans by bessemHmidi.

the class SockJsServlet method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setHeader("Access-Control-Allow-Origin", "true");
    log.log(Level.FINE, "SockJsServlet#service for {0} {1}", new Object[] { req.getMethod(), req.getPathInfo() });
    AsyncContext asyncContext = req.startAsync();
    // no timeout
    asyncContext.setTimeout(0);
    SockJsServletRequest sockJsReq = new SockJsServletRequest(req);
    SockJsServletResponse sockJsRes = new SockJsServletResponse(res, asyncContext);
    try {
        sockJsServer.dispatch(sockJsReq, sockJsRes);
    } catch (SockJsException ex) {
        throw new ServletException("Error during SockJS request:", ex);
    }
    if ("application/x-www-form-urlencoded".equals(req.getHeader("Content-Type"))) {
        // Let the servlet parse data and just pretend like we did
        sockJsReq.onAllDataRead();
    } else if (req.isAsyncStarted()) {
        req.getInputStream().setReadListener(sockJsReq);
    }
}
Also used : ServletException(javax.servlet.ServletException) SockJsException(org.projectodd.sockjs.SockJsException) AsyncContext(javax.servlet.AsyncContext)

Example 63 with AsyncContext

use of javax.servlet.AsyncContext in project tomcat by apache.

the class Async1 method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext actx = req.startAsync();
    actx.setTimeout(30 * 1000);
    Runnable run = new Runnable() {

        @Override
        public void run() {
            try {
                String path = "/jsp/async/async1.jsp";
                Thread.currentThread().setName("Async1-Thread");
                log.info("Putting AsyncThread to sleep");
                Thread.sleep(2 * 1000);
                log.info("Dispatching to " + path);
                actx.dispatch(path);
            } catch (InterruptedException x) {
                log.error("Async1", x);
            } catch (IllegalStateException x) {
                log.error("Async1", x);
            }
        }
    };
    Thread t = new Thread(run);
    t.start();
}
Also used : AsyncContext(javax.servlet.AsyncContext)

Example 64 with AsyncContext

use of javax.servlet.AsyncContext in project tomcat by apache.

the class Async2 method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext actx = req.startAsync();
    actx.setTimeout(30 * 1000);
    Runnable run = new Runnable() {

        @Override
        public void run() {
            try {
                Thread.currentThread().setName("Async2-Thread");
                log.info("Putting AsyncThread to sleep");
                Thread.sleep(2 * 1000);
                log.info("Writing data.");
                Date date = new Date(System.currentTimeMillis());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
                actx.getResponse().getWriter().write("Output from background thread. Time: " + sdf.format(date) + "\n");
                actx.complete();
            } catch (InterruptedException x) {
                log.error("Async2", x);
            } catch (IllegalStateException x) {
                log.error("Async2", x);
            } catch (IOException x) {
                log.error("Async2", x);
            }
        }
    };
    Thread t = new Thread(run);
    t.start();
}
Also used : AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 65 with AsyncContext

use of javax.servlet.AsyncContext in project tomcat by apache.

the class Async3 method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext actx = req.startAsync();
    actx.setTimeout(30 * 1000);
    actx.dispatch("/jsp/async/async3.jsp");
}
Also used : AsyncContext(javax.servlet.AsyncContext)

Aggregations

AsyncContext (javax.servlet.AsyncContext)120 IOException (java.io.IOException)61 HttpServletRequest (javax.servlet.http.HttpServletRequest)53 ServletException (javax.servlet.ServletException)52 HttpServletResponse (javax.servlet.http.HttpServletResponse)50 Test (org.junit.Test)43 CountDownLatch (java.util.concurrent.CountDownLatch)33 HttpServlet (javax.servlet.http.HttpServlet)32 InterruptedIOException (java.io.InterruptedIOException)24 ServletOutputStream (javax.servlet.ServletOutputStream)20 ReadListener (javax.servlet.ReadListener)19 ServletInputStream (javax.servlet.ServletInputStream)19 AsyncEvent (javax.servlet.AsyncEvent)18 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)18 AsyncListener (javax.servlet.AsyncListener)15 UncheckedIOException (java.io.UncheckedIOException)14 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)14 Request (org.eclipse.jetty.server.Request)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 WriteListener (javax.servlet.WriteListener)11