Search in sources :

Example 21 with AsyncEvent

use of javax.servlet.AsyncEvent in project metrics by dropwizard.

the class InstrumentedHandler method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    final String prefix = this.prefix == null ? name(getHandler().getClass(), name) : name(this.prefix, name);
    this.requests = metricRegistry.timer(name(prefix, "requests"));
    this.dispatches = metricRegistry.timer(name(prefix, "dispatches"));
    this.activeRequests = metricRegistry.counter(name(prefix, "active-requests"));
    this.activeDispatches = metricRegistry.counter(name(prefix, "active-dispatches"));
    this.activeSuspended = metricRegistry.counter(name(prefix, "active-suspended"));
    this.asyncDispatches = metricRegistry.meter(name(prefix, "async-dispatches"));
    this.asyncTimeouts = metricRegistry.meter(name(prefix, "async-timeouts"));
    this.responses = new Meter[] { // 1xx
    metricRegistry.meter(name(prefix, "1xx-responses")), // 2xx
    metricRegistry.meter(name(prefix, "2xx-responses")), // 3xx
    metricRegistry.meter(name(prefix, "3xx-responses")), // 4xx
    metricRegistry.meter(name(prefix, "4xx-responses")), // 5xx
    metricRegistry.meter(name(prefix, "5xx-responses")) };
    this.getRequests = metricRegistry.timer(name(prefix, "get-requests"));
    this.postRequests = metricRegistry.timer(name(prefix, "post-requests"));
    this.headRequests = metricRegistry.timer(name(prefix, "head-requests"));
    this.putRequests = metricRegistry.timer(name(prefix, "put-requests"));
    this.deleteRequests = metricRegistry.timer(name(prefix, "delete-requests"));
    this.optionsRequests = metricRegistry.timer(name(prefix, "options-requests"));
    this.traceRequests = metricRegistry.timer(name(prefix, "trace-requests"));
    this.connectRequests = metricRegistry.timer(name(prefix, "connect-requests"));
    this.moveRequests = metricRegistry.timer(name(prefix, "move-requests"));
    this.otherRequests = metricRegistry.timer(name(prefix, "other-requests"));
    metricRegistry.register(name(prefix, "percent-4xx-1m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[3].getOneMinuteRate(), requests.getOneMinuteRate());
        }
    });
    metricRegistry.register(name(prefix, "percent-4xx-5m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[3].getFiveMinuteRate(), requests.getFiveMinuteRate());
        }
    });
    metricRegistry.register(name(prefix, "percent-4xx-15m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[3].getFifteenMinuteRate(), requests.getFifteenMinuteRate());
        }
    });
    metricRegistry.register(name(prefix, "percent-5xx-1m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[4].getOneMinuteRate(), requests.getOneMinuteRate());
        }
    });
    metricRegistry.register(name(prefix, "percent-5xx-5m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[4].getFiveMinuteRate(), requests.getFiveMinuteRate());
        }
    });
    metricRegistry.register(name(prefix, "percent-5xx-15m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[4].getFifteenMinuteRate(), requests.getFifteenMinuteRate());
        }
    });
    this.listener = new AsyncListener() {

        private long startTime;

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {
            asyncTimeouts.mark();
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
            startTime = System.currentTimeMillis();
            event.getAsyncContext().addListener(this);
        }

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

        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            final AsyncContextState state = (AsyncContextState) event.getAsyncContext();
            final HttpServletRequest request = (HttpServletRequest) state.getRequest();
            final HttpServletResponse response = (HttpServletResponse) state.getResponse();
            updateResponses(request, response, startTime);
            if (state.getHttpChannelState().getState() != HttpChannelState.State.DISPATCHED) {
                activeSuspended.dec();
            }
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AsyncContextState(org.eclipse.jetty.server.AsyncContextState) RatioGauge(com.codahale.metrics.RatioGauge) AsyncListener(javax.servlet.AsyncListener) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent)

Example 22 with AsyncEvent

use of javax.servlet.AsyncEvent in project chassis by Kixeye.

the class InstrumentedHandler method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    final String prefix = name(getHandler().getClass(), name);
    this.requests = timer(name(prefix, "requests"));
    this.dispatches = timer(name(prefix, "dispatches"));
    this.activeRequests = metricRegistry.counter(name(prefix, "active-requests"));
    this.activeDispatches = metricRegistry.counter(name(prefix, "active-dispatches"));
    this.activeSuspended = metricRegistry.counter(name(prefix, "active-suspended"));
    this.asyncDispatches = metricRegistry.meter(name(prefix, "async-dispatches"));
    this.asyncTimeouts = metricRegistry.meter(name(prefix, "async-timeouts"));
    this.responses = new Meter[] { // 1xx
    metricRegistry.meter(name(prefix, "1xx-responses")), // 2xx
    metricRegistry.meter(name(prefix, "2xx-responses")), // 3xx
    metricRegistry.meter(name(prefix, "3xx-responses")), // 4xx
    metricRegistry.meter(name(prefix, "4xx-responses")), // 5xx
    metricRegistry.meter(name(prefix, "5xx-responses")) };
    this.getRequests = timer(name(prefix, "get-requests"));
    this.postRequests = timer(name(prefix, "post-requests"));
    this.headRequests = timer(name(prefix, "head-requests"));
    this.putRequests = timer(name(prefix, "put-requests"));
    this.deleteRequests = timer(name(prefix, "delete-requests"));
    this.optionsRequests = timer(name(prefix, "options-requests"));
    this.traceRequests = timer(name(prefix, "trace-requests"));
    this.connectRequests = timer(name(prefix, "connect-requests"));
    this.moveRequests = timer(name(prefix, "move-requests"));
    this.otherRequests = timer(name(prefix, "other-requests"));
    this.listener = new AsyncListener() {

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {
            asyncTimeouts.mark();
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
            event.getAsyncContext().addListener(this);
        }

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

        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            final AsyncContextState state = (AsyncContextState) event.getAsyncContext();
            final Request request = (Request) state.getRequest();
            updateResponses(request);
            if (!(state.getHttpChannelState().getState() == State.DISPATCHED)) {
                activeSuspended.dec();
            }
        }
    };
}
Also used : AsyncContextState(org.eclipse.jetty.server.AsyncContextState) AsyncListener(javax.servlet.AsyncListener) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent)

Example 23 with AsyncEvent

use of javax.servlet.AsyncEvent 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 24 with AsyncEvent

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

the class AsyncContextImpl method onAsyncComplete.

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

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

Example 25 with AsyncEvent

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

the class AsyncContextImpl method onAsyncStart.

private void onAsyncStart(final AsyncContext newAsyncContext) {
    final boolean setupRequired = SecurityActions.currentServletRequestContext() == null;
    servletRequestContext.getCurrentServletContext().invokeRunnable(servletRequestContext.getExchange(), new Runnable() {

        @Override
        public void run() {
            //now run request listeners
            setupRequestContext(setupRequired);
            try {
                for (final BoundAsyncListener listener : asyncListeners) {
                    //make sure we use the new async context
                    AsyncEvent event = new AsyncEvent(newAsyncContext, listener.servletRequest, listener.servletResponse);
                    try {
                        listener.asyncListener.onStartAsync(event);
                    } catch (IOException e) {
                        UndertowServletLogger.REQUEST_LOGGER.ioExceptionDispatchingAsyncEvent(e);
                    }
                }
            } finally {
                tearDownRequestContext(setupRequired);
            }
        }
    });
}
Also used : IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent)

Aggregations

AsyncEvent (javax.servlet.AsyncEvent)39 IOException (java.io.IOException)27 Test (org.junit.Test)21 AsyncListener (javax.servlet.AsyncListener)20 AsyncContext (javax.servlet.AsyncContext)19 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)16 ServletException (javax.servlet.ServletException)12 HttpServlet (javax.servlet.http.HttpServlet)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 OutputStream (java.io.OutputStream)4 Request (org.eclipse.jetty.server.Request)4 Socket (java.net.Socket)3 URISyntaxException (java.net.URISyntaxException)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 ServletResponse (javax.servlet.ServletResponse)3 HttpTester (org.eclipse.jetty.http.HttpTester)3 MockAsyncContext (org.springframework.mock.web.test.MockAsyncContext)3 RequestContext (com.linkedin.r2.message.RequestContext)2