Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3 setting ThreadFactory for ThreadPoolTaskExecutor

Is this possible or is it managed by the Application Server? Passing the ThreadPoolTaskExecutor ref to a bean is a no-brainer but trying to set the threadfactory on the aforementioned executor seems to have no effect ...

like image 582
Christopher Dancy Avatar asked Feb 27 '12 20:02

Christopher Dancy


1 Answers

Actually, setting a ThreadFactory is a no-brainer as well:

<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 that ThreadPoolTaskExecutor extends from ExecutorConfigurationSupport and this is where setThreadFactory(java.util.concurrent.ThreadFactory) is defined.

like image 96
Tomasz Nurkiewicz Avatar answered Oct 23 '22 13:10

Tomasz Nurkiewicz