Tuesday, 22 June 2010

jBPM Custom Mail Producer

As stated in the jBPM developers guide (13.4.1. Custom Producers) it is possible to plug in your own custom mail producer.

However, the topic in the developers guide is very brief and just by implementing the interface or extending the MailProducerImpl your code won’t work.

Option one: “Google”…. No luck this time - makes me wondering how many people really use jBPM 4?

Second option: diving into the source code myself and complete the puzzle on my own. After a couple of hours I came to a solutions that does the trick.

As you can see in the source code instantiating a mail provider is done in org.jbpm.jpdl.internal.xml.JpdlParser:


So to enable a process to use a custom mail producer you extend the notification node with a class attribute as shown below:


So far, so good.
Final step: writing your custom producer.
I started simply by creating an empty class which extends from MailProducerImpl :


Running my code however resulted in the following error: Exception in thread "main" java.lang.NullPointerException


It learned me that in case of specifying a custom mail producer a mail template is not set.


To solve this error adjust the constructor of the mail producer by adding the following code:


At the end this is how my Mail producer looks like (it is still extended from the default mail producer but at this point just implementing the interface would probably do the trick)

Last thing I want to mention is the override of the produce method. In that method I retrieve the current task from the TaskContext. This opens the possibility to use it in the other methods of the class . (Below also a code snippet from the org.jbpm.jpdl.internal.activity.MailListener which is responsible for maintaining the TaskContext).



Now you can spam yourself while testing and fine tuning your code :-)