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();
}
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);
}
}
});
}
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");
}
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));
}
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());
}
Aggregations