use of org.eclipse.jetty.server.handler.StatisticsHandler in project jetty.project by eclipse.
the class StatisticsServletTest method getStats.
@Test
public void getStats() throws Exception {
StatisticsHandler statsHandler = new StatisticsHandler();
_server.setHandler(statsHandler);
ServletContextHandler statsContext = new ServletContextHandler(statsHandler, "/");
statsContext.addServlet(new ServletHolder(new TestServlet()), "/test1");
ServletHolder servletHolder = new ServletHolder(new StatisticsServlet());
servletHolder.setInitParameter("restrictToLocalhost", "false");
statsContext.addServlet(servletHolder, "/stats");
statsContext.setSessionHandler(new SessionHandler());
_server.start();
getResponse("/test1");
String response = getResponse("/stats?xml=true");
Stats stats = parseStats(response);
Assert.assertEquals(1, stats.responses2xx);
getResponse("/stats?statsReset=true");
response = getResponse("/stats?xml=true");
stats = parseStats(response);
Assert.assertEquals(1, stats.responses2xx);
getResponse("/test1");
getResponse("/nothing");
response = getResponse("/stats?xml=true");
stats = parseStats(response);
Assert.assertEquals(3, stats.responses2xx);
Assert.assertEquals(1, stats.responses4xx);
}
use of org.eclipse.jetty.server.handler.StatisticsHandler in project felix by apache.
the class JettyService method initializeJetty.
private void initializeJetty() throws Exception {
if (this.config.isUseHttp() || this.config.isUseHttps()) {
final int threadPoolMax = this.config.getThreadPoolMax();
if (threadPoolMax >= 0) {
this.server = new Server(new QueuedThreadPool(threadPoolMax));
} else {
this.server = new Server();
}
this.server.addLifeCycleListener(this);
this.server.addBean(new HashLoginService("OSGi HTTP Service Realm"));
this.parent = new ContextHandlerCollection();
ServletContextHandler context = new ServletContextHandler(this.parent, this.config.getContextPath(), ServletContextHandler.SESSIONS);
configureSessionManager(context);
this.controller.getEventDispatcher().setActive(true);
context.addEventListener(controller.getEventDispatcher());
context.getSessionHandler().addEventListener(controller.getEventDispatcher());
final ServletHolder holder = new ServletHolder(this.controller.createDispatcherServlet());
holder.setAsyncSupported(true);
context.addServlet(holder, "/*");
context.setMaxFormContentSize(this.config.getMaxFormSize());
if (this.config.isRegisterMBeans()) {
this.mbeanServerTracker = new MBeanServerTracker(this.context, this.server);
this.mbeanServerTracker.open();
context.addBean(new StatisticsHandler());
}
this.server.setHandler(this.parent);
this.server.start();
// session id manager is only available after server is started
context.getSessionHandler().getSessionIdManager().getSessionHouseKeeper().setIntervalSec(this.config.getLongProperty(JettyConfig.FELIX_JETTY_SESSION_SCAVENGING_INTERVAL, HouseKeeper.DEFAULT_PERIOD_MS / 1000L));
if (this.config.isProxyLoadBalancerConnection()) {
customizerWrapper = new CustomizerWrapper();
this.loadBalancerCustomizerTracker = new LoadBalancerCustomizerFactoryTracker(this.context, customizerWrapper);
this.loadBalancerCustomizerTracker.open();
}
final StringBuilder message = new StringBuilder("Started Jetty ").append(this.jettyVersion).append(" at port(s)");
if (this.config.isUseHttp() && initializeHttp()) {
message.append(" HTTP:").append(this.config.getHttpPort());
}
if (this.config.isUseHttps() && initializeHttps()) {
message.append(" HTTPS:").append(this.config.getHttpsPort());
}
this.connectorTracker = new ConnectorFactoryTracker(this.context, this.server);
this.connectorTracker.open();
if (this.server.getConnectors() != null && this.server.getConnectors().length > 0) {
message.append(" on context path ").append(this.config.getContextPath());
message.append(" [");
ThreadPool threadPool = this.server.getThreadPool();
if (threadPool instanceof ThreadPool.SizedThreadPool) {
ThreadPool.SizedThreadPool sizedThreadPool = (ThreadPool.SizedThreadPool) threadPool;
message.append("minThreads=").append(sizedThreadPool.getMinThreads()).append(",");
message.append("maxThreads=").append(sizedThreadPool.getMaxThreads()).append(",");
}
Connector connector = this.server.getConnectors()[0];
if (connector instanceof ServerConnector) {
@SuppressWarnings("resource") ServerConnector serverConnector = (ServerConnector) connector;
message.append("acceptors=").append(serverConnector.getAcceptors()).append(",");
message.append("selectors=").append(serverConnector.getSelectorManager().getSelectorCount());
}
message.append("]");
SystemLogger.info(message.toString());
this.controller.register(context.getServletContext(), getServiceProperties());
} else {
this.stopJetty();
SystemLogger.error("Jetty stopped (no connectors available)", null);
}
try {
this.requestLogTracker = new RequestLogTracker(this.context, this.config.getRequestLogFilter());
this.requestLogTracker.open();
this.server.setRequestLog(requestLogTracker);
} catch (InvalidSyntaxException e) {
SystemLogger.error("Invalid filter syntax in request log tracker", e);
}
if (this.config.isRequestLogOSGiEnabled()) {
this.osgiRequestLog = new LogServiceRequestLog(this.config);
this.osgiRequestLog.register(this.context);
SystemLogger.info("Directing Jetty request logs to the OSGi Log Service");
}
if (this.config.getRequestLogFilePath() != null && !this.config.getRequestLogFilePath().isEmpty()) {
this.fileRequestLog = new FileRequestLog(config);
this.fileRequestLog.start(this.context);
SystemLogger.info("Directing Jetty request logs to " + this.config.getRequestLogFilePath());
}
} else {
SystemLogger.warning("Jetty not started (HTTP and HTTPS disabled)", null);
}
}
use of org.eclipse.jetty.server.handler.StatisticsHandler in project javalin by tipsy.
the class TestCustomJetty method test_embeddedServer_withStatisticsHandler.
@Test
public void test_embeddedServer_withStatisticsHandler() throws Exception {
StatisticsHandler statisticsHandler = new StatisticsHandler();
Javalin app = Javalin.create().port(0).embeddedServer(new EmbeddedJettyFactory(() -> {
Server server = new Server();
server.setHandler(statisticsHandler);
return server;
})).get("/", ctx -> ctx.result("Hello World")).start();
String origin = "http://localhost:" + app.port();
int requests = 5;
for (int i = 0; i < requests; i++) {
assertThat(Unirest.get(origin + "/").asString().getBody(), is("Hello World"));
assertThat(Unirest.get(origin + "/not_there").asString().getStatus(), is(404));
}
assertThat(statisticsHandler.getDispatched(), is(requests * 2));
assertThat(statisticsHandler.getResponses2xx(), is(requests));
assertThat(statisticsHandler.getResponses4xx(), is(requests));
app.stop();
}
use of org.eclipse.jetty.server.handler.StatisticsHandler in project javalin by tipsy.
the class TestCustomJetty method test_embeddedServer_withHandlerChain.
@Test
public void test_embeddedServer_withHandlerChain() throws Exception {
AtomicLong logCount = new AtomicLong(0);
RequestLog requestLog = (request, response) -> logCount.incrementAndGet();
RequestLogHandler requestLogHandler = new RequestLogHandler();
requestLogHandler.setRequestLog(requestLog);
StatisticsHandler handlerChain = new StatisticsHandler();
handlerChain.setHandler(requestLogHandler);
Javalin app = Javalin.create().port(0).embeddedServer(new EmbeddedJettyFactory(() -> {
Server server = new Server();
server.setHandler(handlerChain);
return server;
})).get("/", ctx -> ctx.result("Hello World")).start();
String origin = "http://localhost:" + app.port();
int requests = 10;
for (int i = 0; i < requests; i++) {
assertThat(Unirest.get(origin + "/").asString().getBody(), is("Hello World"));
assertThat(Unirest.get(origin + "/not_there").asString().getStatus(), is(404));
}
assertThat(handlerChain.getDispatched(), is(requests * 2));
assertThat(handlerChain.getResponses2xx(), is(requests));
assertThat(handlerChain.getResponses4xx(), is(requests));
assertThat(logCount.get(), is((long) (requests * 2)));
app.stop();
}
use of org.eclipse.jetty.server.handler.StatisticsHandler in project kafka by apache.
the class RestServer method initializeServer.
public void initializeServer() {
log.info("Initializing REST server");
/* Needed for graceful shutdown as per `setStopTimeout` documentation */
StatisticsHandler statsHandler = new StatisticsHandler();
statsHandler.setHandler(handlers);
jettyServer.setHandler(statsHandler);
jettyServer.setStopTimeout(GRACEFUL_SHUTDOWN_TIMEOUT_MS);
jettyServer.setStopAtShutdown(true);
try {
jettyServer.start();
} catch (Exception e) {
throw new ConnectException("Unable to initialize REST server", e);
}
log.info("REST server listening at " + jettyServer.getURI() + ", advertising URL " + advertisedUrl());
log.info("REST admin endpoints at " + adminUrl());
}
Aggregations