Java Code Examples for org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#setRejectedExecutionHandler()

The following examples show how to use org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#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: AsyncTaskExecutePool.java    From sk-admin with Apache License 2.0 6 votes vote down vote up
@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    //核心线程池大小
    executor.setCorePoolSize(config.getCorePoolSize());
    //最大线程数
    executor.setMaxPoolSize(config.getMaxPoolSize());
    //队列容量
    executor.setQueueCapacity(config.getQueueCapacity());
    //活跃时间
    executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
    //线程名字前缀
    executor.setThreadNamePrefix("sk-async-");
    // setRejectedExecutionHandler:当pool已经达到max size的时候,如何处理新任务
    // CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.initialize();
    return executor;
}
 
Example 2
Source File: AsyncTaskExecutePool.java    From springboot-learn with MIT License 6 votes vote down vote up
@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(config.getCorePoolSize());
    executor.setMaxPoolSize(config.getMaxPoolSize());
    executor.setQueueCapacity(config.getQueueCapacity());
    executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
    executor.setThreadNamePrefix(config.getThreadNamePrefix());
    //线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy
    //AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 -->
    //CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度 -->
    //DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
    //DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.initialize();
    return executor;
}
 
Example 3
Source File: AsyncConfiguration.java    From seed with Apache License 2.0 6 votes vote down vote up
@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setThreadNamePrefix("SeedExecutor-");
    executor.setMaxPoolSize(this.maxPoolSize);     //线程池最大数量
    executor.setCorePoolSize(this.corePoolSize);   //线程池最小数量
    executor.setQueueCapacity(this.queueCapacity); //队列大小(最小的线程数被占满后,新任务会放进queue)
    executor.setAwaitTerminationSeconds(60 * 15);
    executor.setWaitForTasksToCompleteOnShutdown(true);
    ////饱和策略:自定义
    //executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
    //    @Override
    //    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
    //        //---------
    //    }
    //});
    //饱和策略:队列满时,使用预定义的异常处理类
    //ABORT(缺省)  :不执行,并抛TaskRejectedException异常
    //DISCARD       :不执行,也不抛异常,直接丢弃任务
    //DISCARD_OLDEST:丢弃queue中最旧的那个任务,并执行当期任务
    //CALLER_RUNS   :不在新线程中执行任务,而是由调用者所在的线程来执行
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.initialize();
    return executor;
}
 
Example 4
Source File: SpringAsyncConfig.java    From charging_pile_cloud with MIT License 6 votes vote down vote up
@Bean
public TaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    //配置核心线程数
    executor.setCorePoolSize(200);
    //配置最大线程数
    executor.setMaxPoolSize(1000);
    //配置队列大小
    executor.setQueueCapacity(400);
    //配置线程池中的线程的名称前缀
    executor.setThreadNamePrefix("thread-");
    // rejection-policy:当pool已经达到max size的时候,如何处理新任务
    // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    //执行初始化
    executor.initialize();
    return executor;
}
 
Example 5
Source File: Web3SDKConnector.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
public static ThreadPoolTaskExecutor initThreadPool(int core, int max, int keepalive) {
    // init thread pool
    ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
    pool.setThreadNamePrefix("web3sdk-");
    pool.setCorePoolSize(core);
    pool.setMaxPoolSize(max);
    // queue conflict with thread pool scale up, forbid it
    pool.setQueueCapacity(0);
    pool.setKeepAliveSeconds(keepalive);
    // abort policy
    pool.setRejectedExecutionHandler(null);
    pool.setDaemon(true);
    pool.initialize();

    log.info("init ThreadPoolTaskExecutor");
    return pool;
}
 
Example 6
Source File: AsyncPoolConfig.java    From SpringAll with MIT License 6 votes vote down vote up
@Bean
public ThreadPoolTaskExecutor asyncThreadPoolTaskExecutor(){
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(20);
    executor.setMaxPoolSize(200);
    executor.setQueueCapacity(25);
    executor.setKeepAliveSeconds(200);
    executor.setThreadNamePrefix("asyncThread");
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setAwaitTerminationSeconds(60);

    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

    executor.initialize();
    return executor;
}
 
Example 7
Source File: TaskExecutePool.java    From canal-mongo with Apache License 2.0 5 votes vote down vote up
@Bean
public ThreadPoolTaskExecutor myTaskAsyncPool() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(config.getCorePoolSize());
    executor.setMaxPoolSize(config.getMaxPoolSize());
    executor.setQueueCapacity(config.getQueueCapacity());
    executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
    executor.setThreadNamePrefix("Async-");
    // rejection-policy:当pool已经达到max size的时候,如何处理新任务
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
    executor.initialize();
    return executor;
}
 
Example 8
Source File: WebConfigration.java    From FATE-Serving with Apache License 2.0 5 votes vote down vote up
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(coreSize>0?coreSize:processors);
    executor.setMaxPoolSize(maxSize>0?maxSize:2*processors);
    executor.setThreadNamePrefix("ProxyAsync");
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
    executor.initialize();
    configurer.setTaskExecutor(executor);
    configurer.setDefaultTimeout(timeout);
    configurer.registerCallableInterceptors(new TimeoutCallableProcessingInterceptor());
}
 
Example 9
Source File: AsyncConfig.java    From dbys with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(20);
    executor.setMaxPoolSize(100);
    executor.setQueueCapacity(100);
    executor.setKeepAliveSeconds(60);
    executor.setThreadNamePrefix("taskExecutor-");
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.initialize();
    return executor;
}
 
Example 10
Source File: AemServiceConfiguration.java    From jwala with Apache License 2.0 5 votes vote down vote up
@Bean(name = "jvmTaskExecutor")
public TaskExecutor getJvmTaskExecutor(@Qualifier("pollingThreadFactory") final ThreadFactory threadFactory,
                                       @Value("${jvm.thread-task-executor.pool.size}") final int corePoolSize,
                                       @Value("${jvm.thread-task-executor.pool.max-size}") final int maxPoolSize,
                                       @Value("${jvm.thread-task-executor.pool.queue-capacity}") final int queueCapacity,
                                       @Value("${jvm.thread-task-executor.pool.keep-alive-sec}") final int keepAliveSeconds) {
    final ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
    threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
    threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
    threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
    threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
    threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    threadPoolTaskExecutor.setThreadFactory(threadFactory);
    return threadPoolTaskExecutor;
}
 
Example 11
Source File: BladeExecutorConfiguration.java    From blade-tool with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	executor.setCorePoolSize(bladeAsyncProperties.getCorePoolSize());
	executor.setMaxPoolSize(bladeAsyncProperties.getMaxPoolSize());
	executor.setQueueCapacity(bladeAsyncProperties.getQueueCapacity());
	executor.setKeepAliveSeconds(bladeAsyncProperties.getKeepAliveSeconds());
	executor.setThreadNamePrefix("async-executor-");
	executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
	return executor;
}
 
Example 12
Source File: AsycTaskExecutorConfig.java    From open-capacity-platform with Apache License 2.0 5 votes vote down vote up
@Bean
public TaskExecutor taskExecutor() {
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
       executor.setCorePoolSize(corePoolSize);
       executor.setMaxPoolSize(maxPoolSize);
       executor.setQueueCapacity(queueCapacity);
       executor.setThreadNamePrefix("MyExecutor-");

       // rejection-policy:当pool已经达到max size的时候,如何处理新任务
       // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
       executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
       executor.initialize();
       return executor;
}
 
Example 13
Source File: TaskConfiguration.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
@Bean("taskExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(100);
        executor.setMaxPoolSize(400);
        executor.setQueueCapacity(400);
        executor.setKeepAliveSeconds(60);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        executor.setThreadNamePrefix("taskExecutor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
//        executor.setTaskDecorator(new GrayTaskDecorator());
        return executor;
    }
 
Example 14
Source File: PlowThreadPools.java    From plow with Apache License 2.0 5 votes vote down vote up
/**
 * Handles communication with RNDaemon.
 */
@Bean(name="rndCommandExecutor")
public ThreadPoolTaskExecutor rndCommandExecutor() {
    ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
    t.setCorePoolSize(plowCfg.get("plow.rndpool.cache.threads", 8));
    t.setMaxPoolSize(plowCfg.get("plow.rndpool.cache.threads", 8));
    t.setThreadNamePrefix("RndRun");
    t.setDaemon(false);
    t.setQueueCapacity(1000);
    t.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    return t;
}
 
Example 15
Source File: AsyncConfig.java    From springboot-guide with Apache License 2.0 5 votes vote down vote up
@Bean
public Executor taskExecutor() {
  ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  // 核心线程数
  executor.setCorePoolSize(CORE_POOL_SIZE);
  // 最大线程数
  executor.setMaxPoolSize(MAX_POOL_SIZE);
  // 队列大小
  executor.setQueueCapacity(QUEUE_CAPACITY);
  // 当最大池已满时,此策略保证不会丢失任务请求,但是可能会影响应用程序整体性能。
  executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
  executor.setThreadNamePrefix("My ThreadPoolTaskExecutor-");
  executor.initialize();
  return executor;
}
 
Example 16
Source File: ProvisioningContext.java    From syncope with Apache License 2.0 5 votes vote down vote up
/**
 * Used by {@link org.apache.syncope.core.provisioning.java.propagation.PriorityPropagationTaskExecutor}.
 *
 * @return executor
 */
@Bean
public Executor propagationTaskExecutorAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(env.getProperty("propagationTaskExecutorAsyncExecutor.corePoolSize", Integer.class));
    executor.setMaxPoolSize(env.getProperty("propagationTaskExecutorAsyncExecutor.maxPoolSize", Integer.class));
    executor.setQueueCapacity(env.getProperty("propagationTaskExecutorAsyncExecutor.queueCapacity", Integer.class));
    executor.setThreadNamePrefix("PropagationTaskExecutor-");
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
    executor.initialize();
    return executor;
}
 
Example 17
Source File: ProvisioningContext.java    From syncope with Apache License 2.0 5 votes vote down vote up
/**
 * Annotated as {@code @Primary} because it will be used by {@code @Async} in {@link AsyncConnectorFacade}.
 *
 * @return executor
 */
@Bean
@Primary
public Executor asyncConnectorFacadeExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(env.getProperty("asyncConnectorFacadeExecutor.corePoolSize", Integer.class));
    executor.setMaxPoolSize(env.getProperty("asyncConnectorFacadeExecutor.maxPoolSize", Integer.class));
    executor.setQueueCapacity(env.getProperty("asyncConnectorFacadeExecutor.queueCapacity", Integer.class));
    executor.setThreadNamePrefix("AsyncConnectorFacadeExecutor-");
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
    executor.initialize();
    return executor;
}
 
Example 18
Source File: ThreadPoolConfig.java    From biliob_backend with MIT License 5 votes vote down vote up
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor singleThreadPool = new ThreadPoolTaskExecutor();
    singleThreadPool.setCorePoolSize(20);
    singleThreadPool.setMaxPoolSize(20);
    singleThreadPool.setQueueCapacity(20);
    singleThreadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    singleThreadPool.initialize();
    return singleThreadPool;
}
 
Example 19
Source File: AsyncTaskExecutePool.java    From momo-cloud-permission with Apache License 2.0 5 votes vote down vote up
/**
 * myTaskAsynPool即配置线程池的方法名,此处如果不写自定义线程池的方法名,会使用默认的线程池
 */
//@Async("threadPoolTaskExecutor")
// Future<String> future = new AsyncResult<>("success!");
@Bean("threadPoolTaskExecutor")
@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    //此方法返回可用处理器的虚拟机的最大数量; 不小于1
    int core = Runtime.getRuntime().availableProcessors();
    //核心线程池大小
    executor.setCorePoolSize(core);
    //最大线程数
    executor.setMaxPoolSize(core * 2 + 1);
    //队列容量
    executor.setQueueCapacity(queueCapacity);
    //活跃时间
    executor.setKeepAliveSeconds(keepAliveSeconds);
    //线程名字前缀
    executor.setThreadNamePrefix("taskExecutor-");

    // setRejectedExecutionHandler:当pool已经达到max size的时候,如何处理新任务
    // CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行
    //对拒绝task的处理策略
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    //加载
    executor.initialize();
    return executor;
}
 
Example 20
Source File: SiteConfiguration.java    From mblog with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public TaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(2);
    executor.setMaxPoolSize(8);
    executor.setQueueCapacity(100);
    executor.setKeepAliveSeconds(60);
    executor.setThreadNamePrefix("mtons.mblog.logThread-");
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.setWaitForTasksToCompleteOnShutdown(true);
    return executor;
}