使用 Activiti 引擎在集群应用程序上执行异步服务任务

Async service tasks on clustered applications with Activiti engine

我在同一个数据库上有 2 个应用程序 运行ning。 他们俩都这样启动activiti进程:

for (i = 0; i < msgNbr; i++) {
    Map<String, Object> dataMap = Data.prepareData(testOptions);
    runtimeService.startProcessInstanceByKey("asyncTransferProcess", dataMap);
}

正在启动的进程中的第一个服务任务是异步的:

<serviceTask id="serviceTask1" name="ServiceTask1" activiti:exclusive="true"
    activiti:class="com.test.activiti.async.ServiceTask1"></serviceTask>

异步执行器配置:

<property name="asyncExecutor" ref="asyncExecutor" />
<property name="asyncExecutorEnabled" value="true" />
<property name="asyncExecutorActivate" value="true" />

<bean id="asyncExecutor" class="org.activiti.engine.impl.asyncexecutor.DefaultAsyncJobExecutor">
    <property name="corePoolSize" value="20" />
    <property name="maxPoolSize" value="50" />
    <property name="keepAliveTime" value="3000" />
    <property name="queueSize" value="200" />
    <property name="maxTimerJobsPerAcquisition" value="2" />
    <property name="maxAsyncJobsDuePerAcquisition" value="2" />
    <property name="defaultAsyncJobAcquireWaitTimeInMillis" value="1000" />
    <property name="defaultTimerJobAcquireWaitTimeInMillis" value="1000" />
    <property name="timerLockTimeInMillis" value="60000" />
    <property name="asyncJobLockTimeInMillis" value="60000" />
</bean>

问题是当应用程序尝试 运行 进程时我得到 ActivitiOptimisticLockingException 和 NullPointerException 导致应用程序可能尝试 运行 相同的工作。如果不注意异常,应用程序可以正常工作,但我想知道是否有任何提示如何 运行 在同一数据库上使用异步进程的多个应用程序,并且可能有一种方法可以使应用程序 运行 只有自己的工作?

您需要做的第一件事是启用强 UUID。这是通过将以下内容添加到您的 Activiti 配置中来启用的 class:

StrongUuidGenerator idGenerator = new StrongUuidGenerator(); processEngineConfiguration.setIdGenerator(idGenerator);

为什么我认为这会有所帮助?因为您遇到的乐观锁很可能来自于从数据库中检索下一个 ID。 Strong UUID 生成器不接触数据库,因此更适合大型应用程序。

希望对您有所帮助。