Search in sources :

Example 1 with ThreadPool

use of org.eclipse.jetty.util.thread.ThreadPool in project camel by apache.

the class JettyHttpComponent method createServer.

protected Server createServer() {
    Server s = null;
    ThreadPool tp = threadPool;
    QueuedThreadPool qtp = null;
    // configure thread pool if min/max given
    if (minThreads != null || maxThreads != null) {
        if (getThreadPool() != null) {
            throw new IllegalArgumentException("You cannot configure both minThreads/maxThreads and a custom threadPool on JettyHttpComponent: " + this);
        }
        qtp = new QueuedThreadPool();
        if (minThreads != null) {
            qtp.setMinThreads(minThreads.intValue());
        }
        if (maxThreads != null) {
            qtp.setMaxThreads(maxThreads.intValue());
        }
        tp = qtp;
    }
    if (tp != null) {
        try {
            if (!Server.getVersion().startsWith("8")) {
                s = Server.class.getConstructor(ThreadPool.class).newInstance(tp);
            } else {
                s = new Server();
                if (isEnableJmx()) {
                    enableJmx(s);
                }
                Server.class.getMethod("setThreadPool", ThreadPool.class).invoke(s, tp);
            }
        } catch (Exception e) {
        //ignore
        }
    }
    if (s == null) {
        s = new Server();
    }
    if (qtp != null) {
        // let the thread names indicate they are from the server
        qtp.setName("CamelJettyServer(" + ObjectHelper.getIdentityHashCode(s) + ")");
        try {
            qtp.start();
        } catch (Exception e) {
            throw new RuntimeCamelException("Error starting JettyServer thread pool: " + qtp, e);
        }
    }
    ContextHandlerCollection collection = new ContextHandlerCollection();
    s.setHandler(collection);
    // setup the error handler if it set to Jetty component
    if (getErrorHandler() != null) {
        s.addBean(getErrorHandler());
    } else if (!Server.getVersion().startsWith("8")) {
        //need an error handler that won't leak information about the exception 
        //back to the client.
        ErrorHandler eh = new ErrorHandler() {

            public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
                String msg = HttpStatus.getMessage(response.getStatus());
                request.setAttribute(RequestDispatcher.ERROR_MESSAGE, msg);
                if (response instanceof Response) {
                    //need to use the deprecated method to support compiling with Jetty 8
                    ((Response) response).setStatus(response.getStatus(), msg);
                }
                super.handle(target, baseRequest, request, response);
            }

            protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException {
                super.writeErrorPage(request, writer, code, message, false);
            }
        };
        s.addBean(eh, false);
    }
    return s;
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Server(org.eclipse.jetty.server.Server) MBeanServer(javax.management.MBeanServer) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) GeneralSecurityException(java.security.GeneralSecurityException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) Endpoint(org.apache.camel.Endpoint) HttpCommonEndpoint(org.apache.camel.http.common.HttpCommonEndpoint) HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Writer(java.io.Writer)

Example 2 with ThreadPool

use of org.eclipse.jetty.util.thread.ThreadPool in project dropwizard by dropwizard.

the class HttpsConnectorFactoryTest method testBuild.

@Test
public void testBuild() throws Exception {
    final HttpsConnectorFactory https = new HttpsConnectorFactory();
    https.setBindHost("127.0.0.1");
    https.setPort(8443);
    https.setKeyStorePath("/etc/app/server.ks");
    https.setKeyStoreType("JKS");
    https.setKeyStorePassword("correct_horse");
    https.setKeyStoreProvider("BC");
    https.setTrustStorePath("/etc/app/server.ts");
    https.setTrustStoreType("JKS");
    https.setTrustStorePassword("battery_staple");
    https.setTrustStoreProvider("BC");
    https.setKeyManagerPassword("new_overlords");
    https.setNeedClientAuth(true);
    https.setWantClientAuth(true);
    https.setCertAlias("alt_server");
    https.setCrlPath(new File("/etc/ctr_list.txt"));
    https.setEnableCRLDP(true);
    https.setEnableOCSP(true);
    https.setMaxCertPathLength(4);
    https.setOcspResponderUrl(new URI("http://windc1/ocsp"));
    https.setJceProvider("BC");
    https.setAllowRenegotiation(false);
    https.setEndpointIdentificationAlgorithm("HTTPS");
    https.setValidateCerts(true);
    https.setValidatePeers(true);
    https.setSupportedProtocols(ImmutableList.of("TLSv1.1", "TLSv1.2"));
    https.setSupportedCipherSuites(ImmutableList.of("TLS_DHE_RSA.*", "TLS_ECDHE.*"));
    final Server server = new Server();
    final MetricRegistry metrics = new MetricRegistry();
    final ThreadPool threadPool = new QueuedThreadPool();
    final Connector connector = https.build(server, metrics, "test-https-connector", threadPool);
    assertThat(connector).isInstanceOf(ServerConnector.class);
    final ServerConnector serverConnector = (ServerConnector) connector;
    assertThat(serverConnector.getPort()).isEqualTo(8443);
    assertThat(serverConnector.getHost()).isEqualTo("127.0.0.1");
    assertThat(serverConnector.getName()).isEqualTo("test-https-connector");
    assertThat(serverConnector.getServer()).isSameAs(server);
    assertThat(serverConnector.getScheduler()).isInstanceOf(ScheduledExecutorScheduler.class);
    assertThat(serverConnector.getExecutor()).isSameAs(threadPool);
    final Jetty93InstrumentedConnectionFactory jetty93SslConnectionFacttory = (Jetty93InstrumentedConnectionFactory) serverConnector.getConnectionFactory("ssl");
    assertThat(jetty93SslConnectionFacttory).isInstanceOf(Jetty93InstrumentedConnectionFactory.class);
    assertThat(jetty93SslConnectionFacttory.getTimer()).isSameAs(metrics.timer("org.eclipse.jetty.server.HttpConnectionFactory.127.0.0.1.8443.connections"));
    final SslContextFactory sslContextFactory = ((SslConnectionFactory) jetty93SslConnectionFacttory.getConnectionFactory()).getSslContextFactory();
    assertThat(getField(SslContextFactory.class, "_keyStoreResource", true).get(sslContextFactory)).isEqualTo(Resource.newResource("/etc/app/server.ks"));
    assertThat(sslContextFactory.getKeyStoreType()).isEqualTo("JKS");
    assertThat(getField(SslContextFactory.class, "_keyStorePassword", true).get(sslContextFactory).toString()).isEqualTo("correct_horse");
    assertThat(sslContextFactory.getKeyStoreProvider()).isEqualTo("BC");
    assertThat(getField(SslContextFactory.class, "_trustStoreResource", true).get(sslContextFactory)).isEqualTo(Resource.newResource("/etc/app/server.ts"));
    assertThat(sslContextFactory.getKeyStoreType()).isEqualTo("JKS");
    assertThat(getField(SslContextFactory.class, "_trustStorePassword", true).get(sslContextFactory).toString()).isEqualTo("battery_staple");
    assertThat(sslContextFactory.getKeyStoreProvider()).isEqualTo("BC");
    assertThat(getField(SslContextFactory.class, "_keyManagerPassword", true).get(sslContextFactory).toString()).isEqualTo("new_overlords");
    assertThat(sslContextFactory.getNeedClientAuth()).isTrue();
    assertThat(sslContextFactory.getWantClientAuth()).isTrue();
    assertThat(sslContextFactory.getCertAlias()).isEqualTo("alt_server");
    assertThat(sslContextFactory.getCrlPath()).isEqualTo(new File("/etc/ctr_list.txt").getAbsolutePath());
    assertThat(sslContextFactory.isEnableCRLDP()).isTrue();
    assertThat(sslContextFactory.isEnableOCSP()).isTrue();
    assertThat(sslContextFactory.getMaxCertPathLength()).isEqualTo(4);
    assertThat(sslContextFactory.getOcspResponderURL()).isEqualTo("http://windc1/ocsp");
    assertThat(sslContextFactory.getProvider()).isEqualTo("BC");
    assertThat(sslContextFactory.isRenegotiationAllowed()).isFalse();
    assertThat(getField(SslContextFactory.class, "_endpointIdentificationAlgorithm", true).get(sslContextFactory)).isEqualTo("HTTPS");
    assertThat(sslContextFactory.isValidateCerts()).isTrue();
    assertThat(sslContextFactory.isValidatePeerCerts()).isTrue();
    assertThat(sslContextFactory.getIncludeProtocols()).containsOnly("TLSv1.1", "TLSv1.2");
    assertThat(sslContextFactory.getIncludeCipherSuites()).containsOnly("TLS_DHE_RSA.*", "TLS_ECDHE.*");
    final ConnectionFactory httpConnectionFactory = serverConnector.getConnectionFactory("http/1.1");
    assertThat(httpConnectionFactory).isInstanceOf(HttpConnectionFactory.class);
    final HttpConfiguration httpConfiguration = ((HttpConnectionFactory) httpConnectionFactory).getHttpConfiguration();
    assertThat(httpConfiguration.getSecureScheme()).isEqualTo("https");
    assertThat(httpConfiguration.getSecurePort()).isEqualTo(8443);
    assertThat(httpConfiguration.getCustomizers()).hasAtLeastOneElementOfType(SecureRequestCustomizer.class);
    connector.stop();
    server.stop();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) URI(java.net.URI) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ConnectionFactory(org.eclipse.jetty.server.ConnectionFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) File(java.io.File) Test(org.junit.Test)

Example 3 with ThreadPool

use of org.eclipse.jetty.util.thread.ThreadPool in project metrics by dropwizard.

the class ExampleServer method main.

public static void main(String[] args) throws Exception {
    COUNTER_1.inc();
    COUNTER_2.inc();
    final ThreadPool threadPool = new InstrumentedQueuedThreadPool(REGISTRY);
    final Server server = new Server(threadPool);
    final Connector connector = new ServerConnector(server, new InstrumentedConnectionFactory(new HttpConnectionFactory(), REGISTRY.timer("http.connection")));
    server.addConnector(connector);
    final ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/initial");
    context.setAttribute(MetricsServlet.METRICS_REGISTRY, REGISTRY);
    context.setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, new HealthCheckRegistry());
    final ServletHolder holder = new ServletHolder(new AdminServlet());
    context.addServlet(holder, "/dingo/*");
    final InstrumentedHandler handler = new InstrumentedHandler(REGISTRY);
    handler.setHandler(context);
    server.setHandler(handler);
    server.start();
    server.join();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) InstrumentedHandler(com.codahale.metrics.jetty9.InstrumentedHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) InstrumentedQueuedThreadPool(com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool) AdminServlet(com.codahale.metrics.servlets.AdminServlet) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) InstrumentedQueuedThreadPool(com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) InstrumentedConnectionFactory(com.codahale.metrics.jetty9.InstrumentedConnectionFactory)

Example 4 with ThreadPool

use of org.eclipse.jetty.util.thread.ThreadPool in project dropwizard by dropwizard.

the class DefaultServerFactory method build.

@Override
public Server build(Environment environment) {
    // ensures that the environment is configured before the server is built
    configure(environment);
    printBanner(environment.getName());
    final ThreadPool threadPool = createThreadPool(environment.metrics());
    final Server server = buildServer(environment.lifecycle(), threadPool);
    final Handler applicationHandler = createAppServlet(server, environment.jersey(), environment.getObjectMapper(), environment.getValidator(), environment.getApplicationContext(), environment.getJerseyServletContainer(), environment.metrics());
    final Handler adminHandler = createAdminServlet(server, environment.getAdminContext(), environment.metrics(), environment.healthChecks());
    final RoutingHandler routingHandler = buildRoutingHandler(environment.metrics(), server, applicationHandler, adminHandler);
    final Handler gzipHandler = buildGzipHandler(routingHandler);
    server.setHandler(addStatsHandler(addRequestLog(server, gzipHandler, environment.getName())));
    return server;
}
Also used : Server(org.eclipse.jetty.server.Server) RoutingHandler(io.dropwizard.jetty.RoutingHandler) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) RoutingHandler(io.dropwizard.jetty.RoutingHandler) Handler(org.eclipse.jetty.server.Handler)

Example 5 with ThreadPool

use of org.eclipse.jetty.util.thread.ThreadPool in project jetty.project by eclipse.

the class JettyHttpServerProvider method createHttpServer.

@Override
public HttpServer createHttpServer(InetSocketAddress addr, int backlog) throws IOException {
    Server server = _server;
    boolean shared = true;
    if (server == null) {
        ThreadPool threadPool = new DelegatingThreadPool(new QueuedThreadPool());
        server = new Server(threadPool);
        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { new ContextHandlerCollection(), new DefaultHandler() });
        server.setHandler(handlerCollection);
        shared = false;
    }
    JettyHttpServer jettyHttpServer = new JettyHttpServer(server, shared);
    jettyHttpServer.bind(addr, backlog);
    return jettyHttpServer;
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) HttpsServer(com.sun.net.httpserver.HttpsServer) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Aggregations

ThreadPool (org.eclipse.jetty.util.thread.ThreadPool)15 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)9 Server (org.eclipse.jetty.server.Server)8 Test (org.junit.Test)6 ServerConnector (org.eclipse.jetty.server.ServerConnector)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 Connector (org.eclipse.jetty.server.Connector)3 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)3 Handler (org.eclipse.jetty.server.Handler)2 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)2 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)2 HealthCheckRegistry (com.codahale.metrics.health.HealthCheckRegistry)1 InstrumentedConnectionFactory (com.codahale.metrics.jetty9.InstrumentedConnectionFactory)1 InstrumentedHandler (com.codahale.metrics.jetty9.InstrumentedHandler)1 InstrumentedQueuedThreadPool (com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool)1 AdminServlet (com.codahale.metrics.servlets.AdminServlet)1 HttpServer (com.sun.net.httpserver.HttpServer)1 HttpsServer (com.sun.net.httpserver.HttpsServer)1 ContextRoutingHandler (io.dropwizard.jetty.ContextRoutingHandler)1 RoutingHandler (io.dropwizard.jetty.RoutingHandler)1