Java Code Examples for java.util.concurrent.ThreadPoolExecutor#setRejectedExecutionHandler()

The following examples show how to use java.util.concurrent.ThreadPoolExecutor#setRejectedExecutionHandler() . 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: Pool.java    From tomee with Apache License 2.0 6 votes vote down vote up
private Executor createExecutor() {
    final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 10,
            60L, SECONDS,
            new LinkedBlockingQueue<>(2), new DaemonThreadFactory("org.apache.openejb.util.Pool", hashCode()));

    threadPoolExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(final Runnable r, final ThreadPoolExecutor tpe) {

            if (null == r || null == tpe || tpe.isShutdown() || tpe.isTerminated() || tpe.isTerminating()) {
                return;
            }

            try {
                if (!tpe.getQueue().offer(r, 20, SECONDS)) {
                    org.apache.openejb.util.Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources")
                            .warning("Default pool executor failed to run asynchronous process: " + r);
                }
            } catch (final InterruptedException e) {
                //Ignore
            }
        }
    });

    return threadPoolExecutor;
}
 
Example 2
Source File: DefaultZKClientService.java    From twill with Apache License 2.0 6 votes vote down vote up
@Override
protected void doStart() {
  // A single thread executor for all events
  ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
                                                       new LinkedBlockingQueue<Runnable>(),
                                                       Threads.createDaemonThreadFactory("zk-client-EventThread"));
  // Just discard the execution if the executor is closed
  executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
  eventExecutor = executor;

  try {
    zooKeeper.set(createZooKeeper());
  } catch (IOException e) {
    notifyFailed(e);
  }
}
 
Example 3
Source File: ManagedAsyncJobExecutor.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
protected void initAsyncJobExecutionThreadPool() {
    if (threadFactory == null) {
        LOGGER.warn("A managed thread factory was not found, falling back to self-managed threads");
        super.initAsyncJobExecutionThreadPool();
    } else {
        if (threadPoolQueue == null) {
            LOGGER.info("Creating thread pool queue of size {}", queueSize);
            threadPoolQueue = new ArrayBlockingQueue<>(queueSize);
        }

        if (executorService == null) {
            LOGGER.info("Creating executor service with corePoolSize {}, maxPoolSize {} and keepAliveTime {}", corePoolSize, maxPoolSize, keepAliveTime);

            ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, threadPoolQueue, threadFactory);
            threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            executorService = threadPoolExecutor;

        }

        startJobAcquisitionThread();
    }
}
 
Example 4
Source File: ManagedAsyncJobExecutor.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
protected void initAsyncJobExecutionThreadPool() {
  if (threadFactory == null) {
    log.warn("A managed thread factory was not found, falling back to self-managed threads");
    super.initAsyncJobExecutionThreadPool();
  } else {
    if (threadPoolQueue == null) {
      log.info("Creating thread pool queue of size {}", queueSize);
      threadPoolQueue = new ArrayBlockingQueue<Runnable>(queueSize);
    }

    if (executorService == null) {
      log.info("Creating executor service with corePoolSize {}, maxPoolSize {} and keepAliveTime {}", corePoolSize, maxPoolSize, keepAliveTime);

      ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, threadPoolQueue, threadFactory);
      threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
      executorService = threadPoolExecutor;

    }

    startJobAcquisitionThread();
  }
}
 
Example 5
Source File: TripleServer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
protected ThreadPoolExecutor initThreadPool(ServerConfig serverConfig) {
    ThreadPoolExecutor threadPool = BusinessPool.initPool(serverConfig);
    threadPool.setThreadFactory(new NamedThreadFactory(
        "SEV-TRIPLE-BIZ-" + serverConfig.getPort(), serverConfig.isDaemon()));
    threadPool.setRejectedExecutionHandler(new SofaRejectedExecutionHandler());
    if (serverConfig.isPreStartCore()) { // 初始化核心线程池
        threadPool.prestartAllCoreThreads();
    }
    return threadPool;
}
 
Example 6
Source File: GraphiteHandler.java    From newts with Apache License 2.0 5 votes vote down vote up
public GraphiteHandler(SampleRepository repository, GraphiteInitializer parent) {
    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    int concurrency = Runtime.getRuntime().availableProcessors();
    m_executor = new ThreadPoolExecutor(concurrency, concurrency, 0L, MILLISECONDS, queue);
    m_executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    m_repository = repository;
    m_parent = parent;
    m_lines = Lists.newArrayList();
    LOG.debug("Using storage concurrency of {}", concurrency);
}
 
Example 7
Source File: CountedRejectedExecutionHandlerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void countRejections() {
  // given
  ThreadPoolExecutor executor =
      new ThreadPoolExecutor(0, 1, 60L, TimeUnit.SECONDS, new SynchronousQueue<>());
  executor.setRejectedExecutionHandler((r, executor1) -> {});

  CountedRejectedExecutionHandler.monitorRejections(
      registry, executor, CountedRejectedExecutionHandler.class.getName(), userTags);
  CountDownLatch runnableTaskComplete = new CountDownLatch(1);
  Runnable stub =
      () -> {
        try {
          runnableTaskComplete.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
          throw new IllegalStateException("runnable interrupted before completion");
        }
      };
  executor.submit(stub);

  // then
  for (int i = 0; i < 14; i++) {
    executor.submit(
        () -> {
          // do nothing. Task has to be rejected.
        });
  }
  // when
  assertEquals(
      registry
          .get("executor.rejected")
          .tags(userTags)
          .tag("name", CountedRejectedExecutionHandler.class.getName())
          .counter()
          .count(),
      14.0);
  // cleanup
  runnableTaskComplete.countDown();
  executor.shutdownNow();
}
 
Example 8
Source File: AredisAsyncService.java    From uavstack with Apache License 2.0 5 votes vote down vote up
public AredisAsyncService(String redisServerAddress, int minConcurrent, int maxConcurrent, int queueSize,
        String password) {

    if (maxConcurrent <= 0) {
        maxConcurrent = 50;
    }

    if (minConcurrent <= 0) {
        minConcurrent = 10;
    }

    if (queueSize <= 0) {
        queueSize = 100;
    }

    this.redisServerAddress = redisServerAddress;

    if (password != null) {
        AsyncRedisFactory.setAuth(redisServerAddress, password);
    }

    /**
     * 初始化线程池
     */
    executor = new ThreadPoolExecutor(minConcurrent, maxConcurrent, 15, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(queueSize));
    executor.allowCoreThreadTimeOut(true);
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

    if (logger.isTraceEnable()) {
        logger.info(this,
                "AredisAsyncService线程池设置:min=" + minConcurrent + ",max=" + maxConcurrent + ",queue=" + queueSize);
    }

    factory = new AsyncRedisFactory(executor);
}
 
Example 9
Source File: ReadaheadPool.java    From big-c with Apache License 2.0 5 votes vote down vote up
private ReadaheadPool() {
  pool = new ThreadPoolExecutor(POOL_SIZE, MAX_POOL_SIZE, 3L, TimeUnit.SECONDS,
      new ArrayBlockingQueue<Runnable>(CAPACITY));
  pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
  pool.setThreadFactory(new ThreadFactoryBuilder()
    .setDaemon(true)
    .setNameFormat("Readahead Thread #%d")
    .build());
}
 
Example 10
Source File: ReadaheadPool.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private ReadaheadPool() {
  pool = new ThreadPoolExecutor(POOL_SIZE, MAX_POOL_SIZE, 3L, TimeUnit.SECONDS,
      new ArrayBlockingQueue<Runnable>(CAPACITY));
  pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
  pool.setThreadFactory(new ThreadFactoryBuilder()
    .setDaemon(true)
    .setNameFormat("Readahead Thread #%d")
    .build());
}
 
Example 11
Source File: ThreadPoolExecutorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * setRejectedExecutionHandler(null) throws NPE
 */
public void testSetRejectedExecutionHandlerNull() {
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(1, 2,
                               LONG_DELAY_MS, MILLISECONDS,
                               new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        try {
            p.setRejectedExecutionHandler(null);
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
Example 12
Source File: ObsService.java    From huaweicloud-sdk-java-obs with Apache License 2.0 5 votes vote down vote up
protected ThreadPoolExecutor initThreadPool(AbstractBulkRequest request) {
    int taskThreadNum = request.getTaskThreadNum();
    int workQueenLength = request.getTaskQueueNum();
    ThreadPoolExecutor executor = new ThreadPoolExecutor(taskThreadNum, taskThreadNum, 0, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(workQueenLength));
    executor.setRejectedExecutionHandler(new BlockRejectedExecutionHandler());
    return executor;
}
 
Example 13
Source File: ExecutorAutoConfiguration.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return the executor for UI background processes.
 */
@Bean(name = "uiExecutor")
@ConditionalOnMissingBean(name = "uiExecutor")
public Executor uiExecutor() {
    final BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(20);
    final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 20, 10000, TimeUnit.MILLISECONDS,
            blockingQueue, new ThreadFactoryBuilder().setNameFormat("ui-executor-pool-%d").build());
    threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    return new DelegatingSecurityContextExecutor(threadPoolExecutor);
}
 
Example 14
Source File: SharedResources.java    From rapid with Apache License 2.0 5 votes vote down vote up
/**
 * TPE with a rejected execution handler specified.
 */
private ThreadPoolExecutor newNamedThreadPool(final int threads, final String poolName, final Endpoint address) {
    final ThreadPoolExecutor tpe = new ThreadPoolExecutor(threads, threads,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(),
            newNamedThreadFactory(poolName, address));
    tpe.setRejectedExecutionHandler(new BackgroundExecutorRejectionHandler());
    return tpe;
}
 
Example 15
Source File: ReadaheadPool.java    From RDFS with Apache License 2.0 5 votes vote down vote up
private ReadaheadPool() {
  pool = new ThreadPoolExecutor(POOL_SIZE, MAX_POOL_SIZE, 3L, TimeUnit.SECONDS,
      new ArrayBlockingQueue<Runnable>(CAPACITY));
  pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
  pool.setThreadFactory(new ThreadFactoryBuilder()
    .setDaemon(true)
    .setNameFormat("Readahead Thread #%d")
    .build());
}
 
Example 16
Source File: AbstractHttpServer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
protected ThreadPoolExecutor initThreadPool(ServerConfig serverConfig) {
    ThreadPoolExecutor threadPool = BusinessPool.initPool(serverConfig);
    threadPool.setThreadFactory(new NamedThreadFactory("SEV-" + serverConfig.getProtocol().toUpperCase()
        + "-BIZ-" + serverConfig.getPort(), serverConfig.isDaemon()));
    threadPool.setRejectedExecutionHandler(new SofaRejectedExecutionHandler());
    if (serverConfig.isPreStartCore()) { // 初始化核心线程池
        threadPool.prestartAllCoreThreads();
    }
    return threadPool;
}
 
Example 17
Source File: SepConsumer.java    From hbase-indexer with Apache License 2.0 4 votes vote down vote up
/**
 * @param subscriptionTimestamp timestamp of when the index subscription became active (or more accurately, not
 *                              inactive)
 * @param listener              listeners that will process the events
 * @param threadCnt             number of worker threads that will handle incoming SEP events
 * @param hostName              hostname to bind to
 * @param payloadExtractor      extracts payloads to include in SepEvents
 */
public SepConsumer(String subscriptionId, long subscriptionTimestamp, EventListener listener, int threadCnt,
        String hostName, ZooKeeperItf zk, Configuration hbaseConf, PayloadExtractor payloadExtractor) throws IOException, InterruptedException {
    Preconditions.checkArgument(threadCnt > 0, "Thread count must be > 0");
    this.subscriptionId = SepModelImpl.toInternalSubscriptionName(subscriptionId);
    this.subscriptionTimestamp = subscriptionTimestamp;
    this.listener = listener;
    this.zk = zk;
    this.hbaseConf = hbaseConf;
    this.sepMetrics = new SepMetrics(subscriptionId);
    this.payloadExtractor = payloadExtractor;
    this.executors = Lists.newArrayListWithCapacity(threadCnt);

    InetSocketAddress initialIsa = new InetSocketAddress(hostName, 0);
    if (initialIsa.getAddress() == null) {
        throw new IllegalArgumentException("Failed resolve of " + initialIsa);
    }
    String name = "regionserver/" + initialIsa.toString();
    this.rpcServer = new RpcServer(this, name, getServices(),
    /*HBaseRPCErrorHandler.class, OnlineRegions.class},*/
            initialIsa, // BindAddress is IP we got for this server.
            //hbaseConf.getInt("hbase.regionserver.handler.count", 10),
            //hbaseConf.getInt("hbase.regionserver.metahandler.count", 10),
            hbaseConf,
            new FifoRpcScheduler(hbaseConf, hbaseConf.getInt("hbase.regionserver.handler.count", 10)));
      /*
      new SimpleRpcScheduler(
        hbaseConf,
        hbaseConf.getInt(HConstants.REGION_SERVER_HANDLER_COUNT, HConstants.DEFAULT_REGION_SERVER_HANDLER_COUNT),
        hbaseConf.getInt("hbase.regionserver.metahandler.count", 10),
        hbaseConf.getInt("hbase.regionserver.handler.count", 10),
        this,
        HConstants.QOS_THRESHOLD)
      );
      */
    this.serverName = ServerName.valueOf(hostName, rpcServer.getListenerAddress().getPort(), System.currentTimeMillis());
    this.zkWatcher = new ZooKeeperWatcher(hbaseConf, this.serverName.toString(), null);

    // login the zookeeper client principal (if using security)
    ZKUtil.loginClient(hbaseConf, "hbase.zookeeper.client.keytab.file",
            "hbase.zookeeper.client.kerberos.principal", hostName);

    // login the server principal (if using secure Hadoop)
    User.login(hbaseConf, "hbase.regionserver.keytab.file",
            "hbase.regionserver.kerberos.principal", hostName);

    for (int i = 0; i < threadCnt; i++) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 10, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(100));
        executor.setRejectedExecutionHandler(new WaitPolicy());
        executors.add(executor);
    }
}
 
Example 18
Source File: ThreadPoolManager.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a pool based on the configuration info.
 * <p>
 * @param config the pool configuration
 * @param threadNamePrefix prefix for the thread names of the pool
 * @param threadPriority the priority of the created threads
 * @return A ThreadPool wrapper
 */
public ExecutorService createPool( PoolConfiguration config, String threadNamePrefix, int threadPriority )
{
    BlockingQueue<Runnable> queue = null;
    if ( config.isUseBoundary() )
    {
        log.debug( "Creating a Bounded Buffer to use for the pool" );
        queue = new LinkedBlockingQueue<>(config.getBoundarySize());
    }
    else
    {
        log.debug( "Creating a non bounded Linked Queue to use for the pool" );
        queue = new LinkedBlockingQueue<>();
    }

    ThreadPoolExecutor pool = new ThreadPoolExecutor(
        config.getStartUpSize(),
        config.getMaximumPoolSize(),
        config.getKeepAliveTime(),
        TimeUnit.MILLISECONDS,
        queue,
        new DaemonThreadFactory(threadNamePrefix, threadPriority));

    // when blocked policy
    switch (config.getWhenBlockedPolicy())
    {
        case ABORT:
            pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
            break;

        case RUN:
            pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            break;

        case WAIT:
            throw new RuntimeException("POLICY_WAIT no longer supported");

        case DISCARDOLDEST:
            pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
            break;

        default:
            break;
    }

    pool.prestartAllCoreThreads();

    return pool;
}
 
Example 19
Source File: ManagedThreadPool.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
public ManagedThreadPool(ThreadPoolExecutor pool) {
    this.pool = pool;
    pool.setRejectedExecutionHandler(
            new CountingRejectionHandler(pool.getRejectedExecutionHandler()));
}
 
Example 20
Source File: GameSyncExecutor.java    From asteria-3.0 with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Creates and configures the update service for this game sync executor.
 * The returned executor is <b>unconfigurable</b> meaning it's configuration
 * can no longer be modified.
 * 
 * @param nThreads
 *            the amount of threads to create this service.
 * @return the newly created and configured service.
 */
private ExecutorService create(int nThreads) {
    if (nThreads <= 1)
        return null;
    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(nThreads);
    executor.setRejectedExecutionHandler(new CallerRunsPolicy());
    executor.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("GameSyncThread").build());
    return Executors.unconfigurableExecutorService(executor);
}