Java Code Examples for org.eclipse.jetty.server.HttpChannelState#addListener()

The following examples show how to use org.eclipse.jetty.server.HttpChannelState#addListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpResponseStatisticsCollector.java    From vespa with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(String path, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    inFlight.incrementAndGet();

    /* The control flow logic here is mostly a copy from org.eclipse.jetty.server.handler.StatisticsHandler.handle(..) */
    try {
        Handler handler = getHandler();
        if (handler != null && shutdown.get() == null && isStarted()) {
            handler.handle(path, baseRequest, request, response);
        } else if (!baseRequest.isHandled()) {
            baseRequest.setHandled(true);
            response.sendError(HttpStatus.SERVICE_UNAVAILABLE_503);
        }
    } finally {
        HttpChannelState state = baseRequest.getHttpChannelState();

        if (state.isSuspended()) {
            if (state.isInitial()) {
                state.addListener(completionWatcher);
            }
        } else if (state.isInitial()) {
            observeEndOfRequest(baseRequest, response);
        }
    }
}
 
Example 2
Source File: TimedHandler.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(String path, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Timer.Sample sample = Timer.start(registry);
    LongTaskTimer.Sample requestSample;

    HttpChannelState state = baseRequest.getHttpChannelState();
    if (state.isInitial()) {
        requestSample = openRequests.start();
        request.setAttribute(SAMPLE_REQUEST_TIMER_ATTRIBUTE, sample);
        request.setAttribute(SAMPLE_REQUEST_LONG_TASK_TIMER_ATTRIBUTE, requestSample);
    } else {
        asyncDispatches.increment();
        request.setAttribute(SAMPLE_REQUEST_TIMER_ATTRIBUTE, sample);
        requestSample = (LongTaskTimer.Sample) request.getAttribute(SAMPLE_REQUEST_LONG_TASK_TIMER_ATTRIBUTE);
    }

    try {
        Handler handler = getHandler();
        if (handler != null && !shutdown.isShutdown() && isStarted()) {
            handler.handle(path, baseRequest, request, response);
        } else {
            if (!baseRequest.isHandled()) {
                baseRequest.setHandled(true);
            }
            if (!baseRequest.getResponse().isCommitted()) {
                response.sendError(HttpStatus.SERVICE_UNAVAILABLE_503);
            }
        }
    } finally {
        if (state.isSuspended()) {
            if (state.isInitial()) {
                state.addListener(onCompletion);
                asyncWaits.incrementAndGet();
            }
        } else if (state.isInitial()) {
            sample.stop(Timer.builder("jetty.server.requests")
                    .description("HTTP requests to the Jetty server")
                    .tags(tagsProvider.getTags(request, response))
                    .tags(tags)
                    .register(registry));

            requestSample.stop();

            // If we have no more dispatches, should we signal shutdown?
            FutureCallback shutdownCallback = shutdown.get();
            if (shutdownCallback != null) {
                response.flushBuffer();
                if (openRequests.activeTasks() == 0) {
                    shutdownCallback.succeeded();
                }
            }
        }
        // else onCompletion will handle it.
    }
}
 
Example 3
Source File: InstrumentedHandler.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(String path,
                   Request request,
                   HttpServletRequest httpRequest,
                   HttpServletResponse httpResponse) throws IOException, ServletException {

    activeDispatches.inc();

    final long start;
    final HttpChannelState state = request.getHttpChannelState();
    if (state.isInitial()) {
        // new request
        activeRequests.inc();
        start = request.getTimeStamp();
    } else {
        // resumed request
        start = System.currentTimeMillis();
        activeSuspended.dec();
        if (state.getState() == State.DISPATCHED) {
            asyncDispatches.mark();
        }
    }

    try {
        super.handle(path, request, httpRequest, httpResponse);
    } finally {
        final long now = System.currentTimeMillis();
        final long dispatched = now - start;

        activeDispatches.dec();
        dispatches.update(dispatched, TimeUnit.MILLISECONDS);

        if (state.isSuspended()) {
            if (state.isInitial()) {
                state.addListener(listener);
            }
            activeSuspended.inc();
        } else if (state.isInitial()) {
            requests.update(dispatched, TimeUnit.MILLISECONDS);
            updateResponses(request);
        }
        // else onCompletion will handle it.
    }
}