Category Archives: activiti

Activiti Maven Archetype

As activiti developer, It happens too often that one has to create sample test cases . Some boilerplate code is necessary even for the most basic test case to run. Things like sample config file, sample process model, test case.

To make it faster, Joram has created a “Activiti Unit Test template” which makes life much easier. Personally I have used it a lot. It does most of the job, but again you need to do some customization to fully use it. To say, if you use multiple copies, without change, you can import only one of them in Eclipse.

There are tools for scaffolding and generating which allow faster, more dynamic and customizable templates. As maven is already used, maven archetype seems to be the logical tool.

In the most recent version of activiti (version 5.17.1) , the  first activiti maven archetype contains the same code written by Joram, in a new shell.

The archetype will be available in standard activiti maven repository. This simple command, will create a unit test template.

mvn archetype:generate
-DarchetypeGroupId=org.activiti.archetypes
-DarchetypeArtifactId=activiti-archetype-unittest
-DarchetypeVersion=5.17.1-SNAPSHOT
-DgroupId=org.myGroup
-DartifactId=myArtifact

 

The first three parameters  specify the archetype to be used. GroupId and artifactId determines your customized groupId and artifactId. Packages will be created according to the given group Id.

As simple is that.

This is just the start. Lot’s of extensions are possible. Ideas like creating a template for a web application (one with angular.js and one with jsf), selecting database type and so on.

 

Real Multithreaded parallel execution in activiti

Here is a real scenario:

We are going to implement a  web service using activiti. So we should be synchronous and quick. In this scenario call comes from Camel endoint, but it is not relevant for this discussion and we can ignore it safely without loosing generality.

To conclude the service, we need to call 3 other external web services. External web services take random amount of time to return back the result. After all 3 web services are returned, we need to aggregate their returned results and return the result of aggregation as the return value of our web service.

This diagram shows the outline:

parallelSampleSerial

 

This works fine. As each step is synchronous, activiti executes the next step only when the previous one is returned. The returned values is stored as a process variable. At aggregation we have all the returned values and we can aggregate.

But if the web service calls are independent, we can run them in parallel and save time and resources. And here is where all the problems begin.

BPMN has already a parallel gateway available. The first temptation is  using that feature. something like this:

parallelSampleParallel

 

It looks nice on the paper, but we know that at activiti uses a single thread to run all the three paths. This means that at the end of the day, they are sequenced. There is no differences to the previous one, except that we cannot be sure about the order of the execution of web service calls. Not very promising !

What if we make the parallel services asynchronous? Ok, that is not easy as well. Here is a nice post from Tijs about how it could be done. It might be a little outdated though, thank to the new Async Job Executor starting from version 5.17.0.

With this approach, aggregate part is done properly. Activiti makes sure, the aggregate service is called only when all 3 web service calls are finished. good !

But the problem is that we need to return aggregated result as the return of web service. As the parallel tasks are asynchronous, the control returns to the caller immediately, without waiting for the external web service calls to return. So we don’t have the result yet. We need to wait and synchronize for the result to come. Usual way to do it is by locks and monitors in Java. Not exactly feasible here, as the the very same objects are not passed to