Setting a thread factory for async tasks

cancel
Showing results for 
Search instead for 
Did you mean: 
boris-petrov
Member II

Setting a thread factory for async tasks

Jump to solution

My use case is this: I'm using the async executor. I have some async service tasks and I want to set the thread names for the threads that will be executing these tasks. I guess the same applies for script tasks - I want the thread that executes them to have a specific name. How can I achieve these things?

1 Solution

Accepted Solutions
gdharley
Intermediate

Re: Setting a thread factory for async tasks

Jump to solution

This would require that you overload the Job Executor service classes.

You can find the necessary classes under here:
Activiti/modules/activiti-engine/src/main/java/org/activiti/engine/impl/asyncexecutor at master · Ac... 
Specifically you should look in AbstractAsyncJobExecutor.java

As for script tasks, it may be difficult to name the thread as sometimes the thread will be a servlet thread, sometimes it will be inherited from a previous job (sync continuation) and sometimes it will be picked up from the Job executor (async continuation).

Cheers,

Greg

View solution in original post

2 Replies
gdharley
Intermediate

Re: Setting a thread factory for async tasks

Jump to solution

This would require that you overload the Job Executor service classes.

You can find the necessary classes under here:
Activiti/modules/activiti-engine/src/main/java/org/activiti/engine/impl/asyncexecutor at master · Ac... 
Specifically you should look in AbstractAsyncJobExecutor.java

As for script tasks, it may be difficult to name the thread as sometimes the thread will be a servlet thread, sometimes it will be inherited from a previous job (sync continuation) and sometimes it will be picked up from the Job executor (async continuation).

Cheers,

Greg

boris-petrov
Member II

Re: Setting a thread factory for async tasks

Jump to solution

Thanks for the response! This is what I did:

AbstractAsyncJobExecutor asyncExecutor = new DefaultAsyncJobExecutor();
asyncExecutor.setExecuteAsyncRunnableFactory(MyExecuteAsyncRunnable::new);
processEngineConfiguration.setAsyncExecutor(asyncExecutor);

...

class MyExecuteAsyncRunnable extends ExecuteAsyncRunnable {
     public MyExecuteAsyncRunnable(JobEntity job, CommandExecutor commandExecutor) {
          super(job, commandExecutor);
     }

     @Override
     public void run() {
          Thread.currentThread().setName("asd");
          super.run();
     }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

It seems to work correctly - does it look fine though? I don't like that I've hardcoded setting the async executor to be a DefaultAsyncJobExecutor but unfortunately processEngineConfiguration.getAsyncExecutor() returns null. Also, I'm extending the ExecuteAsyncRunnable class while there are others that I could extend - e.g. TenantAwareExecuteAsyncRunnable. Is that also correct? Is there a better way of doing all of those things?

As for the script tasks - you have no ideas how to make sure that all script tasks are executed in a specific thread?

Thanks again!