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

The following examples show how to use org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#setKeepAliveSeconds() . 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: TaskExecutorFactoryBean.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void afterPropertiesSet() {
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	determinePoolSizeRange(executor);
	if (this.queueCapacity != null) {
		executor.setQueueCapacity(this.queueCapacity);
	}
	if (this.keepAliveSeconds != null) {
		executor.setKeepAliveSeconds(this.keepAliveSeconds);
	}
	if (this.rejectedExecutionHandler != null) {
		executor.setRejectedExecutionHandler(this.rejectedExecutionHandler);
	}
	if (this.beanName != null) {
		executor.setThreadNamePrefix(this.beanName + "-");
	}
	executor.afterPropertiesSet();
	this.target = 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: 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 4
Source File: TaskExecutorConfig.java    From batch-scheduler with MIT License 6 votes vote down vote up
@Bean
public TaskExecutor taskExecutor() {

    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    // 设置核心线程数
    executor.setCorePoolSize(10);
    // 设置最大线程数
    executor.setMaxPoolSize(15);
    // 设置队列容量
    executor.setQueueCapacity(20);
    // 设置线程活跃时间(秒)
    executor.setKeepAliveSeconds(60);
    // 设置默认线程名称
    executor.setThreadNamePrefix("batch-task-running-");
    // 设置拒绝策略
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    // 等待所有任务结束后再关闭线程池
    executor.setWaitForTasksToCompleteOnShutdown(true);

    return executor;
}
 
Example 5
Source File: TaskExecutorConfig.java    From spring-boot-cookbook with Apache License 2.0 6 votes vote down vote up
@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(2);
    taskExecutor.setQueueCapacity(25);//如果任务数大于corePoolSize则放到Queue中,queue只能存放25个阻塞的任务
    //如果需要执行的任务数大于corePoolSize+QueueCapacity则会创建(maxPoolSize-corePoolSize)个线程来继续执行任务
    taskExecutor.setMaxPoolSize(20);
    //如果大于需要执行的任务数大于maxPoolSize+QueueCapacity,
    // 则会根据RejectedExecutionHandler策略来决定怎么处理这些超出上面配置的任务
    //此处使用CallerRunsPolicy:会通过new 一个Thread的方式来执行这些任务
    taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    taskExecutor.setKeepAliveSeconds(60);//实际工作的线程数大于corePoolSize时,如果60s没有被使用则销毁

    taskExecutor.setWaitForTasksToCompleteOnShutdown(true);//线程池会等任务完成后才会关闭
    taskExecutor.setAwaitTerminationSeconds(60);//如果60s后线程仍未关闭,则关闭线程池
    taskExecutor.setThreadNamePrefix("ThreadPoolTaskExecutor-");//实际显示为: [lTaskExecutor-1] c.t.learning.thread.AsyncTaskService     :
    taskExecutor.initialize();
    return taskExecutor;
}
 
Example 6
Source File: ExecutorConfig.java    From youkefu with Apache License 2.0 6 votes vote down vote up
/**
 * 作业平台使用的线程池
 * @return
 */
@Bean(name = "uKeFuTaskExecutor")
public ThreadPoolTaskExecutor taskExecutor() {

	ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
	// 线程池维护线程的最少数量
	poolTaskExecutor.setCorePoolSize(CORE_POOL_SIZE);
	// 线程池维护线程的最大数量
	poolTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
	// 线程池所使用的缓冲队列
	poolTaskExecutor.setQueueCapacity(200);
	// 线程池维护线程所允许的空闲时间
	poolTaskExecutor.setKeepAliveSeconds(30);
	poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
	poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
	return poolTaskExecutor;
}
 
Example 7
Source File: TaskPoolConfig.java    From SpringBootLearn with Apache License 2.0 6 votes vote down vote up
@Bean(name = "taskExecutor")
public Executor taskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    // 核心线程数
    executor.setCorePoolSize(10);
    // 最大线程数
    executor.setMaxPoolSize(20);
    // 缓存队列
    executor.setQueueCapacity(200);
    // 允许线程的空闲时间60秒
    executor.setKeepAliveSeconds(60);
    // 线程池名的前缀
    executor.setThreadNamePrefix("taskExecutor-");
    // 线程池对拒绝任务的处理策略
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    // 设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
    executor.setWaitForTasksToCompleteOnShutdown(true);
    // 设置线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,以确保应用最后能够被关闭
    executor.setAwaitTerminationSeconds(60);
    return executor;
}
 
Example 8
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 9
Source File: RedisCacheConfig.java    From agile-service-old with Apache License 2.0 5 votes vote down vote up
/**
 * 异步方法删除redis缓存的线程池
 */
@Bean("redisTaskExecutor")
public Executor taskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.setMaxPoolSize(50);
    executor.setQueueCapacity(1000);
    executor.setKeepAliveSeconds(60);
    executor.setThreadNamePrefix("delete-redis-cache-");
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    return executor;
}
 
Example 10
Source File: AsyncExecutorConfig.java    From NFVO with Apache License 2.0 5 votes vote down vote up
@Override
@Bean
@Scope("prototype")
public ThreadPoolTaskExecutor getAsyncExecutor() {
  ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  executor.setCorePoolSize(corePoolSize);
  executor.setQueueCapacity(queueCapacity);
  executor.setMaxPoolSize(maxPoolSize > 0 ? maxPoolSize : Integer.MAX_VALUE);
  executor.setKeepAliveSeconds(keepAliveSeconds);
  executor.setThreadNamePrefix("OpenBatonAsyncTask-");
  executor.initialize();
  return executor;
}
 
Example 11
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 12
Source File: FebsRouteEnhanceConfigure.java    From FEBS-Cloud with Apache License 2.0 5 votes vote down vote up
@Bean(FebsConstant.ASYNC_POOL)
public ThreadPoolTaskExecutor asyncThreadPoolTaskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.setMaxPoolSize(20);
    executor.setQueueCapacity(100);
    executor.setKeepAliveSeconds(30);
    executor.setThreadNamePrefix("Febs-Gateway-Async-Thread");
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setAwaitTerminationSeconds(60);
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.initialize();
    return executor;
}
 
Example 13
Source File: FebsWebConfigure.java    From FEBS-Cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 注册异步线程池
 */
@Bean(FebsConstant.ASYNC_POOL)
public ThreadPoolTaskExecutor asyncThreadPoolTaskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.setMaxPoolSize(20);
    executor.setQueueCapacity(100);
    executor.setKeepAliveSeconds(30);
    executor.setThreadNamePrefix("Febs-Async-Thread");
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setAwaitTerminationSeconds(60);
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.initialize();
    return executor;
}
 
Example 14
Source File: GeneralConfig.java    From feiqu-opensource with Apache License 2.0 5 votes vote down vote up
@Bean(name = "threadPoolTaskExecutor")
public ThreadPoolTaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
    // 线程池维护线程的最少数量
    pool.setCorePoolSize(10);
    // 线程池维护线程的最大数量
    pool.setMaxPoolSize(1000);
    // 当调度器shutdown被调用时等待当前被调度的任务完成
    pool.setWaitForTasksToCompleteOnShutdown(true);
    pool.setKeepAliveSeconds(300);
    pool.setQueueCapacity(20000);
    pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    return pool;
}
 
Example 15
Source File: CoreSpringModuleConfig.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an Async "task" executor which is also a normal "executor".It is also being used by the "@EnableAsync" annotation and the fact that this class
 * implements AsyncConfigurer. That way, all methods annotated with "@Async" will be executed asynchronously by this executor.
 *
 * @return the async task executor.
 */
@Override
@Bean // This will call the "initialize" method of the ThreadPoolTaskExecutor automatically.
public TaskExecutor getAsyncExecutor()
{
    // Create a Spring thread pool "task" executor that is backed by a JDK Thread Pool Executor.
    // Use the environment to make the key thread pool parameters configurable although changing them would require a server restart.
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(configurationHelper.getProperty(ConfigurationValue.THREAD_POOL_CORE_POOL_SIZE, Integer.class));
    executor.setMaxPoolSize(configurationHelper.getProperty(ConfigurationValue.THREAD_POOL_MAX_POOL_SIZE, Integer.class));
    executor.setKeepAliveSeconds(configurationHelper.getProperty(ConfigurationValue.THREAD_POOL_KEEP_ALIVE_SECS, Integer.class));
    executor.setQueueCapacity(configurationHelper.getProperty(ConfigurationValue.THREAD_POOL_QUEUE_CAPACITY, Integer.class));
    return executor;
}
 
Example 16
Source File: Web3Config.java    From WeBASE-Transaction with Apache License 2.0 5 votes vote down vote up
/**
 * set sdk threadPool.
 * 
 * @return
 */
@Bean
public ThreadPoolTaskExecutor sdkThreadPool() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(corePoolSize);
    executor.setMaxPoolSize(maxPoolSize);
    executor.setQueueCapacity(queueCapacity);
    executor.setKeepAliveSeconds(keepAlive);
    executor.setRejectedExecutionHandler(new AbortPolicy());
    executor.setThreadNamePrefix("sdkThreadPool-");
    executor.initialize();
    return executor;
}
 
Example 17
Source File: ServiceSpringModuleConfig.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Activiti's dedicated TaskExecutor bean definition.
 *
 * @return TaskExecutor
 */
@Bean
public TaskExecutor activitiTaskExecutor()
{
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(configurationHelper.getProperty(ConfigurationValue.ACTIVITI_THREAD_POOL_CORE_POOL_SIZE, Integer.class));
    taskExecutor.setMaxPoolSize(configurationHelper.getProperty(ConfigurationValue.ACTIVITI_THREAD_POOL_MAX_POOL_SIZE, Integer.class));
    taskExecutor.setKeepAliveSeconds(configurationHelper.getProperty(ConfigurationValue.ACTIVITI_THREAD_POOL_KEEP_ALIVE_SECS, Integer.class));
    taskExecutor.setQueueCapacity(configurationHelper.getProperty(ConfigurationValue.ACTIVITI_THREAD_POOL_QUEUE_CAPACITY, Integer.class));
    return taskExecutor;
}
 
Example 18
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 19
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;
}
 
Example 20
Source File: SchedulingConfig.java    From NettyReverseProxy with Apache License 2.0 5 votes vote down vote up
@Bean(name = "frontendWorkTaskExecutor")
public Executor createFrontendWorkTaskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(1);
    executor.setMaxPoolSize(1);
    executor.setKeepAliveSeconds(0);
    executor.setQueueCapacity(0);
    executor.setDaemon(true);
    executor.setThreadNamePrefix("frontendWorkTaskExecutor-");
    return executor;
}