springboot整合activiti

springboot整合activiti

起男 149 2023-05-09

springboot整合activiti

导入依赖

        <!-- 引入Activiti7 -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter</artifactId>
            <version>7.1.0.M4</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.activiti.dependencies</groupId>
            <artifactId>activiti-dependencies</artifactId>
            <version>7.0.0.Beta2</version>
            <type>pom</type>
        </dependency>
        <!-- 生成流程图 -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-image-generator</artifactId>
            <version>7.0.0.Beta2</version>
        </dependency>

修改配置文件

spring:
  #数据库配置
  datasource:
    driver-class-name: 
    url: 
    username: 
    password: 
  # activiti7配置
  activiti:
    # 自动部署验证设置:true-开启(默认)、false-关闭
    check-process-definitions: false
    # 开启历史表 默认不生成历史信息表
    db-history-used: true
    # 记录历史等级
    # none:不保存任何的历史数据,在流程执行过程中这是最高效的
    # activity:级别高于none,保存流程实例与流程行为,其他数据不存
    # audit:默认值,除了activity以外,还会保存全部的流程任务及其属性
    # full:保存历史数据的最高级别,除了会保存audit以外,还会保存其他全部流程相关的细节数据,如一些流程参数等
    history-level: full
    # 关闭自动部署
    deployment-mode: never-fail
    # 对数据库中所有表进行更新操作,如果表不存在,则自动创建
    # true启动时会对数据库中所有表做更新操作,如果表不存在,则自动创建
    # false默认值,启动时,对比数据库中表的版本,如果没有或不匹配则抛出异常
    # create_drop:启动时创建表,在关闭时删除表(必须手动关闭引擎,才能删除表)
    # drop-create:启动时删除原来的旧表,然后在创建新表(不需要手动关闭引擎)
    database-schema-update: true
    # 解决频繁查询SQL问题
    async-executor-activate: false

编辑bpmn文件

默认在classpath下的processes目录下的bpmn和bpmn20.xml文件定义的流程会在启动时自动加载

idea编辑bpmn文件需要安装插件actiBPM

网关

activiti中提供了四种网关

  • 排他网关ExclusiveGateway:用来在流程中实现决策。当流程执行到这个网关,所有分支都会判断条件是否为true,如果为true则执行该分支

    注意:排他网关只会选择一个为true的分支执行。如果多个分支都为true,则会选择id值小的执行

  • 并行网关ParallelGateway:允许将流程分成多个分支,也可以把多个分支汇聚到一起,并行网关的功能是基于进入和外出顺序流的

    注意:并行网关不会解析条件,即使顺序流中定义了条件,也会被忽略

  • 包含网关InclusiveGateway:可以看作是排他网关和并行网关的合体

  • 事件网关EventGateway:允许根据事件来判断流向。网关的美国外出顺序流都要链接到一个中间捕获事件。当流程达到一个基于事件网关,网关会尽等待状态:暂停执行,与此同时,会为每个外出顺序流创建相对应的事件订阅

UEL表达式

  • 表达式以${开始;}结束,列如:$
  • 支持逻辑运算:$
  • 支持变量(变量名必须小写)与实体类赋值

操作

流程部署类操作

    @Autowired
    private RepositoryService repositoryService;

    //流程部署
    @Test
    void test01(){
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("processes/demo.bpmn")
                .name("test01")
                .deploy();
        System.out.println(deployment.getName());
        System.out.println(deployment.getKey());
    }

	//流程部署列表
    @Test
    void test02(){
        List<ProcessDefinition> list = repositoryService
                .createProcessDefinitionQuery().list();

        list.forEach(processDefinition -> {
            System.out.println(processDefinition.getId());
            System.out.println(processDefinition.getName());
            System.out.println(processDefinition.getKey());
            System.out.println(processDefinition.getVersion());
        });
    }

    //删除流程部署
    @Test
    void test03(){
        String id = "ee68b8df-e445-11ed-b471-ecd68ac1a461";
        //不能删除还有实例的流程,true强制删除开启级联
        repositoryService.deleteDeployment(id,true);

    }

流程实例类操作

    @Autowired
    private RuntimeService runtimeService;

    //初始化流程实例
    @Test
    void test01(){
        String processKey = "demo01";//流程定义的key
        String businessKey = "123";//activiti扩展,业务key
        HashMap<String, Object> hashMap = new HashMap<>();//流程变量,可以是字面量也可以是引用类型,在bpmn文件中使用${xxx}或${xxx.xxx}获取

        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey(processKey, businessKey, hashMap);

        System.out.println("流程实例id:"+processInstance.getProcessInstanceId());
    }
    //查询流程实例
    @Test
    void test02(){
        String instanceId = "4b0f2ca3-e447-11ed-845e-ecd68ac1a461";
        ProcessInstance processInstance = runtimeService
                .createProcessInstanceQuery()
                .processInstanceId(instanceId)
                .singleResult();
        System.out.println(processInstance.getProcessInstanceId());
        System.out.println(processInstance.getProcessDefinitionId());
        System.out.println(processInstance.isEnded());
        System.out.println(processInstance.isSuspended());
    }
    //流程实例列表
    @Test
    void test03(){
        List<ProcessInstance> list = runtimeService
                .createProcessInstanceQuery()
                //.processDefinitionKey("demo01")//查看指定流程定义的流程实例
                .list();

        list.forEach(processInstance -> {
            System.out.println(processInstance.getProcessInstanceId());
            System.out.println(processInstance.getProcessDefinitionId());
        });
    }

    //挂起流程实例
    @Test
    void test04(){
        String instanceId = "4b0f2ca3-e447-11ed-845e-ecd68ac1a461";
        runtimeService.suspendProcessInstanceById(instanceId);
    }
    //激活流程实例
    @Test
    void test05(){
        String instanceId = "4b0f2ca3-e447-11ed-845e-ecd68ac1a461";
        runtimeService.activateProcessInstanceById(instanceId);
    }
    //删除流程实例
    @Test
    void test06(){
        String instanceId = "4b0f2ca3-e447-11ed-845e-ecd68ac1a461";
        String reason = "删除原因";
        runtimeService.deleteProcessInstance(instanceId,reason);
    }

任务类操作

    @Autowired
    private TaskService taskService;

    //查询任务列表
    @Test
    void test01(){
        List<Task> list = taskService.createTaskQuery().list();
        list.forEach(task -> {
            System.out.println(task.getId());
            System.out.println(task.getName());
            System.out.println(task.getAssignee());
        });
    }
    //查询我的待办
    @Test
    void test02(){
        String assignee = "zhangsan";

        List<Task> list = taskService.createTaskQuery()
                .taskAssignee(assignee)
                .list();
        list.forEach(task -> {
            System.out.println(task.getId());
            System.out.println(task.getName());
            System.out.println(task.getAssignee());
        });
    }

    //完成任务
    @Test
    void test03(){
        String taskId = "4b1212d7-e447-11ed-845e-ecd68ac1a461";
        Map<String,Object> variables = new HashMap<>();
        taskService.complete(taskId,variables);
    }

    //拾取任务
    @Test
    void test04(){
        String taskId = "09cb1487-e44a-11ed-9e85-ecd68ac1a461";
        String userId = "abc";

        taskService.claim(taskId,userId);
    }
    //归还任务
    @Test
    void test05(){
        String taskId = "09cb1487-e44a-11ed-9e85-ecd68ac1a461";

        taskService.unclaim(taskId);
    }
    //交办任务
    @Test
    void test06(){
        String taskId = "09cb1487-e44a-11ed-9e85-ecd68ac1a461";
        String userId = "lisi";
        taskService.setAssignee(taskId,userId);
    }
	//修改变量
	@Test
    void test07(){
        taskService.setVariable("09cb1487-e44a-11ed-9e85-ecd68ac1a461","k1","v1");
    }

查看历史类操作

    @Autowired
    private HistoryService historyService;
	//根据受理人查询
    @Test
    void test01(){
        String assignee = "zhangsan";

        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                .orderByHistoricTaskInstanceEndTime()
                .asc()
                .taskAssignee(assignee)
                .list();
        list.forEach(historicTaskInstance -> {
            System.out.println("id:"+historicTaskInstance.getId());
            System.out.println("instanceId:"+historicTaskInstance.getProcessInstanceId());
            System.out.println("name:"+historicTaskInstance.getName());
        });
    }
	//根据流程实例查询
    @Test
    void test02(){
        String instanceId = "4b0f2ca3-e447-11ed-845e-ecd68ac1a461";
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                .orderByHistoricTaskInstanceEndTime()
                .asc()
                .processInstanceId(instanceId)
                .list();
        list.forEach(historicTaskInstance -> {
            System.out.println("id:"+historicTaskInstance.getId());
            System.out.println("instanceId:"+historicTaskInstance.getProcessInstanceId());
            System.out.println("name:"+historicTaskInstance.getName());
        });
    }