Search in sources :

Example 41 with AsyncEvent

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

the class AsyncServlet5 method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AsyncContext ctx = req.startAsync();
    resp.setContentType("text/event-stream");
    resp.setHeader("Cache-Control", "no-cache");
    ctx.setTimeout(10_000L);
    ctx.addListener(new AsyncEventListener() {

        @Override
        public void onError(AsyncEvent event) throws IOException {
            super.onError(event);
            latch.countDown();
        }
    });
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            final ScheduledFuture<?> scheduledFuture = executorService.scheduleAtFixedRate(new Job(ctx), 0, 1, SECONDS);
            try {
                latch.await(11, SECONDS);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            scheduledFuture.cancel(true);
            ctx.complete();
        }
    });
    t.start();
}
Also used : AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncEvent(javax.servlet.AsyncEvent) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 42 with AsyncEvent

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

the class AsyncContextImpl method onAsyncError.

private void onAsyncError(final Throwable t) {
    final boolean setupRequired = SecurityActions.currentServletRequestContext() == null;
    servletRequestContext.getCurrentServletContext().invokeRunnable(servletRequestContext.getExchange(), new Runnable() {

        @Override
        public void run() {
            setupRequestContext(setupRequired);
            try {
                for (final BoundAsyncListener listener : asyncListeners) {
                    AsyncEvent event = new AsyncEvent(AsyncContextImpl.this, listener.servletRequest, listener.servletResponse, t);
                    try {
                        listener.asyncListener.onError(event);
                    } catch (IOException e) {
                        UndertowServletLogger.REQUEST_LOGGER.ioExceptionDispatchingAsyncEvent(e);
                    } catch (Throwable t) {
                        UndertowServletLogger.REQUEST_LOGGER.failureDispatchingAsyncEvent(t);
                    }
                }
            } finally {
                tearDownRequestContext(setupRequired);
            }
        }
    });
}
Also used : IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent)

Example 43 with AsyncEvent

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

the class AsyncErrorListenerServlet method doGet.

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

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

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

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

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
        }
    });
    throw new RuntimeException("FAILED");
}
Also used : AsyncListener(javax.servlet.AsyncListener) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent)

Example 44 with AsyncEvent

use of javax.servlet.AsyncEvent in project ddf by codice.

the class ServletMetricsTest method asyncDoFilterError.

@Test
public void asyncDoFilterError() throws Exception {
    when(mockRequest.isAsyncStarted()).thenReturn(true);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(mockRequest.getAsyncContext()).thenReturn(asyncContext);
    AsyncEvent event = mock(AsyncEvent.class);
    when(event.getSuppliedRequest()).thenReturn(mockRequest);
    when(event.getSuppliedResponse()).thenReturn(mockResponse);
    underTest.doFilter(mockRequest, mockResponse, mockFilterChain);
    ArgumentCaptor<AsyncListener> arg = ArgumentCaptor.forClass(AsyncListener.class);
    verify(asyncContext).addListener(arg.capture());
    AsyncListener listener = arg.getValue();
    listener.onStartAsync(event);
    listener.onError(event);
    listener.onComplete(event);
    assertThat(meterRegistry.summary(LATENCY, getTags(DEFAULT_METHOD, 500)).count(), is(1L));
}
Also used : AsyncListener(javax.servlet.AsyncListener) AsyncContext(javax.servlet.AsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 45 with AsyncEvent

use of javax.servlet.AsyncEvent in project aws-xray-sdk-java by aws.

the class AWSXRayServletFilterTest method testAWSXRayServletAsyncListenerEmitsSegmentWhenProcessingEvent.

@Test
public void testAWSXRayServletAsyncListenerEmitsSegmentWhenProcessingEvent() throws IOException, ServletException {
    AWSXRayServletFilter servletFilter = new AWSXRayServletFilter("test");
    AsyncContext asyncContext = mock(AsyncContext.class);
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRequestURL()).thenReturn(new StringBuffer("test_url"));
    when(request.getMethod()).thenReturn("TEST_METHOD");
    when(request.isAsyncStarted()).thenReturn(true);
    when(request.getAsyncContext()).thenReturn(asyncContext);
    HttpServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = mockChain(request, response);
    AsyncEvent event = mock(AsyncEvent.class);
    when(event.getSuppliedRequest()).thenReturn(request);
    when(event.getSuppliedResponse()).thenReturn(response);
    servletFilter.doFilter(request, response, chain);
    Assert.assertNull(AWSXRay.getTraceEntity());
    AWSXRayServletAsyncListener listener = (AWSXRayServletAsyncListener) Whitebox.getInternalState(servletFilter, "listener");
    listener.onComplete(event);
    verify(AWSXRay.getGlobalRecorder().getEmitter(), Mockito.times(1)).sendSegment(Mockito.any());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Aggregations

AsyncEvent (javax.servlet.AsyncEvent)51 IOException (java.io.IOException)38 AsyncContext (javax.servlet.AsyncContext)35 AsyncListener (javax.servlet.AsyncListener)32 HttpServletResponse (javax.servlet.http.HttpServletResponse)23 HttpServletRequest (javax.servlet.http.HttpServletRequest)20 Test (org.junit.Test)19 ServletException (javax.servlet.ServletException)12 HttpServlet (javax.servlet.http.HttpServlet)9 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ServletResponse (javax.servlet.ServletResponse)5 OutputStream (java.io.OutputStream)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 FilterChain (javax.servlet.FilterChain)4 Socket (java.net.Socket)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 HttpTester (org.eclipse.jetty.http.HttpTester)3