org.eclipse.jetty.util.thread.ThreadPool Java Examples

The following examples show how to use org.eclipse.jetty.util.thread.ThreadPool. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JettyResourceFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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();
}
 
Example #2
Source File: ArmeriaServerFactory.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
public Server build(Environment environment) {
    Objects.requireNonNull(environment, "environment");
    printBanner(environment.getName());
    final MetricRegistry metrics = environment.metrics();
    final ThreadPool threadPool = createThreadPool(metrics);
    final Server server = buildServer(environment.lifecycle(), threadPool);

    final JerseyEnvironment jersey = environment.jersey();
    if (!isJerseyEnabled()) {
        jersey.disable();
    }

    addDefaultHandlers(server, environment, metrics);
    serverBuilder = buildServerBuilder(server, metrics);
    return server;
}
 
Example #3
Source File: ApplicationServer.java    From rest-utils with Apache License 2.0 6 votes vote down vote up
public ApplicationServer(T config, ThreadPool threadPool) {
  super(threadPool);

  this.config = config;
  this.applications = new ApplicationGroup(this);

  int gracefulShutdownMs = config.getInt(RestConfig.SHUTDOWN_GRACEFUL_MS_CONFIG);
  if (gracefulShutdownMs > 0) {
    super.setStopTimeout(gracefulShutdownMs);
  }
  super.setStopAtShutdown(true);

  MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
  super.addEventListener(mbContainer);
  super.addBean(mbContainer);

  this.sslContextFactory = createSslContextFactory(config);
  configureConnectors(sslContextFactory);
}
 
Example #4
Source File: ApplicationServer.java    From rest-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Create the thread pool with request queue.
 *
 * @return thread pool used by the server
 */
private static ThreadPool createThreadPool(RestConfig config) {
  /* Create blocking queue for the thread pool. */
  int initialCapacity = config.getInt(RestConfig.REQUEST_QUEUE_CAPACITY_INITIAL_CONFIG);
  int growBy = config.getInt(RestConfig.REQUEST_QUEUE_CAPACITY_GROWBY_CONFIG);
  int maxCapacity = config.getInt(RestConfig.REQUEST_QUEUE_CAPACITY_CONFIG);
  log.info("Initial capacity {}, increased by {}, maximum capacity {}.",
          initialCapacity, growBy, maxCapacity);

  BlockingQueue<Runnable> requestQueue =
          new BlockingArrayQueue<>(initialCapacity, growBy, maxCapacity);
  
  return new QueuedThreadPool(config.getInt(RestConfig.THREAD_POOL_MAX_CONFIG),
          config.getInt(RestConfig.THREAD_POOL_MIN_CONFIG),
          requestQueue);
}
 
Example #5
Source File: TestWebServerTask.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testThreadPool() throws Exception {
  Configuration serverConf = new Configuration();
  serverConf.set(WebServerTask.HTTP_MAX_THREADS, 100);

  WebServerTask webServerTask = createWebServerTask(new File("target").getAbsolutePath(),
      serverConf,
      Collections.<WebAppProvider>emptySet(),
      true
  );
  try {
    webServerTask.initTask();
    Server server = webServerTask.getServer();
    assertEquals(100, ((ThreadPool.SizedThreadPool)server.getThreadPool()).getMaxThreads());
  } finally {
    webServerTask.stopTask();
  }

}
 
Example #6
Source File: WebSocketProvider.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
public ConnectionWrapper(final Session connection,
                         final SocketAddress localAddress,
                         final SocketAddress remoteAddress,
                         final MultiVersionProtocolEngine protocolEngine,
                         final ThreadPool threadPool)
{
    _connection = connection;
    _localAddress = localAddress;
    _remoteAddress = remoteAddress;
    _protocolEngine = protocolEngine;
    _threadPool = threadPool;
    _tickJob = new Runnable()
                {
                    @Override
                    public void run()
                    {
                        synchronized (ConnectionWrapper.this)
                        {
                            protocolEngine.getAggregateTicker().tick(System.currentTimeMillis());
                            doWrite();
                        }
                    }
                };
}
 
Example #7
Source File: JettyResourceFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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();
}
 
Example #8
Source File: AthenzJettyContainerTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@Test
public void testContainerThreadPool() {
    
    System.setProperty(AthenzConsts.ATHENZ_PROP_MAX_THREADS, "100");
    
    AthenzJettyContainer container = new AthenzJettyContainer();
    container.createServer(100);
    
    Server server = container.getServer();
    assertNotNull(server);
    
    ThreadPool threadPool = server.getThreadPool();
    assertNotNull(threadPool);
    
    // at this point we have no threads so the value is 0
    assertEquals(threadPool.getThreads(), 0);
    assertEquals(threadPool.getIdleThreads(), 0);
}
 
Example #9
Source File: InstrumentedHttpConnectorFactory.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
protected ServerConnector buildConnector(Server server,
                                         Scheduler scheduler,
                                         ByteBufferPool bufferPool,
                                         String name,
                                         ThreadPool threadPool,
                                         ConnectionFactory... factories) {
    // Intercept any buildConnector() calls and wrap the provided ConnectionFactory instances with our InstrumentedConnectionFactoryWrapper
    InstrumentedConnectionFactoryWrapper connectionFactoryWrappers[] = new InstrumentedConnectionFactoryWrapper[factories.length];
    for (int i = 0; i < factories.length; ++i) {
        connectionFactoryWrappers[i] = new InstrumentedConnectionFactoryWrapper(factories[i], _metricRegistry.get(), getBindHost(), getPort());
    }

    return super.buildConnector(server, scheduler, bufferPool, name, threadPool, connectionFactoryWrappers);
}
 
Example #10
Source File: ZeppelinServer.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private static Server setupJettyServer(ZeppelinConfiguration conf) {
  ThreadPool threadPool =
    new QueuedThreadPool(conf.getInt(ConfVars.ZEPPELIN_SERVER_JETTY_THREAD_POOL_MAX),
                         conf.getInt(ConfVars.ZEPPELIN_SERVER_JETTY_THREAD_POOL_MIN),
                         conf.getInt(ConfVars.ZEPPELIN_SERVER_JETTY_THREAD_POOL_TIMEOUT));
  final Server server = new Server(threadPool);
  initServerConnector(server, conf.getServerPort(), conf.getServerSslPort());
  return server;
}
 
Example #11
Source File: InstrumentedHttpConnectorFactory.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
public Connector build(Server server,
                       MetricRegistry metrics,
                       String name,
                       ThreadPool threadPool) {
    _metricRegistry.set(metrics);
    return super.build(server, metrics, name, threadPool);
}
 
Example #12
Source File: JettyServerThreadPoolMetrics.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Override
public void bindTo(MeterRegistry registry) {
    if (threadPool instanceof SizedThreadPool) {
        SizedThreadPool sizedThreadPool = (SizedThreadPool) threadPool;
        Gauge.builder("jetty.threads.config.min", sizedThreadPool, SizedThreadPool::getMinThreads)
                .description("The minimum number of threads in the pool")
                .tags(tags).register(registry);
        Gauge.builder("jetty.threads.config.max", sizedThreadPool, SizedThreadPool::getMaxThreads)
                .description("The maximum number of threads in the pool")
                .tags(tags).register(registry);
        if (threadPool instanceof QueuedThreadPool) {
            QueuedThreadPool queuedThreadPool = (QueuedThreadPool) threadPool;
            Gauge.builder("jetty.threads.busy", queuedThreadPool, QueuedThreadPool::getBusyThreads)
                    .description("The number of busy threads in the pool")
                    .tags(tags).register(registry);
            Gauge.builder("jetty.threads.jobs", queuedThreadPool, QueuedThreadPool::getQueueSize)
                    .description("Number of jobs queued waiting for a thread")
                    .tags(tags).register(registry);
        }
    }
    Gauge.builder("jetty.threads.current", threadPool, ThreadPool::getThreads)
            .description("The total number of threads in the pool")
            .tags(tags).register(registry);
    Gauge.builder("jetty.threads.idle", threadPool, ThreadPool::getIdleThreads)
            .description("The number of idle threads in the pool").tags(tags)
            .register(registry);
}
 
Example #13
Source File: JettyManager.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private ThreadPool configureThreadPool(final int port) {
	final QueuedThreadPool threadPool = new QueuedThreadPool(threadPoolCapacity);
	threadPool.setMinThreads(minThreadCount);
	threadPool.setMaxThreads(maxThreadCount);
	threadPool.setName("Jetty thread pool [" + port + "]");
	threadPool.setDetailedDump(true);
	return threadPool;
}
 
Example #14
Source File: JettyServer.java    From freeacs with MIT License 5 votes vote down vote up
/**
 * Creates a Jetty server with supplied thread pool.
 *
 * @param threadPool thread pool
 * @return a new jetty server instance
 */
@Override
public Server create(ThreadPool threadPool) {
  Server server = threadPool != null ? new Server(threadPool) : new Server();
  server.setAttribute("org.eclipse.jetty.server.Request.maxFormContentSize", maxPostSize);
  server.setAttribute("org.eclipse.jetty.server.Request.maxFormKeys", maxFormKeys);
  return server;
}
 
Example #15
Source File: JettyHTTPServerEngine.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void setupThreadPool() {
    if (isSetThreadingParameters()) {

        ThreadPool pl = getThreadPool();
        //threads for the acceptors and selectors are taken from
        //the pool so we need to have room for those
        AbstractConnector aconn = (AbstractConnector) connector;
        int acc = aconn.getAcceptors() * 2;
        if (getThreadingParameters().isSetMaxThreads()
            && getThreadingParameters().getMaxThreads() <= acc) {
            throw new Fault(new Message("NOT_ENOUGH_THREADS", LOG,
                                        port,
                                        acc + 1,
                                        getThreadingParameters().getMaxThreads(),
                                        acc));
        }
        if (!(pl instanceof QueuedThreadPool)) {
            throw new Fault(new Message("NOT_A_QUEUED_THREAD_POOL", LOG, pl.getClass()));
        }
        if (getThreadingParameters().isThreadNamePrefixSet()) {
            ((QueuedThreadPool) pl).setName(getThreadingParameters().getThreadNamePrefix());
        }
        if (getThreadingParameters().isSetMinThreads()) {
            ((QueuedThreadPool) pl).setMinThreads(getThreadingParameters().getMinThreads());
        }
        if (getThreadingParameters().isSetMaxThreads()) {
            ((QueuedThreadPool) pl).setMaxThreads(getThreadingParameters().getMaxThreads());
        }
    }
}
 
Example #16
Source File: StreamingReceiver.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
private ThreadPool createThreadPool(KylinConfig kylinConfig) {
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setMinThreads(kylinConfig.getStreamingReceiverHttpMinThreads());
    threadPool.setMaxThreads(kylinConfig.getStreamingReceiverHttpMaxThreads());
    return threadPool;
}
 
Example #17
Source File: LimitedMethodServer.java    From datacollector with Apache License 2.0 4 votes vote down vote up
public LimitedMethodServer(ThreadPool threadPool) {
  super(threadPool);
}
 
Example #18
Source File: StreamingReceiver.java    From kylin with Apache License 2.0 4 votes vote down vote up
private ThreadPool createThreadPool(KylinConfig kylinConfig) {
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setMinThreads(kylinConfig.getStreamingReceiverHttpMinThreads());
    threadPool.setMaxThreads(kylinConfig.getStreamingReceiverHttpMaxThreads());
    return threadPool;
}
 
Example #19
Source File: JettyServiceTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
void configurator() throws Exception {
    assertThat(jettyBeans)
              .hasAtLeastOneElementOfType(ThreadPool.class)
              .hasAtLeastOneElementOfType(WebAppContext.class);
}
 
Example #20
Source File: JettyHTTPServerEngine.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void setThreadPool(ThreadPool p) {
    threadPool = p;
}
 
Example #21
Source File: JettyHTTPServerEngineTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * Check that names of threads serving requests for instances of JettyHTTPServerEngine
 * can be set with user specified name.
 */
@Test
public void testSettingThreadNames() throws Exception {
    // User specific thread name prefix 1
    String threadNamePrefix1 = "TestPrefix";
    JettyHTTPServerEngine engine = factory.createJettyHTTPServerEngine(PORT1, "http");
    ThreadingParameters parameters = new ThreadingParameters();
    parameters.setThreadNamePrefix(threadNamePrefix1);
    engine.setThreadingParameters(parameters);
    engine.finalizeConfig();
    JettyHTTPTestHandler handler = new JettyHTTPTestHandler("string1", true);
    engine.addServant(new URL("https://localhost:" + PORT1 + "/test"), handler);
    assertTrue("No threads whose name is started with " + threadNamePrefix1,
            checkForExistenceOfThreads(threadNamePrefix1));

    // Default thread name prefix
    engine = factory.createJettyHTTPServerEngine(PORT3, "http");
    engine.finalizeConfig();
    handler = new JettyHTTPTestHandler("string3", true);
    engine.addServant(new URL("https://localhost:" + PORT3 + "/test"), handler);
    ThreadPool threadPool = engine.getServer().getThreadPool();
    QueuedThreadPool qtp = (QueuedThreadPool)threadPool;
    String prefixDefault = qtp.getName();
    assertTrue("No threads whose name is started with " + prefixDefault,
            checkForExistenceOfThreads(prefixDefault));

    // User specific thread name prefix 2
    String threadNamePrefix2 = "AnotherPrefix";
    engine = factory.createJettyHTTPServerEngine(PORT2, "http");
    parameters = new ThreadingParameters();
    parameters.setThreadNamePrefix(threadNamePrefix2);
    engine.setThreadingParameters(parameters);
    engine.finalizeConfig();
    handler = new JettyHTTPTestHandler("string2", true);
    engine.addServant(new URL("https://localhost:" + PORT2 + "/test"), handler);
    assertTrue("No threads whose name is started with " + threadNamePrefix2,
            checkForExistenceOfThreads(threadNamePrefix2));

    JettyHTTPServerEngineFactory.destroyForPort(PORT1);
    JettyHTTPServerEngineFactory.destroyForPort(PORT2);
    JettyHTTPServerEngineFactory.destroyForPort(PORT3);
}
 
Example #22
Source File: ServerRuntime.java    From EDDI with Apache License 2.0 4 votes vote down vote up
private ThreadPool createThreadPool() {
    return new ExecutorThreadPool(threadPoolExecutor);
}
 
Example #23
Source File: JettyAdapter.java    From enkan with Eclipse Public License 1.0 4 votes vote down vote up
private ThreadPool createThreadPool() {
    QueuedThreadPool pool = new QueuedThreadPool(50);
    pool.setMinThreads(8);
    return pool;
}
 
Example #24
Source File: JettyServerThreadPoolMetrics.java    From micrometer with Apache License 2.0 4 votes vote down vote up
public JettyServerThreadPoolMetrics(ThreadPool threadPool, Iterable<Tag> tags) {
    this.threadPool = threadPool;
    this.tags = tags;
}
 
Example #25
Source File: JettyFactory.java    From kumuluzee with MIT License 3 votes vote down vote up
private ThreadPool createThreadPool() {

        QueuedThreadPool threadPool = new QueuedThreadPool();

        threadPool.setMinThreads(serverConfig.getMinThreads());
        threadPool.setMaxThreads(serverConfig.getMaxThreads());

        log.info("Starting KumuluzEE on Jetty with " + serverConfig.getMinThreads() + " minimum " +
                "and " + serverConfig.getMaxThreads() + " maximum threads");

        return threadPool;
    }
 
Example #26
Source File: WebSocketClientFactory.java    From WebSocket-for-Android with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Creates a WebSocketClientFactory with the given ThreadPool and the default configuration.</p>
 *
 * @param threadPool the ThreadPool instance to use
 */
public WebSocketClientFactory(ThreadPool threadPool)
{
    this(threadPool, new RandomMaskGen());
}
 
Example #27
Source File: WebSocketClientFactory.java    From WebSocket-for-Android with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Creates a WebSocketClientFactory with the given ThreadPool and the given MaskGen.</p>
 *
 * @param threadPool the ThreadPool instance to use
 * @param maskGen    the MaskGen instance to use
 */
public WebSocketClientFactory(ThreadPool threadPool, MaskGen maskGen)
{
    this(threadPool, maskGen, 8192);
}
 
Example #28
Source File: WebSocketClientFactory.java    From WebSocket-for-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Get the ThreadPool.
 * Used to set/query the thread pool configuration.
 *
 * @return The {@link ThreadPool}
 */
public ThreadPool getThreadPool()
{
    return _threadPool;
}
 
Example #29
Source File: WebSocketClientFactory.java    From IoTgo_Android_App with MIT License 2 votes vote down vote up
/**
 * Get the ThreadPool.
 * Used to set/query the thread pool configuration.
 *
 * @return The {@link ThreadPool}
 */
public ThreadPool getThreadPool()
{
    return _threadPool;
}
 
Example #30
Source File: WebSocketClientFactory.java    From IoTgo_Android_App with MIT License 2 votes vote down vote up
/**
 * <p>Creates a WebSocketClientFactory with the given ThreadPool and the given MaskGen.</p>
 *
 * @param threadPool the ThreadPool instance to use
 * @param maskGen    the MaskGen instance to use
 */
public WebSocketClientFactory(ThreadPool threadPool, MaskGen maskGen)
{
    this(threadPool, maskGen, 8192);
}