转载请注明出处 http://www.paraller.com
原文排版地址 点击跳转

属性Bean:

@Configuration
public class ContactsExecutor {

    @Value("${contacts.thread.core-pool}")
    private int corePoolSize;

    @Value("${contacts.thread.max-pool}")
    private int maxPoolSize;

    @Value("${contacts.queue.capacity}")
    private int queueCapacity;

    @Value("${contacts.thread.timeout}")
    private int threadTimeout;

    @Bean
    @Qualifier("contactsExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
        threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
        threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
        threadPoolTaskExecutor.setKeepAliveSeconds(threadTimeout);

        return threadPoolTaskExecutor;
    }
}

线程配置文件:

# thread pool and queue size for processing contacts data
contacts.thread.timeout=2
contacts.thread.core-pool=10
contacts.thread.max-pool=25
contacts.queue.capacity=25

主程序入口添加注释:

@EnableAsync
@ComponentScan
@EnableAutoConfiguration
public class Application {
.......
}

调用类:

@Async("mainExecutor")
public void methodA(){}

Configure queue and thread pool for async execution


paraller
207 声望12 粉丝