Spring 3为ThreadPoolTask​​Executor设置ThreadFactory

时间:2012-02-27 20:05:43

标签: java spring javabeans threadpool

这可能还是由Application Server管理?将ThreadPoolTask​​Executor引用传递给bean是不费脑子的但是试图在上述执行器上设置threadfactory似乎没有效果......

1 个答案:

答案 0 :(得分:6)

实际上,设置ThreadFactory也是明智的选择:

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="threadFactory" ref="threadFactory"/>
</bean>
<bean id="threadFactory" class="org.springframework.scheduling.concurrent.CustomizableThreadFactory">
    <constructor-arg value="Custom-prefix-"/>
</bean>

或:

@Bean
public ThreadPoolTaskExecutor taskExecutor() {
    final ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setThreadFactory(threadFactory());
    return taskExecutor;
}

@Bean
public ThreadFactory threadFactory() {
    return new CustomizableThreadFactory("Custom-prefix-");
}

请注意ThreadPoolTaskExecutorExecutorConfigurationSupport延伸,这是定义setThreadFactory(java.util.concurrent.ThreadFactory)的位置。