views:

163

answers:

2

I want to run background process in parallel with my spring-mvc web-application. I need a way to start in automatically on context loading. Background process is a class that implements Runnable. Is spring-mvc has some facilities for that?

+5  A: 

Spring has a comprehensive task execution framework. See the relevant part of the docs.

I suggest having a Spring bean in your context, which, when initialized, submits your background Runnable to a SimpleAsyncTaskExecutor bean. That's the simplest approach, which you can make more complex and capable as you see fit.

skaffman
+2  A: 

I would go ahead and look at the task scheduling documentation linked by skaffman, but there's also a simpler way if all you really want to do is fire up a background thread at context initialization time.

<bean id="myRunnableThingy">
  ...
</bean>

<bean id="thingyThread" class="java.lang.Thread" init-method="start">
  <constructor-arg ref="myRunnableThingy"/>
</bean>
washley