Spring 3 setting ThreadFactory for ThreadPoolTaskExecutor

Is this possible or is it managed by the application server? Passing ThreadPoolTaskExecutor ref to the bean is straightforward, but trying to set threadfactory for the above executor seems inefficient ...

+5
source share
1 answer

Actually, setting a ThreadFactoryalso does not cause difficulties:

<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>

or

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

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

Note what ThreadPoolTaskExecutorcomes out of ExecutorConfigurationSupportand is defined here setThreadFactory(java.util.concurrent.ThreadFactory).

+6
source

All Articles