Flowable整合Springboot

Flowable整合Springboot,第1张

1.添加相关依赖
<dependency>
    <groupId>org.flowablegroupId>
    <artifactId>flowable-spring-boot-starterartifactId>
    <version>6.6.0version>
dependency>
<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>1.1.14version>
dependency>
<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.21version>
dependency>
<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.13.2version>
    <scope>testscope>
dependency>
2.添加对应的配置
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://10.9.31.40:13306/flowable_learn?charset=utf8mb4&useSSL=false&nullCatalogMeansCurrent=true
    username: root
    password: 123456
    hikari:
      minimum-idle: 5
      idle-timeout: 600000
      maximum-pool-size: 10
      auto-commit: true
      pool-name: MyHikariCP
      max-lifetime: 1800000
      connection-timeout: 30000
      connection-test-query: SELECT 1
flowable:
  #关闭定时任务JOB
  async-executor-activate: false 
  #将databaseSchemaUpdate设置为true。当Flowable发现库与数据库表结构不一致时,会自动将数据库表结构升级至新版本。
  database-schema-update: true
server:
  port: 8082

系统启动的时候检查如果数据库对应的表结构没有创建,会帮助我们先创建对应的表结构

3.案例应用 3.1 流程部署
  • processes目录下的任何BPMN 2.0流程定义都会被自动部署。创建processes目录,并在其中创建示例流程定义(命名为one-task-process.bpmn20.xml)。
  • cases目录下的任何CMMN 1.1定义都会被自动部署。
  • forms目录下的任何Form定义都会被自动部署。

通过手动方式来部署

resources/process目录下创建流程定义文件


<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
             xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
             xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
             xmlns:flowable="http://flowable.org/bpmn"
             typeLanguage="http://www.w3.org/2001/XMLSchema"
             expressionLanguage="http://www.w3.org/1999/XPath"
             targetNamespace="http://www.flowable.org/processdef">

    <process id="holidayRequest" name="Holiday Request" isExecutable="true">

        <startEvent id="startEvent"/>
        <sequenceFlow sourceRef="startEvent" targetRef="approveTask"/>

        <userTask id="approveTask" name="审批任务" flowable:assignee="小红同学"/>
        <sequenceFlow sourceRef="approveTask" targetRef="decision"/>

        <exclusiveGateway id="decision"/>
        <sequenceFlow sourceRef="decision" targetRef="externalSystemCall">
            <conditionExpression xsi:type="tFormalExpression">
                
            conditionExpression>
        sequenceFlow>
        <sequenceFlow  sourceRef="decision" targetRef="sendRejectionMail">
            <conditionExpression xsi:type="tFormalExpression">
                
            conditionExpression>
        sequenceFlow>

        <serviceTask id="externalSystemCall" name="Enter holidays in external system"
                     flowable:class="org.flowable.CallExternalSystemDelegate"/>
        <sequenceFlow sourceRef="externalSystemCall" targetRef="holidayApprovedTask"/>

        <userTask id="holidayApprovedTask" name="Holiday approved"/>
        <sequenceFlow sourceRef="holidayApprovedTask" targetRef="approveEnd"/>

        <serviceTask id="sendRejectionMail" name="Send out rejection email"
                     flowable:class="com.example.flowable.SendRejectionMailHandler"/>
        <sequenceFlow sourceRef="sendRejectionMail" targetRef="rejectEnd"/>

        <endEvent id="approveEnd"/>

        <endEvent id="rejectEnd"/>

    process>

definitions>

编写拒绝策略处理器

public class SendRejectionMailHandler implements JavaDelegate {
    @Override
    public void execute(DelegateExecution delegateExecution) {
        System.out.println("===========================================================");
        System.out.println("请假请求被拒绝!!!");
        System.out.println("===========================================================");
    }
}
@SpringBootTest
class SpringbootApplicationTests {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private RuntimeService runtimeService;

    /**
     * Test deployment
     * 注:因为Springboot会自动扫描processes目录下bpmn2.0定义并自动创建,所以手动部署的时候需要更换resources下的目录,避免创建失败
     */
    @Test
    void testDeployment() {
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("process/one-task-process.bpmn20.xml")
                .name("请假流程")
                .deploy();
        System.out.println("deploy.getId() = " + deployment.getId());
        System.out.println("deploy.getName() = " + deployment.getName());
    }

}
3.2 启动流程

  启动流程和前面介绍的就没什么差异了,通过RuntimeService来实现

/**
 * Test start process
 */
@Test
void startProcess() {
    Map<String, Object> var = new HashMap();
    var.put("assignee-1", "小明同学");
    var.put("assignee-2", "小红同学");
    runtimeService.startProcessInstanceById("holidayRequest:6:c9fa93b2-d19f-11ec-a572-00e04c2fc932", var);
}
3.3 处理流程

  处理流程和前面介绍的也一样,通过TaskService来处理

/**
 * Test list task
 */
@Test
void listTask() {
    List<Task> list = taskService.createTaskQuery()
        .processDefinitionId("holidayRequest:6:c9fa93b2-d19f-11ec-a572-00e04c2fc932")
        .taskAssignee("小红同学").list();
    for (Task task : list) {
        System.out.println("task.getProcessDefinitionId() = " + task.getProcessDefinitionId());
        System.out.println("task.getId() = " + task.getId());
        System.out.println("task.getAssignee() = " + task.getAssignee());
        System.out.println("task.getName() = " + task.getName());
    }
}

/**
 * Test start task
 */
@Test
void startTask() {
    Task task = taskService.createTaskQuery()
        .processDefinitionId("holidayRequest:6:c9fa93b2-d19f-11ec-a572-00e04c2fc932")
        .taskAssignee("小红同学")
        .singleResult();
    // 添加流程变量
    Map<String, Object> variables = new HashMap<>();
    variables.put("approved", false);
    // 完成任务
    if (Objects.nonNull(task)) {
        taskService.complete(task.getId(), variables);
        System.out.println("Complete...");
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/916628.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存