下面列出了怎么用org.eclipse.jetty.server.HttpChannelState的API类实例代码及写法,或者点击链接到github查看源代码。
void onAsyncComplete(AsyncEvent event) {
HttpChannelState state = ((AsyncContextEvent) event).getHttpChannelState();
Request request = state.getBaseRequest();
Timer.Sample sample = (Timer.Sample) request.getAttribute(SAMPLE_REQUEST_TIMER_ATTRIBUTE);
LongTaskTimer.Sample lttSample = (LongTaskTimer.Sample) request.getAttribute(SAMPLE_REQUEST_LONG_TASK_TIMER_ATTRIBUTE);
if (sample != null) {
sample.stop(Timer.builder("jetty.server.requests")
.description("HTTP requests to the Jetty server")
.tags(tagsProvider.getTags(request, request.getResponse()))
.tags(tags)
.register(registry));
lttSample.stop();
}
asyncWaits.decrementAndGet();
// If we have no more dispatches, should we signal shutdown?
FutureCallback shutdownCallback = shutdown.get();
if (shutdownCallback != null && openRequests.activeTasks() == 0) {
shutdownCallback.succeeded();
}
}
@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);
}
}
}
void onAsyncTimeout(AsyncEvent event) {
asyncExpires.increment();
HttpChannelState state = ((AsyncContextEvent) event).getHttpChannelState();
Request request = state.getBaseRequest();
LongTaskTimer.Sample lttSample = (LongTaskTimer.Sample) request.getAttribute(SAMPLE_REQUEST_LONG_TASK_TIMER_ATTRIBUTE);
lttSample.stop();
}
@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.
}
}
@Override
public HttpChannelState getHttpChannelState() {
return this.state;
}
@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.
}
}