Search in sources :

Example 51 with QueuedThreadPool

use of org.eclipse.jetty.util.thread.QueuedThreadPool in project neo4j by neo4j.

the class JettyThreadLimitTest method shouldHaveConfigurableJettyThreadPoolSize.

@Test
public void shouldHaveConfigurableJettyThreadPoolSize() throws Exception {
    Jetty9WebServer server = new Jetty9WebServer(NullLogProvider.getInstance(), Config.empty());
    int numCores = 1;
    // 12 is the new min max Threads value, for one core
    int configuredMaxThreads = 12;
    // In this configuration, 1 thread will become an acceptor...
    int acceptorThreads = 1;
    // ... and 1 thread will become a selector...
    int selectorThreads = 1;
    // ... and the rest are job threads
    int jobThreads = configuredMaxThreads - acceptorThreads - selectorThreads;
    server.setMaxThreads(numCores);
    server.setAddress(new ListenSocketAddress("localhost", 7480));
    try {
        server.start();
        QueuedThreadPool threadPool = (QueuedThreadPool) server.getJetty().getThreadPool();
        threadPool.start();
        CountDownLatch startLatch = new CountDownLatch(jobThreads);
        CountDownLatch endLatch = loadThreadPool(threadPool, configuredMaxThreads + 1, startLatch);
        // Wait for threadPool to create threads
        startLatch.await();
        int threads = threadPool.getThreads();
        assertEquals("Wrong number of threads in pool", configuredMaxThreads, threads);
        endLatch.countDown();
    } finally {
        server.stop();
    }
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 52 with QueuedThreadPool

use of org.eclipse.jetty.util.thread.QueuedThreadPool in project spark by perwendel.

the class JettyServer method create.

/**
     * Creates a Jetty server.
     *
     * @param maxThreads          maxThreads
     * @param minThreads          minThreads
     * @param threadTimeoutMillis threadTimeoutMillis
     * @return a new jetty server instance
     */
public static Server create(int maxThreads, int minThreads, int threadTimeoutMillis) {
    Server server;
    if (maxThreads > 0) {
        int max = (maxThreads > 0) ? maxThreads : 200;
        int min = (minThreads > 0) ? minThreads : 8;
        int idleTimeout = (threadTimeoutMillis > 0) ? threadTimeoutMillis : 60000;
        server = new Server(new QueuedThreadPool(max, min, idleTimeout));
    } else {
        server = new Server();
    }
    return server;
}
Also used : Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool)

Example 53 with QueuedThreadPool

use of org.eclipse.jetty.util.thread.QueuedThreadPool in project spark by perwendel.

the class JettyServerTest method testCreateServer_whenNonDefaultMaxThreadOnly_thenUseDefaultMinThreadAndTimeout.

@Test
public void testCreateServer_whenNonDefaultMaxThreadOnly_thenUseDefaultMinThreadAndTimeout() throws Exception {
    Server server = JettyServer.create(1, 0, 0);
    QueuedThreadPool threadPool = (QueuedThreadPool) server.getThreadPool();
    int minThreads = (int) Whitebox.getInternalState(threadPool, "_minThreads");
    int maxThreads = (int) Whitebox.getInternalState(threadPool, "_maxThreads");
    int idleTimeout = (int) Whitebox.getInternalState(threadPool, "_idleTimeout");
    assertEquals("Server thread pool default minThreads should be 1", 1, minThreads);
    assertEquals("Server thread pool default maxThreads should be the same as specified", 1, maxThreads);
    assertEquals("Server thread pool default idleTimeout should be 60000", 60000, idleTimeout);
}
Also used : Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Test(org.junit.Test)

Example 54 with QueuedThreadPool

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

the class WebsocketEndpointConfigurationTest method testSetServletThreadPool.

@Test
public void testSetServletThreadPool() throws Exception {
    port = AvailablePortFinder.getNextAvailable(16332);
    String uri = "websocket://localhost:" + port + "/bar?bufferSize=25000&maxIdleTime=3000";
    WebsocketEndpoint websocketEndpoint = (WebsocketEndpoint) context.getEndpoint(uri);
    WebsocketComponent component = websocketEndpoint.getComponent();
    QueuedThreadPool qtp = new QueuedThreadPool(20, 1);
    component.setThreadPool(qtp);
    Consumer consumer = websocketEndpoint.createConsumer(processor);
    component.connect((WebsocketProducerConsumer) consumer);
    assertNotNull(consumer);
    assertEquals(WebsocketConsumer.class, consumer.getClass());
    // just check the servlet initial parameters
    ConnectorRef conector = WebsocketComponent.getConnectors().values().iterator().next();
    ServletContextHandler context = (ServletContextHandler) conector.server.getHandler();
    String buffersize = context.getInitParameter("bufferSize");
    assertEquals("Got a wrong buffersize", "25000", buffersize);
    String maxIdleTime = context.getInitParameter("maxIdleTime");
    assertEquals("Got a wrong maxIdleTime", "3000", maxIdleTime);
    WebSocketServletFactory factory = (WebSocketServletFactory) context.getServletContext().getAttribute(WebSocketServletFactory.class.getName());
    int factoryBufferSize = factory.getPolicy().getInputBufferSize();
    assertEquals("Got a wrong buffersize", 25000, factoryBufferSize);
    long factoryMaxIdleTime = factory.getPolicy().getIdleTimeout();
    assertEquals("Got a wrong maxIdleTime", 3000, factoryMaxIdleTime);
}
Also used : WebSocketServletFactory(org.eclipse.jetty.websocket.servlet.WebSocketServletFactory) Consumer(org.apache.camel.Consumer) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ConnectorRef(org.apache.camel.component.websocket.WebsocketComponent.ConnectorRef) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 55 with QueuedThreadPool

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

the class WebsocketComponent method createServer.

protected Server createServer() throws Exception {
    Server server = null;
    if (minThreads == null && maxThreads == null && getThreadPool() == null) {
        minThreads = 1;
        // 1+selectors+acceptors
        maxThreads = 1 + Runtime.getRuntime().availableProcessors() * 2;
    }
    // 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);
        }
        QueuedThreadPool qtp = new QueuedThreadPool();
        if (minThreads != null) {
            qtp.setMinThreads(minThreads.intValue());
        }
        if (maxThreads != null) {
            qtp.setMaxThreads(maxThreads.intValue());
        }
        // let the thread names indicate they are from the server
        qtp.setName("CamelJettyWebSocketServer");
        try {
            qtp.start();
        } catch (Exception e) {
            throw new RuntimeCamelException("Error starting JettyWebSocketServer thread pool: " + qtp, e);
        }
        server = new Server(qtp);
        ContextHandlerCollection collection = new ContextHandlerCollection();
        server.setHandler(collection);
    }
    if (getThreadPool() != null) {
        server = new Server(getThreadPool());
        ContextHandlerCollection collection = new ContextHandlerCollection();
        server.setHandler(collection);
    }
    return server;
}
Also used : Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) RuntimeCamelException(org.apache.camel.RuntimeCamelException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)126 Server (org.eclipse.jetty.server.Server)65 ServerConnector (org.eclipse.jetty.server.ServerConnector)51 Test (org.junit.Test)30 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)26 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)23 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)22 HttpClient (org.eclipse.jetty.client.HttpClient)18 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)18 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)18 IOException (java.io.IOException)17 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)11 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)11 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)10 File (java.io.File)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 InputStream (java.io.InputStream)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)8