Activiti6.0(四)核心Api

流程引擎及服务

  • RepositoryService 流程仓库Service,可以管理流程仓库例如部署删除读取流程资源
  • RuntimeService 运行时Service可以处理所有运行状态的流程实例流程控制(开始,暂停,挂起等)
  • TaskService 任务Service用于管理、查询任务,例如签收、办理、指派等
  • IdentitiServicec 身份Service可以管理查询用户、组之间的关系
  • FormService 表单Service用于读取和流程、任务相关的表单数据
  • HistoryService 历史Service用于查询所有的历史数据
  • ManagementService 引擎管理Service,和具体业务无关,主要查询引擎配置,数据库作业
  • DynamicBpmService 动态bpm服务

流程存储服务

RepositoryService

  • 管理流程定义文件xml及静态资源服务
  • 对流程定义文件对暂停激活
  • 流程定义启动权限管理
  • 部署文件构造器DeploymentBuilder
  • 部署文件查询器DeploymentQuery
  • 流程定义文件查询对象ProcessDefinitionQuery

API文档

序号 方法 含义 描述 库表字段
1 repositoryService.createDeployment().addClasspathResource("参数") .deploy() 部署流程 resources文件下面的xml流程文件 省略
2 repositoryService.createDeploymentQuery().list() 查询所有部署 省略 省略
3 repositoryService.createProcessDefinitionQuery().list() 查询所有部署流程 省略 省略
4 repositoryService.suspendProcessDefinitionById(id)或ByKey 挂起流程 根据流程id挂起流程 修改表ACT_RE_PROCDEF字段SUSPENSION_STATE_:1激活 2挂起
5 repositoryService.activateProcessDefinitionById(id)或ByKey 启动流程 根据流程id激活流程 修改表ACT_RE_PROCDEF字段SUSPENSION_STATE_:1激活 2挂起
6 repositoryService.addCandidateStarterUser(流程id,用户id) 流程与用户对应关系 添加流程与用户关系 操作ACT_RU_IDENTITYLINK表
7 repositoryService.deleteCandidateStarterGroup(流程id,用户组id) 流程与用户组对应关系 添加流程与用户组关系 操作ACT_RU_IDENTITYLINK表
8 repositoryService.deleteCandidateStarterUser(流程id,用户id) 流程与用户对应关系 删除流程与用户关系 操作ACT_RU_IDENTITYLINK表
9 repositoryService.deleteCandidateStarterGroup(流程id,用户组id) 流程与用户对应关系 删除流程与用户组关系 操作ACT_RU_IDENTITYLINK表
10 repositoryService.getIdentityLinksForProcessDefinition(流程id) 查询流程对应关系 查询流程对应用户跟组关系 查询ACT_RU_IDENTITYLINK表

测试代码

package com.guosh.activiti.coreapi;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.DeploymentQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/**
 * 流程存储服务
 * @Author: Guosh
 * @Date: 2019-07-19 11:33
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:activiti-context.xml"})
public class RepostoryServiceTest {
    private static final Logger logger = LoggerFactory.getLogger(RepostoryServiceTest.class);

    @Rule
    @Autowired
    public ActivitiRule activitiRule;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Test
    public void testRepository(){


        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();

        //classpath方式
        Deployment deploy = deploymentBuilder.name("测试部署资源1")
                .addClasspathResource("my-process.bpmn20.xml")
                .addClasspathResource("second_approve.bpmn20.xml")
                .deploy();



        //多次部署
        //classpath方式
        DeploymentBuilder deploymentBuilder2 = repositoryService.createDeployment();

        Deployment deploy2 = deploymentBuilder2.name("测试部署资源2")
                .addClasspathResource("my-process.bpmn20.xml")
                .addClasspathResource("second_approve.bpmn20.xml")
                .deploy();


        //查询部署
        List deployments = repositoryService.createDeploymentQuery()
                .orderByDeploymenTime().asc()
                .list();

        for (Deployment deployment:deployments){
            logger.info("deployment = {}",deployment);
        }
        logger.info("deployments.size = {}",deployments.size());


        //查询已经部署流程
        List processDefinitions = repositoryService
                .createProcessDefinitionQuery()
                .list();
        for (ProcessDefinition processDefinition:processDefinitions) {
            logger.info("processDefinition = {}, version = {}, key = {}, id = {}",
                    processDefinition,
                    processDefinition.getVersion(),
                    processDefinition.getKey(),
                    processDefinition.getId());
        }


    }
    //测试流程挂起启动
    @Test
    public void testSuspend(){
        //流程挂起
        repositoryService.suspendProcessDefinitionById("my-process:1:7506");

        //如果流程挂起启动流程会报错
        try{
            logger.info("启动流程");
            runtimeService.startProcessInstanceById("my-process:1:7506");
            logger.info("启动成功");
        }catch (Exception e){
            logger.info("启动失败");
            logger.info(e.getMessage(),e);
        }


        //流程激活
        repositoryService.activateProcessDefinitionById("my-process:1:7506");
        logger.info("启动流程");
        runtimeService.startProcessInstanceById("my-process:1:7506");
        logger.info("启动成功");


    }


    //测试指定用户或者用户组与流程的关系
    @Test
    public void testCandidateStarter(){

        //给流程指定用户参数流程id与用户id
        repositoryService.addCandidateStarterUser("my-process:1:7506","user");

        //给流程指定用户组
        repositoryService.addCandidateStarterGroup("my-process:1:7506","groupM");

        //查询流程对应关系的所有用户
        List identityLinks = repositoryService.getIdentityLinksForProcessDefinition("my-process:1:7506");

        for (IdentityLink identityLink:identityLinks) {
            logger.info("identityLink = {}",identityLink);
        }

        //删除关系
        repositoryService.deleteCandidateStarterUser("my-process:1:7506","user");
        repositoryService.deleteCandidateStarterGroup("my-process:1:7506","groupM");
    }
}

流程运行控制服务

RuntimeService

  • 启动流程及对流程数据对控制
  • 流程实例(ProcessInstance)与执行流(Execution)查询
  • 触发流程操作、接收消息和信号

RuntimeService启动流程变量管理

  • 启动流程的常用方式(id,key,message)
  • 启动流程可选参数(businesskey,variables,tenantId)
  • 变量(variables)的设置和获取

流程实例与执行流

  • 流程实例(ProcessInstance)表示一次工作流业务的数据实体
  • 执行流(Execution)表示流程实例中具体的执行路径
  • 流程实例接口继承与执行流

流程触发

  • 使用trigger触发ReceiveTask节点
  • 触发信号捕获事件signalEvenReceived
  • 触发消息捕获事件messageEventReceived
序号 方法 含义 描述
1 runtimeService.startProcessInstanceByKey(String processDefinitionKey, Map variables) 根据部署流程key启动一个流程 省略
2 runtimeService.startProcessInstanceById(String processDefinitionId, Map variables) 根据部署流程id启动一个流程 省略
3 runtimeService.createProcessInstanceBuilder().businessKey("businessKey001") .processDefinitionKey(String processDefinitionKey).variables( Map variables) .start() 根据processInstanceBuilder启动流程 省略
4 runtimeService.getVariables(processInstance.getId()) 根据流程实例id获取传参 省略
5 runtimeService.setVariable(processInstance.getId(),"key3","value3") 新增或修改参数 省略
6 runtimeService.createProcessInstanceQuery().processInstanceId(processInstance.getId()) 查询流程实例 根据流程id获取流程实例
7 runtimeService.createExecutionQuery() 获取流程执行对象 省略

测试代码

/**
 * 流程运行控制服务
 * @Author: Guosh
 * @Date: 2019-07-19 11:33
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:activiti-context.xml"})
public class RuntimeServiceTest {
    private static final Logger logger = LoggerFactory.getLogger(RuntimeServiceTest.class);

    @Rule
    @Autowired
    public ActivitiRule activitiRule;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    //部署流流程
    @Test
    public void testRepository(){
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        //classpath方式
        Deployment deploy = deploymentBuilder.name("测试部署资源1")
                .addClasspathResource("my-process-signal-received.bpmn20.xml")
                .deploy();
    }


    //根据key启动流程
    @Test
    public void testStartProcess(){
        Map variables=new HashMap();
        //传入参数
        variables.put("key1","value1");
        //启动流程根据key默认使用的流程的最新版本
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process", variables);
        logger.info("processInstance = {}",processInstance);
    }

    //根据id启动流程
    @Test
    public void testStartProcessById(){
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();

        Map variables=new HashMap();
        //传入参数
        variables.put("key1","value1");
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId(), variables);
        logger.info("processInstance = {}",processInstance);
    }


    //根据processInstanceBuilder启动流程
    @Test
    public void testProcessInstanceBuilder(){
        Map variables=new HashMap();
        //传入参数
        variables.put("key1","value1");
        //启动流程
        ProcessInstanceBuilder processInstanceBuilder = runtimeService.createProcessInstanceBuilder();
        ProcessInstance processInstance = processInstanceBuilder.businessKey("businessKey001")
                .processDefinitionKey("my-process")
                .variables(variables)
                .start();
        logger.info("processInstance = {}",processInstance);
    }

    //根据其流程实例id获取参数
    @Test
    public void testVariables(){
        Map variables=new HashMap();
        //传入参数
        variables.put("key1","value1");
        variables.put("key2","value2");
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process", variables);
        logger.info("processInstance = {}",processInstance);


        //新增一个参数
        runtimeService.setVariable(processInstance.getId(),"key3","value3");
        //修改一个参数
        runtimeService.setVariable(processInstance.getId(),"key2","value2_1");


        //获取流程实例传过来的参数
        Map variables1 = runtimeService.getVariables(processInstance.getId());


        logger.info("variables1 = {}",variables1);
    }





    //根据其流程实例id获取
    @Test
    public void testProcessInstanceQuery(){
        Map variables=new HashMap();
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process", variables);
        logger.info("processInstance = {}",processInstance);

        ProcessInstance processInstance1 = runtimeService.createProcessInstanceQuery()
                .processInstanceId(processInstance.getId()).singleResult();


    }


    //流程执行对象查询操作
    @Test
    public void testExecutionQuery(){
        Map variables=new HashMap();
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process", variables);
        logger.info("processInstance = {}",processInstance);

        //流程执行对象
        List executions = runtimeService.createExecutionQuery().list();
        for (Execution execution:executions){
            logger.info("execution = {}",execution);
        }


    }
}

流程触发

Activiti6.0(四)核心Api_第1张图片
image.png
    //流程触发
    @Test
    public void testTrigger(){
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process");
        logger.info("processInstance = {}",processInstance);
        //流程执行对象
        Execution executions = runtimeService.createExecutionQuery().activityId("someTask").singleResult();
        logger.info("executions = {}",executions);
        //触发执行
        runtimeService.trigger(executions.getId());
        executions = runtimeService.createExecutionQuery().activityId("someTask").singleResult();
        logger.info("executions = {}",executions);

    }

流程文件xml


    
        
        
                
        
        

        

    



信号捕获节点触发

Activiti6.0(四)核心Api_第2张图片
image.png
    //信号流程节点触发
    @Test
    public void testSignalEventReceived(){
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process");
        logger.info("processInstance = {}",processInstance);

        //查询触发信号
        Execution executions = runtimeService.createExecutionQuery().signalEventSubscriptionName("my-signal").singleResult();
        logger.info("executions = {}",executions);

        //触发执行
        runtimeService.signalEventReceived("my-signal");

    }


    
    

    

        
        
        
        
            
        
        

        

    



消息触发

Activiti6.0(四)核心Api_第3张图片
image.png

    //消息流程节点触发
    @Test
    public void testMessageEventReceived(){
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process");
        logger.info("processInstance = {}",processInstance);

        //查询触发信号
        Execution executions = runtimeService.createExecutionQuery().messageEventSubscriptionName("my-message").singleResult();
        logger.info("executions = {}",executions);

        //触发执行
        runtimeService.messageEventReceived("my-message",executions.getId());

    }


    
    

    

        
        
        
        
            
        
        

        

    



流程基于message启动

    @Test
    public void testMessageStart(){
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByMessage("my-message");
        logger.info("processInstance = {}",processInstance);
        
    }



    
    

    

        
            
        
        

        
        

        

    


任务管理服务

  • TaskService
  • 对用户任务(UserTask)管理和流程控制
  • 设置用户任务(UserTask)对权限信息(拥有者,候选人,办理人)
  • 针对用户任务添加任务附件、任务;评价和事件记录

  • TaskService对Task管理与流程控制
  • Task对象对创建,删除
  • 查询Task,并驱动Task节点完成执行
  • Task相关参数变量(variable)设置
序号 方法 含义 描述
1 taskService.createTaskQuery().list() 查询所有任务 省略
2 taskService.setVariable("任务id","键","值") 设置普通变量 省略
3 taskService.setVariableLocal("任务id","键","值") 设置本地变量 省略
4 taskService.getVariables("任务id") 获取普通变量 省略
5 taskService.getVariablesLocal(("任务id") 获取本地变量 省略
6 runtimeService.getVariables(task.getExecutionId()) 通过流获取变量 省略
7 taskService.complete("任务id","传值Map") 到下一个节点 省略
    @Test
    public void testTaskService(){
        Map variables=new HashMap();
        variables.put("message","my test message !!");
        //启动流程
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey("my-process",variables);

        //获取task
        Task task = taskService.createTaskQuery().singleResult();
        logger.info("task = {}",task.toString());
        //描述信息
        logger.info("task.description = {}",task.getDescription());
        //设置普通变量
        taskService.setVariable(task.getId(),"key1","value1");
        //设置本地变量
        taskService.setVariableLocal(task.getId(),"localkey1","localval1");



        //获取普通变量
        Map taskServiceVariables = taskService.getVariables(task.getId());

        //获取本地变量
        Map taskServiceVariablesLocal = taskService.getVariablesLocal(task.getId());
        //通过执行流获取
        Map variables1 = runtimeService.getVariables(task.getExecutionId());

        //{key1=value1, localkey1=localval1, message=my test message !!}
        logger.info("taskServiceVariables = {}",taskServiceVariables);
        //{localkey1=localval1}
        logger.info("taskServiceVariablesLocal = {}",taskServiceVariablesLocal);
        //{key1=value1, message=my test message !!}
        logger.info("variables1 = {}",variables1);

        Map completeVar=new HashMap();
        completeVar.put("ckey1","cvalue1");

        //执行下一个节点
        taskService.complete(task.getId(),completeVar);


  • TaskService设置Task权限信息
  • 候选用户(candidateUser)和候选组(candidateGroup)
  • 指定拥有人(Owner)和办理人(Assignee)
  • 通过claim设置办理人
序号 方法 含义 描述
1 taskService.setOwner("taskId","user") 设置流程发起人 省略
2 taskService.claim(""taskId"","user") 指定代办人 省略
3 taskService.addCandidateUser("user") 添加候选人 省略
4 taskService.addCandidateGroup("group") 添加候选组 省略
5 taskService.createTaskQuery().taskCandidateUser("user").taskUnassigned().list() 查询候选人列表有user但是没指定代办人任务 省略
6 taskService.createTaskQuery().taskCandidateUser("user").taskUnassigned().list() 查询候选人列表有我但是没指定代办人任务 省略
7 taskService.createTaskQuery().taskAssignee("user").list() 查询代办人为user的任务 省略
8 taskService.getIdentityLinksForTask("taskId") 查询任务与人员之间的关系 省略



    

        
        

        
        
            
            some task ${message}
        
        

        

    


    //用户权限测试
    @Test
    public void testTaskServiceUser(){
        Map variables=new HashMap();
        variables.put("message","my test message !!");
        //启动流程
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey("my-process",variables);

        //获取task
        Task task = taskService.createTaskQuery().singleResult();
        logger.info("task = {}",task.toString());
        //描述信息
        logger.info("task.description = {}",task.getDescription());
        //设置流程到发起人
        taskService.setOwner(task.getId(),"user1");
        //指定办理人
        //taskService.setAssignee(task.getId(),"jimmy");

        //查询候选人列表有我但是没指定代办人任务
        List taskList = taskService.createTaskQuery()
                .taskCandidateUser("jimmy")
                .taskUnassigned().list();
        //指定办理人claim会检查该任务是否已经被认领,如果被认领则会抛出ActivitiTaskAlreadyClaimedException
        for (Task task1:taskList){
            taskService.claim(task1.getId(),"jimmy");
        }

        //查询task与多少用户相关数据
        List identityLinksForTask = taskService.getIdentityLinksForTask(task.getId());
        for(IdentityLink identityLink:identityLinksForTask){
            logger.info("identityLink = {}",identityLink);
        }

        //查询代办人为jimmy的任务
        List taskList1 = taskService.createTaskQuery().taskAssignee("jimmy").list();
        for (Task task1:taskList1){
            Map completeVar=new HashMap();
            completeVar.put("ckey1","cvalue1");
            taskService.complete(task.getId(),completeVar);
        }

    }

  • TaskService设置Task附加信息
  • 任务附件(Attachment)创建与查询
  • 任务评价(Comment)创建与查询
序号 方法 含义 描述
1 taskService.createAttachment("类型","任务id","流程Id","附件名称","附件描述","流或者url) 上传附件 省略
2 taskService.getTaskAttachments("任务id") 上传附件 省略
3 taskService.addComment("任务id","流程id","批注1") 添加审批批注 省略
4 taskService.getTaskComments("任务id") 查询审批批注 省略
5 taskService.getTaskEvents("任务id") 查询任务日志记录 省略
    //文件附件测试
    @Test
    public void testTaskServiceAttachment(){
        Map variables=new HashMap();
        variables.put("message","my test message !!");
        //启动流程
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey("my-process",variables);

        //获取task
        Task task = taskService.createTaskQuery().singleResult();
        logger.info("task = {}",task.toString());
        //描述信息
        logger.info("task.description = {}",task.getDescription());

        //上传附件
        taskService.createAttachment("url",task.getId(),
                task.getProcessInstanceId(),"附件名称","附件描述","/url/test.png");

        //查询附件
        List taskAttachments = taskService.getTaskAttachments(task.getId());
        for (Attachment attachment:taskAttachments){
            logger.info("attachment = {}",attachment);
        }

    }


 //批注测试
 @Test
    public void testTaskServiceComment(){
        Map variables=new HashMap();
        variables.put("message","my test message !!");
        //启动流程
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey("my-process",variables);

        //获取task
        Task task = taskService.createTaskQuery().singleResult();
        logger.info("task = {}",task.toString());
        //描述信息
        logger.info("task.description = {}",task.getDescription());

        //添加审批批注
        taskService.addComment(task.getId(),task.getProcessInstanceId(),"recourd note 1");
        taskService.addComment(task.getId(),task.getProcessInstanceId(),"recourd note 2");
        //查询审批批注
        List taskComments = taskService.getTaskComments(task.getId());
        for (Comment comment:taskComments){
            logger.info("comment = {}",comment);
        }

        //查询所有task日志记录
        List taskEvents = taskService.getTaskEvents(task.getId());
        for (Event event:taskEvents){
            logger.info("event = {}",event);
        }

    }

身份管理服务

  • IdentityService
  • 管理用户(User)
  • 管理用户组(Group)
  • 用户与用户组关系(Membership)
序号 方法 含义 描述
1 identityService.newUser("userid") 创建一个用户 省略
2 identityService.newGroup("groupid") 创建一个组 省略
3 identityService.saveUser(user) 保存或者更新用户 省略
4 identityService.saveGroup(group) 保存或者更新组 省略
5 identityService.createUserQuery() 查询用户 省略
6 identityService.createGroupQuery() 查询组 省略
 @Test
    public void testIdentity(){
        //创建user
        User user1 = identityService.newUser("user1");
        //添加属性
        user1.setEmail("[email protected]");

        User user2 = identityService.newUser("user2");
        user2.setEmail("[email protected]");

        identityService.saveUser(user1);
        identityService.saveUser(user2);



        //创建group
        Group group1 = identityService.newGroup("group1");
        identityService.saveGroup(group1);

        Group group2 = identityService.newGroup("group2");
        identityService.saveGroup(group2);

        //创建之间关系userid与grupid
        identityService.createMembership("user1","group1");
        identityService.createMembership("user2","group1");
        identityService.createMembership("user1","group2");



        //查询group1下面的用户
        List userList = identityService.createUserQuery().memberOfGroup("group1").list();
        for (User user:userList) {
            logger.info("user = {}",user);
        }

        //查询user1所属的group
        List groupList = identityService.createGroupQuery().groupMember("user1").list();
        for (Group group:groupList) {
            logger.info("group = {}",group);
        }

    }

表单服务管理

  • FormService
  • 解析流程定义中表单项的配置
  • 提交表单的方式驱动用户节点流转
  • 获取自定义外部表单key

序号 方法 含义 描述
1 formService.getStartFormKey(processDefinition.getId()) 部署流程的id获取表单key 省略
2 formService.getStartFormData(processDefinition.getId()).getFormProperties() 获取开始节点表单内容 省略
3 formService.getStartFormData(processDefinition.getId()).getFormProperties() 获取开始节点表单内容 省略
4 formService.submitStartFormData(processDefinition.getId(), "传值参数") 通过formservice启动流程 省略
5 formService.submitTaskFormData("taskId","传参数") 通过formservice提交task表单 省略
6 formService.getTaskFormData("taskId") 通过taskid获取task节点表单内容 省略



    

        
            
                
            
        
        
        
        
            
                
            
        
        

        

    


 @Test
    public void testFormService(){
        //获取部署的流程
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();

        //获取startformkey
        String startFormKey = formService.getStartFormKey(processDefinition.getId());
        logger.info("startFormKey= {}",startFormKey);


        //获取表单内容
        StartFormData startFormData = formService.getStartFormData(processDefinition.getId());
        List formProperties = startFormData.getFormProperties();
        for (FormProperty formProperty:formProperties) {
            logger.info("formProperty= {}",formProperty);
        }

        //通过formservice启动流程
        Map properties=new HashMap();
        properties.put("message","my test message");
        ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(), properties);
        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();

        //查询task表单
        TaskFormData taskFormData = formService.getTaskFormData(task.getId());
        List formProperties1 = taskFormData.getFormProperties();
        for (FormProperty formProperty:formProperties1) {
            logger.info("formProperties1= {}",formProperty);
        }

        Map properties2 = new HashMap();
        properties2.put("yewORno","yes");
        //用formservice提交task
        formService.submitTaskFormData(task.getId(),properties2);

    }

历史管理服务

  • HistoryService
  • 管理流程实例结束后的历史数据
  • 构建历史数据查询对象
  • 根据流程实例id删除流程历史数据
Activiti6.0(四)核心Api_第4张图片
image.png
  • HistoryService构建历史查询对象
  • create[历史数据实体]Query
  • createNative[历史数据实体]Query | 通过原生sql查询
  • createProcessInstanceHistoryLogQuery | 查询一个流程实例的所有其他数据
  • HistoryService删除历史操作
  • deleteHistoricProcessInstance | 删除历史流程实例及联删除其他信息
  • deleteHistoricTaskInstance | 删除历史的task实例

序号 方法 含义 描述
1 historyService.createHistoricProcessInstanceQuery() 查询流程实例变量 省略
2 historyService.createHistoricActivityInstanceQuery() 查询活动节点 省略
3 historyService.createHistoricTaskInstanceQuery() 查询任务实例 省略
4 historyService.createHistoricVariableInstanceQuery() 查询流程任务变量 省略
5 historyService.createHistoricDetailQuery() 历史任务流程活动详细信息 省略
6 historyService.createProcessInstanceHistoryLogQuery("流程实例id") 查询一个流程实例的所有其他数据 省略
7 historyService.deleteHistoricProcessInstance("流程实例id") 删除历史流程实例 省略
8 historyService.deleteHistoricTaskInstance("taskid") 删除历史任务 省略

    @Test
    public void testHistory(){
        Map variables=new HashMap();
        //传入参数
        variables.put("key0","value0");
        variables.put("key1","value1");
        variables.put("key2","value2");

        Map transientVariables=new HashMap();
        //传入参数
        transientVariables.put("tkey1","tvalue1");


        //启动流程
        ProcessInstanceBuilder processInstanceBuilder = runtimeService.createProcessInstanceBuilder();
        ProcessInstance processInstance = processInstanceBuilder
                .processDefinitionKey("my-process")
                .variables(variables)
                //瞬时变量不会持久化到数据库
                .transientVariables(transientVariables)
                .start();
        logger.info("processInstance = {}",processInstance);

        //修改key1值
        runtimeService.setVariable(processInstance.getId(),"key1","value1_1");

        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();

        Map properties =new HashMap();
        properties.put("fkey1","fvalue1");
        properties.put("key2","value2_2");

        formService.submitTaskFormData(task.getId(),properties);


        //查询流程实例变量
        List historicProcessInstances = historyService.createHistoricProcessInstanceQuery().list();

        for (HistoricProcessInstance historicProcessInstance:historicProcessInstances) {
            logger.info("historicProcessInstance = {}",historicProcessInstance);
        }

        //查询活动节点对象
        List historicActivityInstances = historyService.createHistoricActivityInstanceQuery().list();
        for (HistoricActivityInstance historicActivityInstance:historicActivityInstances) {
            logger.info("historicActivityInstance = {}",historicActivityInstance);
        }

        //查询任务实例
        List historicTaskInstances = historyService.createHistoricTaskInstanceQuery().list();
        for (HistoricTaskInstance historicTaskInstance:historicTaskInstances) {
            logger.info("historicTaskInstance = {}",historicTaskInstance);

        }

        //查询流程任务变量值
        List historicVariableInstances = historyService.createHistoricVariableInstanceQuery().list();
        for (HistoricVariableInstance historicVariableInstance:historicVariableInstances) {
            logger.info("historicVariableInstance = {}",historicVariableInstance);

        }


        //历史任务流程活动详细信息
        List historicDetails = historyService.createHistoricDetailQuery().list();
        for (HistoricDetail historicDetail:historicDetails) {
            logger.info("historicDetail = {}",historicDetail);

        }

        //查询一个流程实例的所有其他数据
        ProcessInstanceHistoryLog processInstanceHistoryLog = historyService.createProcessInstanceHistoryLogQuery(processInstance.getId())
                //包含数据
                .includeVariables()
                .includeFormProperties()
                .includeComments()
                .includeTasks()
                .includeActivities()
                .includeVariableUpdates()
                .singleResult();
        List historicDatas = processInstanceHistoryLog.getHistoricData();

        for (HistoricData historicData:historicDatas) {
            logger.info("historicData = {}",historicData);

        }

        //删除流程实例id
        historyService.deleteHistoricProcessInstance(processInstance.getId());
        //确认是否删除
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();
        logger.info("historicProcessInstance = {}",historicProcessInstance);
    }

其他管理服务

  • 其他管理服务
  • 管理服务ManagementService
  • 动态流程定义服务DynamicBpmnService
  • ManagementService
  • job任务管理
  • 数据库相关通用操作
  • 执行流程引擎命令(Command)
  • Job任务查询
  • JobQuery 查询一般工作
  • TimerJobQuery 查询定时工作
  • SuspendedJobQuery 查询中断工作
  • DeadLetterJobQuery 查询无法执行的工作

序号 方法 含义 描述
1 managementService.createTimerJobQuery() 查询定时工作 省略
2 managementService.createJobQuery() 查询一般工作省略 省略
3 managementService.createSuspendedJobQuery() 查询中断工作 省略
4 managementService.createDeadLetterJobQuery() 查询无法执行的工作 省略

    @Test
    public void testJobQuery(){

        //工作任务查询
        List timerJobList = managementService.createTimerJobQuery().list();

        for (Job timerJob:timerJobList) {
            logger.info("timerJob={}",timerJob);
        }

        JobQuery jobQuery = managementService.createJobQuery();

        //中断的
        SuspendedJobQuery suspendedJobQuery = managementService.createSuspendedJobQuery();

        //不再执行
        DeadLetterJobQuery deadLetterJobQuery = managementService.createDeadLetterJobQuery();
    }


    

        
            

                R5/PT10S
            
        
        
        
        
        

        

    


  • 数据库相关操作
  • 查询表结构元数据(TableMetaData)
  • 通用查询(TablePageQuery)
  • 执行自定义Sql查询(executeCustomSql)

序号 方法 含义 描述
1 managementService.createTablePageQuery().tableName(managementService.getTableName(class)) 查询实体到所有数据 省略
2 managementService.executeCustomSql() 自定义sql查询 省略

public interface MyCustomMapper {
    @Select("SELECT * FROM ACT_RU_TASK")
    public List> findAll();
}

    @Test
    public void testCustSql(){
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process");

        //自定义sql语句
        List> mapList = managementService.executeCustomSql(new AbstractCustomSqlExecution>>(MyCustomMapper.class) {
            @Override
            public List> execute(MyCustomMapper o) {
                return o.findAll();
            }
        });

        for (Map stringObjectMap:mapList) {
            logger.info("stringObjectMap={}",stringObjectMap);
        }

    }
  
    
    
    
    
    
    

    
    
    
    
      
        com.guosh.activiti.mapper.MyCustomMapper
      
    


  
  • 执行流程引擎命令(Command)

    @Test
    public void testCommand(){
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process");
        //创建命令
        ProcessDefinitionEntity processDefinitionEntity= managementService.executeCommand(new Command() {
            @Override
            public ProcessDefinitionEntity execute(CommandContext commandContext) {
                ProcessDefinitionEntity processDefinitionEntity= commandContext.getProcessDefinitionEntityManager()
                        //最后一个流程定义的对象
                        .findLatestProcessDefinitionByKey("my-process");
                return processDefinitionEntity;
            }
        });

        logger.info("processDefinitionEntity={}",processDefinitionEntity);

    }

异常策略

  • ActivitiEXception
  • ActivitiWrongDbException 引擎与数据库版本不匹配
  • ActivitiOptimisticLockingException 并发导致乐观锁异常
  • ActivitiClassLoadingException 加载类异常
  • ActivitiObjectNotFoundException 操作对象不存在
  • ActivitilllegalArgumentException 非法的参数
  • ActivitiTaskAlreadyClaimedException 任务被重新声明代理人
  • BpmnError 定义业务异常控制流程

你可能感兴趣的:(Activiti6.0(四)核心Api)