How to intercept a Job Handling?

How can I hook in to the handling of jobs?

We run in a multi tenant environment and when a job is handled and some of our own java code is called, I need to set the tenant Id from the linked ProcessEngine, in a Context that is used by our own java code. But how?

@edbras

There is an execute(Callable callable) method in the AbstractProcessApplication class that you can override to apply the tenant context to the running invocation thread.

There is an execute(Callable callable) method in the AbstractProcessApplication class that you can override to apply the tenant context to the running invocation thread.

Thanks, but I do not think so.
When is this method exactly called? If I set a breakpoint in running a unit test that will run some process, it is never called.
Also, looking at the arguments, I don’t have any information to determine the tenant id.

What I have build now:
I created a TenantCommandInterceptor that extends from the CommandInterceptor and is added as Post CommandInterceptor such that the Context is filled. As such, I can retrieve the Spring ProcessEngineConfig that contains the deployed tenant id, that is set during creation of the process engine. I remove the tenant id after the command did run. It’s a bit like how Spring wraps a command in a db transaction.

I am writing a unit test as we speak, but it seems to work fine.

AFAIK, this is invoked if the process definitions are deployed as a process application. This is typically not the case for unit tests, because the test rule deploys the process definitions directly.

You don’t need the arguments to get to the tenant information. It’s a thread local implementation which you can access statically as follows: Context.getCoreExecutionContext().getExecution().getTenantId()

The CommandInterceptors I cannot speak for, since I haven’t used that before.

Thanks for the suggestion.
However, it works well with the CommandInterceptor and because of the position of the interceptor, just before any db tx occurs, I can ensure the tenant Id is available. Works like a charm :slight_smile: