@Override public void afterPropertiesSet() throws Exception { String name = this.threadPrefix + "@" + Integer.toHexString(hashCode()); if (this.executor == null) { QueuedThreadPool threadPool = new QueuedThreadPool(); threadPool.setName(name); this.executor = threadPool; } if (this.byteBufferPool == null) { this.byteBufferPool = new MappedByteBufferPool(2048, this.executor instanceof ThreadPool.SizedThreadPool ? ((ThreadPool.SizedThreadPool) executor).getMaxThreads() / 2 : ProcessorUtils.availableProcessors() * 2); } if (this.scheduler == null) { this.scheduler = new ScheduledExecutorScheduler(name + "-scheduler", false); } if (this.executor instanceof LifeCycle) { ((LifeCycle)this.executor).start(); } this.scheduler.start(); }
/** * Creates a Jetty server. * * @param maxThreads maxThreads * @param minThreads minThreads * @param threadTimeoutMillis threadTimeoutMillis * @return a new jetty server instance */ public Server create(int maxThreads, int minThreads, int threadTimeoutMillis) { Server server; if (maxThreads > 0) { int max = maxThreads; 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; }
/** * Set the min, max number of worker threads (simultaneous connections). */ public void setThreads(int min, int max) { QueuedThreadPool pool = (QueuedThreadPool) webServer.getThreadPool(); pool.setMinThreads(min); pool.setMaxThreads(max); }
private static QueuedThreadPool createQueuedThreadPool( JettyThreadCalculator jtc ) { BlockingQueue<Runnable> queue = new BlockingArrayQueue<>( jtc.getMinThreads(), jtc.getMinThreads(), jtc.getMaxCapacity() ); QueuedThreadPool threadPool = new QueuedThreadPool( jtc.getMaxThreads(), jtc.getMinThreads(), JETTY_THREAD_POOL_IDLE_TIMEOUT, queue ); threadPool.setThreadPoolBudget( null ); // mute warnings about Jetty thread pool size return threadPool; }
@Signature public void maxThreads(int maxThreads) { threadPool.setMaxThreads(maxThreads); }
@Signature public void minThreads(int minThreads) { threadPool.setMinThreads(minThreads); }
@Override public Connector build(Server server, MetricRegistry metrics, String name, @Nullable ThreadPool threadPool) { final HttpConfiguration httpConfig = buildHttpConfiguration(); final HttpConnectionFactory httpConnectionFactory = buildHttpConnectionFactory(httpConfig); final Scheduler scheduler = new ScheduledExecutorScheduler(); final ByteBufferPool bufferPool = buildBufferPool(); return buildConnector(server, scheduler, bufferPool, name, threadPool, new Jetty93InstrumentedConnectionFactory(httpConnectionFactory, metrics.timer(httpConnections()))); }
@Signature public int maxThreads() { return threadPool.getMaxThreads(); }
@Signature public int minThreads() { return threadPool.getMinThreads(); }
@Signature public void threadIdleTimeout(int timeout) { threadPool.setIdleTimeout(timeout); }
@Override public int activeThreadCount() { if (server == null) { return 0; } return server.getThreadPool().getThreads() - server.getThreadPool().getIdleThreads(); }
@Override public void destroy() throws Exception { try { if (this.executor instanceof LifeCycle) { ((LifeCycle)this.executor).stop(); } } catch (Throwable ex) { // ignore } try { if (this.scheduler != null) { this.scheduler.stop(); } } catch (Throwable ex) { // ignore } }
private static CountDownLatch loadThreadPool( QueuedThreadPool threadPool, int tasksToSubmit, final CountDownLatch startLatch ) { CountDownLatch endLatch = new CountDownLatch( 1 ); for ( int i = 0; i < tasksToSubmit; i++ ) { threadPool.execute( () -> { startLatch.countDown(); try { endLatch.await(); } catch ( InterruptedException e ) { e.printStackTrace(); } } ); } return endLatch; } }
@Signature public int threadIdleTimeout() { return threadPool.getIdleTimeout(); }
@Override public void afterPropertiesSet() throws Exception { String name = this.threadPrefix + "@" + Integer.toHexString(hashCode()); if (this.executor == null) { QueuedThreadPool threadPool = new QueuedThreadPool(); threadPool.setName(name); this.executor = threadPool; } if (this.byteBufferPool == null) { this.byteBufferPool = new MappedByteBufferPool(2048, this.executor instanceof ThreadPool.SizedThreadPool ? ((ThreadPool.SizedThreadPool) executor).getMaxThreads() / 2 : ProcessorUtils.availableProcessors() * 2); } if (this.scheduler == null) { this.scheduler = new ScheduledExecutorScheduler(name + "-scheduler", false); } if (this.executor instanceof LifeCycle) { ((LifeCycle)this.executor).start(); } this.scheduler.start(); }
QueuedThreadPool threadPool = new QueuedThreadPool(100, 10); Server server = new Server(threadPool);
/** * Set the min, max number of worker threads (simultaneous connections). */ public void setThreads(int min, int max) { QueuedThreadPool pool = (QueuedThreadPool) webServer.getThreadPool(); pool.setMinThreads(min); pool.setMaxThreads(max); }
@Test public void testNumThreads() { // Just make sure the injector stuff for this test is actually working. Assert.assertEquals( 10, ((QueuedThreadPool) server.getThreadPool()).getMaxThreads() ); }
public static int getServerThreads() { return server.getThreadPool().getThreads() - server.getThreadPool().getIdleThreads(); }
@Override public void destroy() throws Exception { try { if (this.executor instanceof LifeCycle) { ((LifeCycle)this.executor).stop(); } } catch (Throwable ex) { // ignore } try { if (this.scheduler != null) { this.scheduler.stop(); } } catch (Throwable ex) { // ignore } }