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

The following examples show how to use org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#setAllowCoreThreadTimeOut() . 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: 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 2
Source File: AppConfig.java    From spring-boot-ddd with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ThreadPoolTaskExecutor appExecutor() {
    ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
    //TODO put configs on the properties
    //TODO define an equation for pool size
    t.setCorePoolSize(350);
    t.setMaxPoolSize(350);
    t.setQueueCapacity(1000);
    t.setAllowCoreThreadTimeOut(true);
    t.setKeepAliveSeconds(120);
    t.initialize();
    return t;
}
 
Example 3
Source File: TaskExecutorRegistration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected ThreadPoolTaskExecutor getTaskExecutor() {
	ThreadPoolTaskExecutor executor = (this.taskExecutor != null ? this.taskExecutor : new ThreadPoolTaskExecutor());
	executor.setCorePoolSize(this.corePoolSize);
	executor.setMaxPoolSize(this.maxPoolSize);
	executor.setKeepAliveSeconds(this.keepAliveSeconds);
	executor.setQueueCapacity(this.queueCapacity);
	executor.setAllowCoreThreadTimeOut(true);
	return executor;
}
 
Example 4
Source File: DaqModule.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public ThreadPoolTaskExecutor daqThreadPoolTaskExecutor() {
  ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  executor.setCorePoolSize(properties.getJms().getUpdate().getNumExecutorThreads());
  executor.setMaxPoolSize(properties.getJms().getUpdate().getNumExecutorThreads());
  executor.setKeepAliveSeconds(properties.getJms().getUpdate().getKeepAliveSeconds());
  executor.setAllowCoreThreadTimeOut(true);
  executor.setThreadNamePrefix(THREAD_NAME_PREFIX);
  return executor;
}
 
Example 5
Source File: CachePersistenceModule.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public ThreadPoolTaskExecutor cachePersistenceThreadPoolTaskExecutor() {
  ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  executor.setCorePoolSize(properties.getNumExecutorThreads());
  executor.setMaxPoolSize(properties.getNumExecutorThreads());
  executor.setKeepAliveSeconds(properties.getKeepAliveSeconds());
  executor.setQueueCapacity(properties.getQueueCapacity());
  executor.setAllowCoreThreadTimeOut(true);
  executor.setThreadNamePrefix(THREAD_NAME_PREFIX);
  executor.initialize();
  return executor;
}
 
Example 6
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 7
Source File: TaskExecutorFactoryBean.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void determinePoolSizeRange(ThreadPoolTaskExecutor executor) {
	if (StringUtils.hasText(this.poolSize)) {
		try {
			int corePoolSize;
			int maxPoolSize;
			int separatorIndex = this.poolSize.indexOf('-');
			if (separatorIndex != -1) {
				corePoolSize = Integer.valueOf(this.poolSize.substring(0, separatorIndex));
				maxPoolSize = Integer.valueOf(this.poolSize.substring(separatorIndex + 1, this.poolSize.length()));
				if (corePoolSize > maxPoolSize) {
					throw new IllegalArgumentException(
							"Lower bound of pool-size range must not exceed the upper bound");
				}
				if (this.queueCapacity == null) {
					// No queue-capacity provided, so unbounded
					if (corePoolSize == 0) {
						// Actually set 'corePoolSize' to the upper bound of the range
						// but allow core threads to timeout...
						executor.setAllowCoreThreadTimeOut(true);
						corePoolSize = maxPoolSize;
					}
					else {
						// Non-zero lower bound implies a core-max size range...
						throw new IllegalArgumentException(
								"A non-zero lower bound for the size range requires a queue-capacity value");
					}
				}
			}
			else {
				Integer value = Integer.valueOf(this.poolSize);
				corePoolSize = value;
				maxPoolSize = value;
			}
			executor.setCorePoolSize(corePoolSize);
			executor.setMaxPoolSize(maxPoolSize);
		}
		catch (NumberFormatException ex) {
			throw new IllegalArgumentException("Invalid pool-size value [" + this.poolSize + "]: only single " +
					"maximum integer (e.g. \"5\") and minimum-maximum range (e.g. \"3-5\") are supported", ex);
		}
	}
}
 
Example 8
Source File: TaskExecutorFactoryBean.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void determinePoolSizeRange(ThreadPoolTaskExecutor executor) {
	if (StringUtils.hasText(this.poolSize)) {
		try {
			int corePoolSize;
			int maxPoolSize;
			int separatorIndex = this.poolSize.indexOf('-');
			if (separatorIndex != -1) {
				corePoolSize = Integer.valueOf(this.poolSize.substring(0, separatorIndex));
				maxPoolSize = Integer.valueOf(this.poolSize.substring(separatorIndex + 1, this.poolSize.length()));
				if (corePoolSize > maxPoolSize) {
					throw new IllegalArgumentException(
							"Lower bound of pool-size range must not exceed the upper bound");
				}
				if (this.queueCapacity == null) {
					// No queue-capacity provided, so unbounded
					if (corePoolSize == 0) {
						// Actually set 'corePoolSize' to the upper bound of the range
						// but allow core threads to timeout...
						executor.setAllowCoreThreadTimeOut(true);
						corePoolSize = maxPoolSize;
					}
					else {
						// Non-zero lower bound implies a core-max size range...
						throw new IllegalArgumentException(
								"A non-zero lower bound for the size range requires a queue-capacity value");
					}
				}
			}
			else {
				Integer value = Integer.valueOf(this.poolSize);
				corePoolSize = value;
				maxPoolSize = value;
			}
			executor.setCorePoolSize(corePoolSize);
			executor.setMaxPoolSize(maxPoolSize);
		}
		catch (NumberFormatException ex) {
			throw new IllegalArgumentException("Invalid pool-size value [" + this.poolSize + "]: only single " +
					"maximum integer (e.g. \"5\") and minimum-maximum range (e.g. \"3-5\") are supported", ex);
		}
	}
}
 
Example 9
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;
}