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

The following examples show how to use org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#setThreadNamePrefix() . 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: AsyncConfiguration.java    From seed with Apache License 2.0 6 votes vote down vote up
@Bean
public Executor seedSimpleExecutor(){
    //ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    //scheduler.setThreadNamePrefix("MyScheduler-"); //线程名称前缀
    //scheduler.setPoolSize(1000);                   //线程池大小
    //scheduler.setThreadPriority(2);                //线程优先级
    //scheduler.initialize();
    //return scheduler;
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setThreadNamePrefix("SeedSimpleExecutor-");
    executor.setCorePoolSize(10); //线程池最小数量
    executor.setMaxPoolSize(200); //线程池最大数量
    executor.setQueueCapacity(5); //队列大小(最小的线程数被占满后,新任务会放进queue)
    executor.initialize();
    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: 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 4
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 5
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 6
Source File: AbstractMessageBrokerConfiguration.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Bean
public ThreadPoolTaskExecutor clientInboundChannelExecutor() {
	TaskExecutorRegistration reg = getClientInboundChannelRegistration().taskExecutor();
	ThreadPoolTaskExecutor executor = reg.getTaskExecutor();
	executor.setThreadNamePrefix("clientInboundChannel-");
	return executor;
}
 
Example 7
Source File: AsyncConfiguration.java    From alchemy with Apache License 2.0 5 votes vote down vote up
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
    log.debug("Creating Async Task Executor");
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(taskExecutionProperties.getPool().getCoreSize());
    executor.setMaxPoolSize(taskExecutionProperties.getPool().getMaxSize());
    executor.setQueueCapacity(taskExecutionProperties.getPool().getQueueCapacity());
    executor.setThreadNamePrefix(taskExecutionProperties.getThreadNamePrefix());
    return new ExceptionHandlingAsyncTaskExecutor(executor);
}
 
Example 8
Source File: AsyncConfiguration.java    From flair-registry with Apache License 2.0 5 votes vote down vote up
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
    log.debug("Creating Async Task Executor");
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
    executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
    executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
    executor.setThreadNamePrefix("j-hipster-registry-Executor-");
    return new ExceptionHandlingAsyncTaskExecutor(executor);
}
 
Example 9
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 10
Source File: AsyncConfiguration.java    From 21-points with Apache License 2.0 5 votes vote down vote up
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
    log.debug("Creating Async Task Executor");
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
    executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
    executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
    executor.setThreadNamePrefix("twenty-one-points-Executor-");
    return new ExceptionHandlingAsyncTaskExecutor(executor);
}
 
Example 11
Source File: AsyncConfiguration.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
    log.debug("Creating Async Task Executor");
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
    executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
    executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
    executor.setThreadNamePrefix("umd-Executor-");
    return new ExceptionHandlingAsyncTaskExecutor(executor);
}
 
Example 12
Source File: AsyncConfiguration.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
    log.debug("Creating Async Task Executor");
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
    executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
    executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
    executor.setThreadNamePrefix("umm-Executor-");
    return new ExceptionHandlingAsyncTaskExecutor(executor);
}
 
Example 13
Source File: AgentAutoConfiguration.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * Get a lazy {@link AsyncTaskExecutor} bean which may be shared by different components if one isn't already
 * defined.
 *
 * @return A {@link ThreadPoolTaskExecutor} instance
 */
@Bean
@Lazy
@ConditionalOnMissingBean(name = "sharedAgentTaskExecutor", value = AsyncTaskExecutor.class)
public AsyncTaskExecutor sharedAgentTaskExecutor() {
    final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.setThreadNamePrefix("agent-task-executor-");
    return executor;
}
 
Example 14
Source File: RqueueMessageListenerContainerTest.java    From rqueue with Apache License 2.0 5 votes vote down vote up
@Test
public void setTaskExecutor() {
  assertNull(container.getTaskExecutor());
  ThreadPoolTaskExecutor asyncTaskExecutor = new ThreadPoolTaskExecutor();
  asyncTaskExecutor.setThreadNamePrefix("testExecutor");
  container.setTaskExecutor(asyncTaskExecutor);
  assertEquals(
      "testExecutor",
      ((ThreadPoolTaskExecutor) container.getTaskExecutor()).getThreadNamePrefix());
}
 
Example 15
Source File: AsyncConfiguration.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
    log.debug("Creating Async Task Executor");
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
    executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
    executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
    executor.setThreadNamePrefix("ucomposer-Executor-");
    return new ExceptionHandlingAsyncTaskExecutor(executor);
}
 
Example 16
Source File: WebSocketConfig.java    From kafka-webview with MIT License 5 votes vote down vote up
/**
 * This thread runs the WebSocketConsumerManager, which manages any consumers for web sockets.
 * It only needs a single thread, because the manager starts up its own managed thread pool.
 * @return new ThreadPool Task executor.
 */
@Bean
public TaskExecutor backgroundConsumerExecutor() {
    final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    // Only a single thread in the pool
    executor.setCorePoolSize(1);
    executor.setMaxPoolSize(1);
    executor.setThreadNamePrefix("Web Socket Consumer Manager");
    executor.initialize();

    return executor;
}
 
Example 17
Source File: AsyncConfiguration.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 5 votes vote down vote up
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
    log.debug("Creating Async Task Executor");
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
    executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
    executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
    executor.setThreadNamePrefix("blog-Executor-");
    return new ExceptionHandlingAsyncTaskExecutor(executor);
}
 
Example 18
Source File: AsyncTaskExecutorConfiguration.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
	log.debug("Creating Async Task Executor");
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	executor.setCorePoolSize(paascloudProperties.getTask().getCorePoolSize());
	executor.setMaxPoolSize(paascloudProperties.getTask().getMaxPoolSize());
	executor.setQueueCapacity(paascloudProperties.getTask().getQueueCapacity());
	executor.setKeepAliveSeconds(paascloudProperties.getTask().getKeepAliveSeconds());
	executor.setThreadNamePrefix(paascloudProperties.getTask().getThreadNamePrefix());
	return new ExceptionHandlingAsyncTaskExecutor(executor);
}
 
Example 19
Source File: TasksAutoConfiguration.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * Get a task executor for executing tasks asynchronously that don't need to be scheduled at a recurring rate.
 *
 * @param tasksExecutorPoolProperties The properties for the task executor thread pool
 * @return The task executor the system to use
 */
@Bean
@ConditionalOnMissingBean(name = "genieAsyncTaskExecutor")
public AsyncTaskExecutor genieAsyncTaskExecutor(final TasksExecutorPoolProperties tasksExecutorPoolProperties) {
    final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(tasksExecutorPoolProperties.getSize());
    executor.setThreadNamePrefix(tasksExecutorPoolProperties.getThreadNamePrefix());
    return executor;
}
 
Example 20
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;
}