在Activiti 6中,可以使用以下代码示例为多实例子任务或多实例子流程添加新的任务实例:
CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor();
commandExecutor.execute(new Command() {
public Void execute(CommandContext commandContext) {
ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(executionId);
ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
ActivityInstance activityInstance = commandContext.getActivityInstanceEntityManager().findActivityInstanceByExecutionId(executionId);
// 获取当前子任务的实例数量
int nrOfInstances = activityInstance.getNrOfInstances();
// 创建新的子任务实例
ExecutionEntity newExecution = executionEntityManager.createChildExecution(executionEntity.getParent());
newExecution.setCurrentFlowElement(executionEntity.getCurrentFlowElement());
newExecution.setActive(true);
newExecution.setConcurrent(true);
newExecution.setScope(false);
newExecution.setVariables(executionEntity.getVariables());
// 更新多实例子任务的实例数量
activityInstance.setNrOfInstances(nrOfInstances + 1);
return null;
}
});
CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor();
commandExecutor.execute(new Command() {
public Void execute(CommandContext commandContext) {
ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(executionId);
ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
ActivityInstance activityInstance = commandContext.getActivityInstanceEntityManager().findActivityInstanceByExecutionId(executionId);
// 获取当前子流程的实例数量
int nrOfInstances = activityInstance.getNrOfInstances();
// 创建新的子流程实例
ExecutionEntity newExecution = executionEntityManager.createChildExecution(executionEntity.getParent());
newExecution.setCurrentFlowElement(executionEntity.getCurrentFlowElement());
newExecution.setActive(true);
newExecution.setConcurrent(true);
newExecution.setScope(false);
newExecution.setVariables(executionEntity.getVariables());
// 更新多实例子流程的实例数量
activityInstance.setNrOfInstances(nrOfInstances + 1);
return null;
}
});
以上示例代码中,executionId
是正在运行的多实例子任务或多实例子流程的执行ID。通过执行上述代码,可以在运行时为多实例子任务或多实例子流程添加新的任务实例。