jBPM job executor for Spring

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:

  • Using the org.jbpm.job.executor.JobExecutorThread class. This is a normal Java thread, so all the common thready things can be done with it. If you want to use these threads, also make sure to check the  org.jbpm.job.executor.JobExecutor class.
  • The enterprise version of jBPM has a JobExecutorServlet. Given a jBPM configuration, it starts up the job executor. The nice thing about this servlet is that it hides all the ugly implementation details of the threads and only needs some configuration
  • The enterprise version also has an EJB implementation which uses JMS. Altough this is quite cool, it is quite overkill for the most applications.

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);
jbpmConfiguration.startJobExecutor();

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.

Regards,

Joram

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” >

</bean>

I really like it: there is no extra bean needed using this approach.

One Comment

  1. william November 26, 2011

    Hi, I tried it, and it worked for two days, then my timers stoped working I dont know why. I used this line:

    org.jbpm.JbpmConfiguration.getInstance().startJobExecutor();

    I dont know why this stopped working, Im sure I havent changed anything.

Leave a Reply

Your email address will not be published. Required fields are marked *