I got things to work, but it seems sub-optimal. Here are more details:
We're using Activiti in a multi-tenant environment where multiple process engines are running in a single app server, each talking to a separate database account.
This is accomplished by calling setDataSource() with a tenant-specific data source on the ProcessEngineConfiguration, prior to calling buildProcessEngine() against it to create the new process engine. This seems to work fine - each engine talks to a separate database for all of it's operations.
However, when job executor activation was enabled, it doesn't appear to use the data source set for the process engine. It appears to be using a data source configured in Spring (which doesn't work in our case, as it doesn't know which tenant it's working with).
Here's how I finally got things to work (The approach was taken based on an examination of the org.activiti.engine.impl.jobexecutor.JobExecutor code - start() method in particular):
- Created a new class that extends JobAcquisitionThread. This class re-implements the run() method to set up the correct data source and then call super.run(). This class is instantiated with an externally-created JobExecutor, which is then passed to the ProcessEngineConfiguration via a setJobExecutor() call. This allowed the available-jobs-query to query against the correct database.
- Create a new class that extends ThreadPoolExecutor that implements beforeExecute() and afterExecute() to set up and tear-down the appropriate data source prior to actual job execution. An instance of this class was set on the JobExecutor instance described above via a call to setThreadPoolExecutor().
Once this was done, the background threads started using the correct data source.
In our non-Activiti code, we support multi-tenancy by having a Spring-configured custom DataSource that is implemented via a map of database-specific data sources, keyed by tenant-id. this tenant-id is set as a thread local variable when a request comes in from a specific tenant, allowing all subsequent code to get a data source for the correct database without having to explicitly specify the tenant identifier. The background job processing threads appeared to be using this data source, but the tenant-id thread local variable wasn't set in this case (obviously), so the job threads were getting the incorrect data source.
Ideally - unless i'm misunderstanding something - all Activiti code would use any data source set on the configuration object used to contruct the engine.
Hope this helps.
BWD