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

The following examples show how to use org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#setAwaitTerminationSeconds() . 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: 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 2
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 3
Source File: AsyncExecutorConfiguration.java    From booties with Apache License 2.0 6 votes vote down vote up
@ConditionalOnProperty(prefix = "async.executor", name = "enabled", matchIfMissing = true)
@Bean(name = DEFAULT_TASK_EXECUTOR_BEAN_NAME, destroyMethod = "shutdown")
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public ThreadPoolTaskExecutor threadPoolTaskExecutor(AsyncExecutorProperties properties) {
    log.info("Creating ThreadPoolTaskExecutor 'taskExecutor' with core-size={} max-size={} queue-capacity={}",
            properties.getCorePoolSize(), properties.getMaxPoolSize(), properties.getQueueCapacity());

    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(properties.getCorePoolSize());
    executor.setMaxPoolSize(properties.getMaxPoolSize());

    executor.setQueueCapacity(properties.getQueueCapacity());
    executor.setThreadNamePrefix(properties.getThreadNamePrefix());
    executor.setAwaitTerminationSeconds(properties.getAwaitTerminationSeconds());

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

    executor.afterPropertiesSet();
    return executor;
}
 
Example 4
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 5
Source File: MyPoolTask.java    From springBoot-study with Apache License 2.0 6 votes vote down vote up
@Bean("MyExecutor")
public Executor taskExecutor() {
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	//核心线程数、最大线程数、任务队列数
	executor.setCorePoolSize(10);
	executor.setMaxPoolSize(20);
	executor.setQueueCapacity(200);
	executor.setKeepAliveSeconds(60);
	executor.setThreadNamePrefix("taskExecutor-");
	//设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
	executor.setWaitForTasksToCompleteOnShutdown(true);
	//设置线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住。
    executor.setAwaitTerminationSeconds(60);
	executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
	return executor;
}
 
Example 6
Source File: MyPoolTask.java    From springBoot-study with Apache License 2.0 6 votes vote down vote up
@Bean("MyExecutor")
public Executor taskExecutor() {
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	//核心线程数、最大线程数、任务队列数
	executor.setCorePoolSize(10);
	executor.setMaxPoolSize(20);
	executor.setQueueCapacity(200);
	executor.setKeepAliveSeconds(60);
	executor.setThreadNamePrefix("taskExecutor-");
	//设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
	executor.setWaitForTasksToCompleteOnShutdown(true);
	//设置线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住。
    executor.setAwaitTerminationSeconds(60);
	executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
	return executor;
}
 
Example 7
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 8
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 9
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 10
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 11
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 12
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 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: ThreadPoolConfig.java    From code with Apache License 2.0 5 votes vote down vote up
@Bean("myThreadPool")
public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
    ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
    threadPool.setCorePoolSize(1);
    threadPool.setMaxPoolSize(1);
    threadPool.setKeepAliveSeconds(30);
    threadPool.setQueueCapacity(1000);
    threadPool.setAllowCoreThreadTimeOut(true);
    threadPool.setAwaitTerminationSeconds(10);
    // 丢弃任务,不抛出异常
    threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
    threadPool.initialize();
    return threadPool;
}
 
Example 15
Source File: AsynThreadPoolConfig.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
@Bean("taskExecutor")
public Executor taskExecutor() {
    log.info("Begin to init taskExecutor");
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(8);
    executor.setMaxPoolSize(8);
    executor.setQueueCapacity(0);
    executor.setKeepAliveSeconds(60);
    executor.setThreadNamePrefix("taskExecutor-");
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setAwaitTerminationSeconds(60);
    executor.initialize();
    return executor;
}
 
Example 16
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 17
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 18
Source File: FebsJobConfigure.java    From FEBS-Cloud with Apache License 2.0 5 votes vote down vote up
@Bean
public ThreadPoolTaskExecutor scheduleJobExecutorService() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.setMaxPoolSize(10);
    executor.setQueueCapacity(20);
    executor.setKeepAliveSeconds(30);
    executor.setThreadNamePrefix("Febs-Job-Thread");
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setAwaitTerminationSeconds(60);
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.initialize();
    return executor;
}
 
Example 19
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;
}