cancel
Showing results for 
Search instead for 
Did you mean: 

How to control serviceTask execution

robdiana72
Champ in-the-making
Champ in-the-making

In Activiti, we have a need to control how jobs (serviceTasks) are executed. More specifically, we have some jobs that we want to limit to 2 concurrently, but other jobs may run 10 concurrently. All of these jobs are serviceTasks, no Timers or other async code. I have looked at the JobExecutor (and the Async version) but it seems to deal only with async code. How can I control this type of serviceTask execution?

1 ACCEPTED ANSWER

daisuke-yoshimo
Star Collaborator
Star Collaborator

> There used to be a setJobExecutor method in the ProcessEngineConfigurationImpl class but it appears to be missing now.

・Synchronous Job Executor

You should override DefaultJobExecutor.class and setJobExecutor to ProcessEngineConfiguration.

ProcessEngineConfiguration.setJobExecutor(org.activiti.engine.impl.jobexecutor.JobExecutor jobExecutor)
https://www.activiti.org/javadocs/org/activiti/engine/ProcessEngineConfiguration.html#setJobExecutor...

・Async Job Executor
You should override DefaultAsyncJobExecutor.class and setAsyncExecutor to ProcessEngineConfiguration.

ProcessEngineConfiguration.setAsyncExecutor(org.activiti.engine.impl.asyncexecutor.AsyncExecutor asyncExecutor)

https://www.activiti.org/javadocs/org/activiti/engine/ProcessEngineConfiguration.html#setAsyncExecut...

View answer in original post

8 REPLIES 8

gdharley
Elite Collaborator
Elite Collaborator

I don't believe this is something you will be able to control using the Job Executor parameters.

These are general parameters to manage your job pool and maximum number of concurrent jobs so you can manage your memory, threads and other JVM resources.

For the service tasks that are restricted to two concurrent executions, I believe you will need to handle these in a different way.

I would have the service task b a stand alone application that reached into the process engine to see if there are any jobs waiting (or alternatively have the process add a message to a queue and have the handler monitor the queue) . The handler will then execute the job and respond (say on a message bus). The message listener in the process can then pull the payload and move on.

This way the self contained handler can manage how many concurrent requests it handles. The approach also throttles execution such that all messages get handled in due course, but not necessarily immediately.

The Camunda engine (a fork of activiti) implements this pattern as what they call external tasks:

External Tasks | docs.camunda.org 

Hope this helps,
Greg

robdiana72
Champ in-the-making
Champ in-the-making

Greg,

Thanks for the suggestion. I had been thinking about an option like this, so I am glad to see it as somewhat recommended.

I know the default JobExecutor does not handle this situation, and we are supposed to be able to supply a different one, but there is no documentation on that kind of thing. I would prefer to get to one solution instead of a solution per workflow.

Thanks.

Rob

gdharley
Elite Collaborator
Elite Collaborator

Hi Robert,

Since the Async Job executor was introduced the ability to completely override the Job Executor seems to have gone away.
There used to be a setJobExecutor method in the ProcessEngineConfigurationImpl class but it appears to be missing now.

However, you can still overload the Job Handlers with your own implementation (which is likely all you need).

Custom Job handlers are registered against a given job type (e.g. ProcessEventJobHandler).

Hopefully this will give you what you need.

Greg

daisuke-yoshimo
Star Collaborator
Star Collaborator

> There used to be a setJobExecutor method in the ProcessEngineConfigurationImpl class but it appears to be missing now.

・Synchronous Job Executor

You should override DefaultJobExecutor.class and setJobExecutor to ProcessEngineConfiguration.

ProcessEngineConfiguration.setJobExecutor(org.activiti.engine.impl.jobexecutor.JobExecutor jobExecutor)
https://www.activiti.org/javadocs/org/activiti/engine/ProcessEngineConfiguration.html#setJobExecutor...

・Async Job Executor
You should override DefaultAsyncJobExecutor.class and setAsyncExecutor to ProcessEngineConfiguration.

ProcessEngineConfiguration.setAsyncExecutor(org.activiti.engine.impl.asyncexecutor.AsyncExecutor asyncExecutor)

https://www.activiti.org/javadocs/org/activiti/engine/ProcessEngineConfiguration.html#setAsyncExecut...

There is no such method in the current ProcessEngineConfiguration classes.
Refer: Activiti/ProcessEngineConfiguration.java at master · Activiti/Activiti · GitHub 

And: Activiti/ProcessEngineConfigurationImpl.java at master · Activiti/Activiti · GitHub 

This method appears to have disappeared in version 5.14.

Greg

My apologies, when I grep'd my source I could not find this reference.

So as long as your configuration class extends ProcessEngineConfiguration you will have the ability to define a custom JobExecutor. Interesting that the Impl class doesnt include this method.

That said, as you can still plug your own Job Executor into the engine, take a look at the ManagedJobExecutor for an example of a custom module.

Greg

robdiana72
Champ in-the-making
Champ in-the-making

Thanks to both of you for your help. I will be pursuing the custom JobExecutor to see if I can get that idea to work.