AnsweredAssumed Answered

Throttling solution discussion

Question asked by mandas on Oct 1, 2012
Latest reply on Oct 3, 2012 by jbarrez

We are using Activiti-5.10 for a project and according to our client needs, all service tasks are exclusive and asynchronous. There was a need to implement a throttling mechanism for those service tasks in a way that execution metrics are enforced for a specific task id. For example a rule which says that the services tasks of id 'someTask' are allowed to have 5 'simultaneous' executions at the same time, which on async terms means that at most 5 'someTask' can be scheduled for acquiring by the job executor. The remaining tasks have to stay in a queue and wait for the running tasks to fall below 5 instances.

As we couldn't implement such thing through the API and we have made several ad-hoc customizations by overriding classes and db mappings, I 'm posting our approach here if anyone is able to provide any suggestions of the proper way to do through the API, if any, because such an approach is tightly binded to internal activiti code.

We have overriden ProcessEngineConfiguration.getMyBatisXmlConfigurationSteam() to read a different mappings.xml which included one additional table of the job ids which were acquired by the job executor but our mechanism decided to postpone their execution. Furthermore, we provided a different Job.xml which modifies selectNextJobsToExecute and selectExclusiveJobsToExecute queries in order to avoid fetching jobs which are on the new table. Afterwards, we have implemented our custom AcquiredJobsCmd which handles the conversation between the executor and the throttling mechanism. We 've also overriden DefaultJobExecutor.ensureInitialization() to provide our custom AcquiredJobsCmd while at the same time we 've also overriden ExecuteJobsRunnable class, to add a single statement which informs the throttling mechanism that a job execution has been finished. Finally, another thread which is running at a specified interval, it deletes the postponed job ids from the new table, thus making them available for the job executor to acquire.