Today, a collegue of me asked how the jBPM job executor is started using Spring. Before going into details: the job executor in jBPM is used to execute jobs (truly an original name). A jBPM job is a unit of work which has a certain duedate. The most common usage of a jBPM job is a timer: when an amount of time has expired, an action must happen. A more complex usage is asynchronous messaging, where the messages are wrapped into jobs.
Now, the default jBPM distribution contains a so-called ‘job executor’. In a nutshell: this component checks if there are new jobs available (now > duedate) and executes the attached action. There are three ways of starting the job executor:
The thing is, these three options aren’t really fit for a Spring based application. Off course, you can call servlets or EJBs in one way or the other using Spring, but it still remains quite clumsy.
The nice thing about open source frameworks is the fact that they are … well … open. So, given the source code of the JobExecutorServlet, it is easy to see that the clever framework creators have narrowed the necessary steps down to these 2 lines (which can be written in 1 line actually):
jbpmConfiguration = JbpmConfiguration.getInstance(configurationName);
By using the Spring org.springframework.context.ApplicationListener interface, one can listen to application context calls. So, if we execute the previous lines while loading the Spring application context, the jBPM job executor threads will also be started. The resulting bean can be found here. Simply declare it in the Spring config and inject a jBPM configuration.
So, that’s it. There’s nothing special or difficult about it, but I hope it’ll save some time for at least one fellow jBPM developer.
Update: Andries Inzé showed me that there is another way of achieving the same by using Spring bean “init or factory methods”:
<bean id=”jbpmConfiguration” factory-method=”getInstance” init-method=”startJobExecutor” class=”org.jbpm.JbpmConfiguration” >
I really like it: there is no extra bean needed using this approach.