Category Archives: java

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:



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:



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 the process. They are serialized and deserialized  via database.

To solve that problem, we can maintain a global static array of objects, and only pass the reference to the proper lock object to the activiti. In aggregation service, the static object is found and signaled with the help of passed index. Signaling causes the waiting thread to release and result to return back to web service.

Just as a rough proof of concept there I have created a  sample, available  in   parallelMultiThreaded in github.

In the sample MyUnitTest test class simulates the body of newly created web service call. Service1, Service2, Service3 simulate external web service calls, simply by sleeping. The executed model is the second one above with parallel gateway.

The static lock and condition arrays are defined as:

 public static Condition[] conditions = new Condition[1000];
 public static Lock[] locks = new Lock[1000];


index, is given a random number (0, not much random I confess) and passed to the activiti.
After starting the instance, main code goes to an unlimited wait loop, waiting for the corresponding object to be unlocked:


After all the external web services are executed, activiti executes aggregation task, which in turn signals the lock:


The log files clearly show that everything goes fine:

02:35:16,817 [pool-1-thread-1] INFO  org.activiti.Service1  - Service 1 started, time = 70
02:35:16,817 [pool-1-thread-2] INFO  org.activiti.Service2  - Service 2 started, time = 70
02:35:16,817 [pool-1-thread-3] INFO  org.activiti.Service3  - Service 3 started, time = 70
02:35:17,318 [pool-1-thread-3] INFO  org.activiti.Service3  - Service 3 finished, time = 571
02:35:17,519 [pool-1-thread-2] INFO  org.activiti.Service2  - Service 2 finished, time = 772
02:35:17,819 [pool-1-thread-1] INFO  org.activiti.Service1  - Service 1 finished, time = 1072
02:35:17,859 [pool-1-thread-1] INFO  org.activiti.AggregateService  - All external ws calls returned. Try to unlock the lock. time = 1112
02:35:17,859 [main] INFO  org.activiti.MyUnitTest  - wait loop exited. Program finished. total taime spent = 1112

It is clear that tasks are running simultaneously  and aggregation and monitoring are done properly. Also there is no considerable delay for unlocking and signaling.


Ok, I am cheating. The time for sleeping are intentionally set to different values with proper distance. Otherwise contention will cause Asynchronous job executor, to retry. This adds several seconds to the whole process. But that is not the subject of this post.

The sample code is not considering optimistic locking problem as is described in the post above, neither it handles timeout in waiting for lock to be release,  but well shows the concept. So with this workaround, it is possible to use activiti parallel gateway for executing external web service really multi-threaded and respond the result synchronously.

But one may suggest to overlook activiti parallel gateways, and do the thread synchronization in a single Java delegate. That way it would be much  conciser and easier to implement. It is much faster, as it does need communicating via database. The cons is that you can not see the real flow in the diagram.


Obviously this only works when everything is running in a single jvm. It is possible to implement it in a cluster, but even the current implementation is not much practical to the long delay caused by asynchronous job picking mechanism

PS: Thank to Joram’s hint, I replaced the low level wait and notify to higher level ReenterantLock.




How to mock web services

In black box testing, sometimes we need to test a system which consumes and produces web services. Here is my experience on how to test such a system.

you need to call a the exposed web service and check how the consumed webs services are called. Exposed web service is already provided by the system, and you need only a client to call it. I used CXF. very straight forward and easy.

But the consumed web services are not there yet. They are not in the scope of tests and mostly not in our control at all.

One approach is to create fix implementation of thos web services, again using a library like CXF and  when the callback functions are called, check that the parametrs are passed correctly and resturn a suitable result for test of other parts at the same time.

This approach works fine only for the simplest cases. If you want to test different scenarios, the fixed mock web service should react differently and you have to put the logic inside it. Sometimes you meay need to have more than one implementation and re-publish the service with the alternative implementations. Not beautiful I admit.

A more beautiful approach is to use mocking frameworks. Here I have selected Mockito.

Here are the steps:

First you have to create a mock object, a normal one based on the service interface. I do it via annotation. like this:

static ServiceInterface serviceMock;

But this should be called from a web service. For any strange reason, it could not be done directly. I had to create a reflecter class to do the job:

public class ServiceProxy implements java.lang.reflect.InvocationHandler {
    private Object obj;

    public static Object newInstance(Object obj) {
        return java.lang.reflect.Proxy.newProxyInstance(
            new ServiceProxy(obj));

    private ServiceProxy(Object obj) {
        this.obj = obj;

    public Object invoke(Object proxy, Method m, Object[] args)
        throws Throwable {
        Object result;
        try {
            result = m.invoke(obj, args);
        } catch (Exception e) {
            throw new RuntimeException("unexpected invocation exception: " +

        return result;

then it is easy, you can define your web service mock and start it.

service = (ServiceInterface)         ServiceProxy.newInstance(serviceMock);
serviceEndpoint = Endpoint.publish(
				"http://localhost:8080/myService", service);

Every thing is ready now. you can run the system and use the mock, just like a normal mock.
As an example of the usage, this code tests if a method is called in web service. Obviously all the mock language of Mockito can be used for more complex tests.


Enjoy your testing !

To present it in a more concrete way I have created a sample usage. It could be found here:

There is also a worldline version. It uses resource locator to find web service.

Expression Language Validation

We used juel as an expression language EL implementation in one our projects. The problem we had, was that we could not afford waiting for the expression to be checked at runtime. For example if you are accessing a pojo field, and that field does not exist, they you only know it in runtime.

To fix this, I created an extension and added a validate method. This method traverses the tree and checks all possible options to make sure the expression is valid with given objects.

I offered the new extention to the community, but they thought it is better to keep the Juel to minimum required to match the standard.

I am going  to publish as an extension to Juel in seperate branch. Maybe someone else also can make use of it. Not planned yet though.

MDA Approach for Card Scheme conversion

The idea is to use MDA approach for conversion of one card schema to the other one. The traditional way is to write java code field by field. What I suggested is to use MDA (Model Driven Approach) for the conversion. The idea is to define the model in a kind of object model, use XML to make the model and create the framework to execute the mode.

During the implementation I understood that usage of standard EL (Expression Language) would be very handy and I successfully tried Juel.

In this way, the conversion rules are much more meaningful and understandable. It is also possible to change them dynamically without recompiling.

There is only one important negative point and that one is type safety. Not all expression in Juel are checked against syntax in type of loading. My next task on this subject is to find how can I guaranty type safety, at least at the same level as java code.

Default page in Tapestry

In the current release of Tapestry version 5.0, there is an odd behavior about default index page. Here is the problem. I have used Tynamo for enforcing security. The desired behavior is that all pages should be authenticated. If you access them, before logging you should be redirected to /login.tml page. Our main page is set as default /index.tml page.

you can configure tynamo on which pages to protect in AppModule.contributeSecurityConfiguration
[code lang=”java”]

this works fine, as you long as access the main page by directly accessing root at http://server.port/app/

But what happens if you try to access a non existent page, some thing like http://server.port/app/blahblah ? strange unchangeable behavior of tapestry, directs this url to default index page, after that it is checked by tynamo. So your protected page is revealed easily. Tynamo has also no way to protect wild pages with exception. Something like
[code lang=”java”]

will also protect /login.tml and prohibits logging in.

The best I came to, was to move the index page to some other page, say /processlist and protect by tynamo. I changed the to:

[code lang=”java”]
public class Index {
Object onActivate() throws MalformedURLException {
return ProcessList.class;

Now if you access your arbitary page, it is redirected to the main page, but this time before security checking and every thing works fine.

A turtle named Hibernate

Hibernate, is a fantastic tool, as long as it is not used in very high number of transactions. In our case for a transactional  application, with a relatively complex data model, it is disastrous. It is one single hibernate insert, which is translated to 5 related sql inserts. Oracle 11g is the test database.

Database tools report less than ms for the real database work.

about 16 ms, is spent on oracle JDBC drivers. And guess how much is spent by hibernate.

about 90 ms.  

I am working on it. Stay tuned.

stand alone JNDI providers

I am need to find some non container based JNDI providers. Here is a short list:


1. Sun filesystem provider (

2. Spring JNDI

3. Apache Harmony (RMI based)

4. Google Simple Jndi

5. ActiveMQ jndi provider