Search in sources :

Example 56 with AsyncContext

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

the class AsyncInputStreamServlet method doPost.

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext context = req.startAsync();
    final ServletOutputStream outputStream = resp.getOutputStream();
    ServletInputStream inputStream = req.getInputStream();
    final MyListener listener = new MyListener(outputStream, inputStream, context);
    inputStream.setReadListener(listener);
    outputStream.setWriteListener(listener);
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) ServletOutputStream(javax.servlet.ServletOutputStream) AsyncContext(javax.servlet.AsyncContext)

Example 57 with AsyncContext

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

the class AnotherAsyncServlet method doGet.

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

        @Override
        public void run() {
            try {
                Thread.sleep(100);
                resp.setContentType("text/plain");
                resp.getWriter().write(AnotherAsyncServlet.class.getSimpleName());
                ctx.complete();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
    t.start();
}
Also used : AsyncContext(javax.servlet.AsyncContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 58 with AsyncContext

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

the class AnotherAsyncServlet method doGet.

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

        @Override
        public void run() {
            try {
                Thread.sleep(100);
                resp.setContentType("text/plain");
                resp.getWriter().write(AnotherAsyncServlet.class.getSimpleName());
                ctx.complete();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
    t.start();
}
Also used : AsyncContext(javax.servlet.AsyncContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 59 with AsyncContext

use of javax.servlet.AsyncContext 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 60 with AsyncContext

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

the class AsyncServlet method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    AsyncContext ctx = req.startAsync();
    // make the request timeout
    ctx.setTimeout(100L);
    ctx.addListener(new SimpleAsyncListener());
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    t.start();
}
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