Spring Boot 应用集成 Activiti 工作流引擎

将 Activiti 工作流引擎集成到 Spring Boot 应用程序中,一般需要以下几个步骤:

在 pom.xml 文件中加入以下依赖


  org.activiti
  activiti-spring-boot-starter-basic
  ${activiti.version}



  org.mybatis.spring.boot
  mybatis-spring-boot-starter
  ${mybatis.version}


配置数据源。例如:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
    username: root
    password: 123456

配置 Activiti,例如:

activiti:
  database-schema-update: true
  database-reduce-time-to-live: true
  async-executor-activate: true
  history-level: full
  check-process-definitions: true
  process-definitions:
    deploy-resources: classpath*:/processes/*.bpmn20.xml
    allow-multiple: true
  id-generator: org.activiti.engine.impl.persistence.StrongUuidGenerator
  jdbc-batch-processing: true
  job-execution:
    activate: true
    max-jobs-per-acquisition: 5
    wait-time-in-millis: 5000
  mail:
    enabled: false

载入 BPMN 文件并部署流程定义,例如:

  @PostConstruct
  private void initProcessDefinitions() {
    final Set deploymentResourceNames = resourceResolver.getResourcesAsStream("/processes/*.bpmn20.xml")
                                          .map(resource -> resource.getFilename())
                                          .collect(Collectors.toSet());
    deploymentResourceNames.forEach(resourceName -> {
      final String processDefinitionId = repositoryService.createDeployment()
                    .addClasspathResource("processes/" + resourceName)
                    .name(resourceName)
                    .deploy()
                    .getId();
      log.info("ProcessDefinition <{}> deployed with id: <{}>", resourceName, processDefinitionId);
    });
  }

编写 Activiti 相关的服务代码,例如:

@Service
public class MyWorkflowService {
  private final ProcessEngine processEngine;

  @Autowired
  public MyWorkflowService(final ProcessEngine processEngine) {
    this.processEngine = processEngine;
  }

  public Execution startWorkflow() {
    return processEngine.getRuntimeService().startProcessInstanceByKey("myProcessKey");
  }

  public List getTasksByUser(final String user) {
    return processEngine.getTaskService().createTaskQuery()
              .taskAssignee(user)
              .list();
  }

  public void completeTask(final String taskId) {
    processEngine.getTaskService().complete(taskId);
  }
}

Activiti 更适合于较大的、复杂的商业流程。

在 Activiti 工作流引擎中,驳回一个任务的具体实现方式可能会有所不同,但是一般可以使用以下两种方案:

历史任务重启:将当前任务的执行历史,重新导入到流程实例中,从而重新开始流程。可以通过如下代码实现:

TaskEntity currentTask = (TaskEntity) taskService.createTaskQuery()
                            .taskId(taskId)
                            .singleResult();
String processInstanceId = currentTask.getProcessInstanceId();
HistoricTaskInstanceEntity historicTask = (HistoricTaskInstanceEntity) historyService.createHistoricTaskInstanceQuery()
                                                        .processInstanceId(processInstanceId)
                                                        .taskId(taskId)
                                                        .singleResult();
List toRollbackTasks = historyService.createHistoricTaskInstanceQuery()
                                                      .processInstanceId(processInstanceId)
                                                      .taskDefinitionKey(historicTask.getTaskDefinitionKey())
                                                      .orderByTaskCreateTime()
                                                      .asc()
                                                      .list();
String activityId = toRollbackTasks.get(0).getTaskDefinitionKey();
runtimeService.createProcessInstanceModification(processInstanceId)
              .cancelAllForActivity(activityId)
              .startBeforeActivity(activityId)
              .processInstanceVariable("taskRejectReason", rejectReason)
              .execute(true, true);

转移至上一个任务节点:将当前任务转移到上一个任务节点,并且设置当前任务的审批状态为驳回。可以通过如下代码实现:

Task currentTask = taskService.createTaskQuery()
                         .taskId(taskId)
                         .singleResult();
String currentActivityId = currentTask.getTaskDefinitionKey();
List historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
                                                            .processInstanceId(currentTask.getProcessInstanceId())
                                                            .activityType("userTask")
                                                            .finished()
                                                            .orderByHistoricActivityInstanceEndTime()
                                                            .desc()
                                                            .list();
if (historicActivityInstances.size() > 1) {
  String previousActivityId = historicActivityInstances.get(0).getActivityId();
  taskService.addComment(taskId, processInsId, comment);
  taskService.complete(taskId, Map.of("status", "rejected", "destination", previousActivityId));
}

需要注意的是,这里提供的代码只是示例代码,具体实现流程和细节可能会有所不同。开发人员需要根据自己的实际情况进行相应的修改和调整。同时需要注意的是,驳回操作可能会影响工作流的正常流转,因此需要在实际操作中谨慎使用。

你可能感兴趣的:(springboot,activity,activity,spring,boot)