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

The following examples show how to use org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#setWaitForTasksToCompleteOnShutdown() . 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: AsyncThreadPoolConfig.java    From hdw-dubbo with Apache License 2.0 6 votes vote down vote up
/**
 * 对于CPU密集型任务,最大线程数是CPU线程数+1。对于IO密集型任务,尽量多配点,可以是CPU线程数*2,或者CPU线程数/(1-阻塞系数)。
 * maxPoolSize=new Double(Math.floor(Runtime.getRuntime().availableProcessors()/(1-0.9))).intValue()
 */
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    // 设置核心线程数
    executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
    // 设置最大线程数
    executor.setMaxPoolSize(new Double(Math.floor(Runtime.getRuntime().availableProcessors() / (1 - 0.9))).intValue());
    // 设置队列容量
    executor.setQueueCapacity(Runtime.getRuntime().availableProcessors());
    // 设置线程活跃时间(秒)
    executor.setKeepAliveSeconds(60);
    // 设置默认线程名称
    executor.setThreadNamePrefix(env.getProperty("spring.application.name") + "-");
    // 设置拒绝策略
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    // 等待所有任务结束后再关闭线程池
    executor.setWaitForTasksToCompleteOnShutdown(true);
    return executor;
}
 
Example 2
Source File: ThreadUtil.java    From Milkomeda with MIT License 6 votes vote down vote up
/**
 * 自定义配置ThreadPoolTaskExecutor
 *
 * @param taskExecutor     ThreadPoolTaskExecutor
 * @param prefix           前辍
 * @param corePoolSize     核心池大小
 * @param maxPoolSize      最大线程池数
 * @param queueCapacity    队列容量
 * @param keepAliveSeconds 线程保存存活时间
 */
public static void configTaskExecutor(ThreadPoolTaskExecutor taskExecutor, String prefix, int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds) {
    // 线程池维护线程的最少数量
    taskExecutor.setCorePoolSize(corePoolSize);
    // 线程池维护线程的最大数量
    taskExecutor.setMaxPoolSize(maxPoolSize);
    // 线程池所使用的缓冲队列
    taskExecutor.setQueueCapacity(queueCapacity);
    // 线程池维护线程所允许的空闲时间
    taskExecutor.setKeepAliveSeconds(keepAliveSeconds);
    // 调度器shutdown被调用时等待当前被调度的任务完成
    taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
    // 等待时长
    taskExecutor.setAwaitTerminationSeconds(60);
    taskExecutor.setThreadNamePrefix(prefix);
    // 线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy,默认为后者
    taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    taskExecutor.setTaskDecorator(new RequestContextDecorator());
    taskExecutor.initialize();
}
 
Example 3
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 4
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 5
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 6
Source File: TaskExecution.java    From data-prep with Apache License 2.0 5 votes vote down vote up
/**
 * @return an Authenticated task executor ready to run.
 */
protected AsyncListenableTaskExecutor getAsyncExecutor() {
    final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(2);
    executor.setMaxPoolSize(10);
    executor.setWaitForTasksToCompleteOnShutdown(false);
    executor.initialize();
    return executor;
}
 
Example 7
Source File: AsyncConfig.java    From SpringBootBucket with MIT License 5 votes vote down vote up
@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(10);
    executor.setMaxPoolSize(100);
    executor.setQueueCapacity(100);
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setAwaitTerminationSeconds(60 * 10);
    executor.setThreadNamePrefix("AsyncThread-");
    executor.initialize(); //如果不初始化,导致找到不到执行器
    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: AppConfig.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Bean
public AsyncTaskExecutor intermediateBuilderExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(intermediateCorePoolSize);
    executor.setQueueCapacity(intermediateQueueCapacity);
    executor.setThreadNamePrefix("intermediateBuilderExecutor-");
    executor.setTaskDecorator(new MDCCleanerTaskDecorator());
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setAwaitTerminationSeconds(AWAIT_TERMINATION_SECONDS);
    executor.initialize();
    return executor;
}
 
Example 10
Source File: AsyncConfig.java    From Pixiv-Illustration-Collection-Backend with Apache License 2.0 5 votes vote down vote up
@Bean
@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(10);
    executor.setMaxPoolSize(10);
    executor.setQueueCapacity(100000);
    executor.setThreadNamePrefix("async-pool-");
    executor.setTaskDecorator(new MdcTaskDecorator());
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.initialize();
    return executor;
}
 
Example 11
Source File: AsyncConfiguration.java    From dk-foundation with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Bean
@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(10);
    executor.setMaxPoolSize(10);
    executor.setQueueCapacity(100);
    executor.setThreadNamePrefix("async-pool-");
    executor.setTaskDecorator(new MdcTaskDecorator());
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.initialize();
    return executor;
}
 
Example 12
Source File: AsyncConfiguration.java    From data-prep with Apache License 2.0 5 votes vote down vote up
/**
 * @return the 'engine' (thread pool) that runs behind the ManagedTaskExecutor.
 */
@Bean(name = "managedTaskEngine")
public AsyncListenableTaskExecutor getManagedTaskExecutorEngine() {
    final ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
    threadPoolTaskExecutor.setCorePoolSize(maxConcurrentRuns);
    threadPoolTaskExecutor.setMaxPoolSize(maxConcurrentRuns);
    threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(false);
    threadPoolTaskExecutor.initialize();

    return threadPoolTaskExecutor;
}
 
Example 13
Source File: SchedulerConfig.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@Bean(name = "taskExecutor")
@Primary
public TaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setMaxPoolSize(5);
    executor.setCorePoolSize(2);
    executor.setQueueCapacity(25);
    executor.setWaitForTasksToCompleteOnShutdown(false);

    return executor;
}
 
Example 14
Source File: SecurityPubSubChannel.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ThreadPoolTaskExecutor executor() {
    ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
    pool.setCorePoolSize(10);
    pool.setMaxPoolSize(10);
    pool.setWaitForTasksToCompleteOnShutdown(true);
    return pool;
}
 
Example 15
Source File: AppConfig.java    From staffjoy with MIT License 5 votes vote down vote up
@Bean(name=ASYNC_EXECUTOR_NAME)
public Executor asyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setTaskDecorator(new ContextCopyingDecorator());
    executor.setCorePoolSize(3);
    executor.setMaxPoolSize(5);
    executor.setQueueCapacity(100);
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setThreadNamePrefix("AsyncThread-");
    executor.initialize();
    return executor;
}
 
Example 16
Source File: AppConfig.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Bean
public AsyncTaskExecutor resourceBuilderExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(corePoolSize);
    executor.setQueueCapacity(queueCapacity);
    executor.setThreadNamePrefix("resourceBuilderExecutor-");
    executor.setTaskDecorator(new MDCCleanerTaskDecorator());
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setAwaitTerminationSeconds(AWAIT_TERMINATION_SECONDS);
    executor.initialize();
    return executor;
}
 
Example 17
Source File: TaskExecution.java    From data-prep with Apache License 2.0 5 votes vote down vote up
/**
 * @return an Authenticated task executor for event multi casting.
 * @see DataPrepEvents
 */
@Bean(name = "applicationEventMulticaster#executor")
public TaskExecutor dataPrepAsyncTaskExecutor() {
    final ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(2);
    taskExecutor.setMaxPoolSize(10);
    taskExecutor.setWaitForTasksToCompleteOnShutdown(false);
    taskExecutor.initialize();
    return taskExecutor;
}
 
Example 18
Source File: FlowableJobConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public TaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(8);
    executor.setMaxPoolSize(8);
    executor.setQueueCapacity(100);
    executor.setThreadNamePrefix("flowable-task-Executor-");
    executor.setAwaitTerminationSeconds(30);
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setAllowCoreThreadTimeOut(true);
    executor.initialize();
    return executor;
}
 
Example 19
Source File: AppConfig.java    From staffjoy with MIT License 5 votes vote down vote up
@Bean(name=ASYNC_EXECUTOR_NAME)
public Executor asyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    // for passing in request scope context
    executor.setTaskDecorator(new ContextCopyingDecorator());
    executor.setCorePoolSize(3);
    executor.setMaxPoolSize(5);
    executor.setQueueCapacity(100);
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setThreadNamePrefix("AsyncThread-");
    executor.initialize();
    return executor;
}
 
Example 20
Source File: AsyncConfiguration.java    From eds-starter6-jpa with Apache License 2.0 4 votes vote down vote up
@Override
public Executor getAsyncExecutor() {
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

	if (this.properties.getCorePoolSize() != null) {
		executor.setCorePoolSize(this.properties.getCorePoolSize());
	}

	if (this.properties.getMaxPoolSize() != null) {
		executor.setMaxPoolSize(this.properties.getMaxPoolSize());
	}

	if (this.properties.getQueueCapacity() != null) {
		executor.setQueueCapacity(this.properties.getQueueCapacity());
	}

	if (this.properties.getThreadNamePrefix() != null) {
		executor.setThreadNamePrefix(this.properties.getThreadNamePrefix());
	}

	if (this.properties.getAllowCoreThreadTimeOut() != null) {
		executor.setAllowCoreThreadTimeOut(
				this.properties.getAllowCoreThreadTimeOut());
	}

	if (this.properties.getWaitForTasksToCompleteOnShutdown() != null) {
		executor.setWaitForTasksToCompleteOnShutdown(
				this.properties.getWaitForTasksToCompleteOnShutdown());
	}

	if (this.properties.getAwaitTerminationSeconds() != null) {
		executor.setAwaitTerminationSeconds(
				this.properties.getAwaitTerminationSeconds());
	}

	if (this.properties.getKeepAliveSeconds() != null) {
		executor.setKeepAliveSeconds(this.properties.getKeepAliveSeconds());
	}

	executor.initialize();
	return executor;
}