Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Spring's ThreadPoolExecutorFactoryBean factory bean

I would like to be able to inject an ExecutorService instance into my Spring services, and the Spring API suggest using ThreadPoolExecutorFactoryBean for this purpose. Very simple question; how the hell do I use the ThreadPoolExecutorFactoryBean to create an ExecutorService that I can wire into my other services?

I feel like a complete idiot for asking his question, but I can't seem to get this figured out.

like image 331
tmbrggmn Avatar asked May 21 '11 17:05

tmbrggmn


People also ask

What is the use of factory bean in Spring?

A FactoryBean is a pattern to encapsulate interesting object construction logic in a class. It might be used, for example, to encode the construction of a complex object graph in a reusable way. Often this is used to construct complex objects that have many dependencies.


2 Answers

To expand on skaffman's answer, here's a short and sweet example of what one needs to do:

<bean id="classNeedingExecutor" class="foo.Bar">
  <property name="executor" ref="threadExecutor" />
</bean>

<bean id="threadExecutor"
  class="org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean">
  <property name="corePoolSize" value="1" />
  <property name="maxPoolSize" value="1" />
</bean>

Again, see the JavaDocs for an explanation of the other properties that can be set to configure the ExecutorService.

like image 113
Duncan Jones Avatar answered Sep 22 '22 12:09

Duncan Jones


First off, you need to learn what a FactoryBean is - read section 3.8.3 of the spring docs.

Then, you read the Javadoc for ThreadPoolExecutorFactoryBean and understand what it does.

Next, you configure a ThreadPoolExecutorFactoryBean in your context. This will create an ExecutorService (since it's a FactoryBean, see above), which you can inject into your bean.

like image 44
skaffman Avatar answered Sep 20 '22 12:09

skaffman