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.
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With