Search in sources :

Example 16 with StatisticsHandler

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);
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) Test(org.junit.Test)

Example 17 with StatisticsHandler

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);
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServerConnector(org.eclipse.jetty.server.ServerConnector) HashLoginService(org.eclipse.jetty.security.HashLoginService) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler)

Example 18 with StatisticsHandler

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();
}
Also used : EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) CoreMatchers.is(org.hamcrest.CoreMatchers.is) AtomicLong(java.util.concurrent.atomic.AtomicLong) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) Unirest(com.mashape.unirest.http.Unirest) RequestLog(org.eclipse.jetty.server.RequestLog) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) Test(org.junit.Test) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Server(org.eclipse.jetty.server.Server) Server(org.eclipse.jetty.server.Server) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) Test(org.junit.Test)

Example 19 with StatisticsHandler

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();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) AtomicLong(java.util.concurrent.atomic.AtomicLong) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) Unirest(com.mashape.unirest.http.Unirest) RequestLog(org.eclipse.jetty.server.RequestLog) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) Test(org.junit.Test) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Server(org.eclipse.jetty.server.Server) EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) RequestLog(org.eclipse.jetty.server.RequestLog) Server(org.eclipse.jetty.server.Server) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) Test(org.junit.Test)

Example 20 with StatisticsHandler

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());
}
Also used : StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) IOException(java.io.IOException) ConfigException(org.apache.kafka.common.config.ConfigException) ConnectException(org.apache.kafka.connect.errors.ConnectException) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Aggregations

StatisticsHandler (org.eclipse.jetty.server.handler.StatisticsHandler)24 Server (org.eclipse.jetty.server.Server)10 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)10 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)9 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)8 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)8 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)7 IOException (java.io.IOException)5 ServerConnector (org.eclipse.jetty.server.ServerConnector)5 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)4 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)4 Test (org.junit.Test)4 Socket (java.net.Socket)3 ConnectException (org.apache.kafka.connect.errors.ConnectException)3 Handler (org.eclipse.jetty.server.Handler)3 Slf4jRequestLog (org.eclipse.jetty.server.Slf4jRequestLog)3 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)3 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)3 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)3 Unirest (com.mashape.unirest.http.Unirest)2