工作流activiti技术学习和研究第五天

上节学习了流程实例,任务执行的知识点,今天接着学习流程变量相关知识。

1.流程变量

流程图请看附件;

流程变量在整个工作流中扮演很重要的作用。例如:请假流程中有请假天数、请假原因等一些参数都为流程变量的范围。流程变量的作用域范围是只对应一个流程实例。也就是说各个流程实例的流程变量是不相互影响的。流程实例结束完成以后流程变量还保存在数据库中(存放到流程变量的历史表中)

部署流程定义

public void deploymentProcessDefinition_inputStream(){
  InputStream inputStreambpmn = this.getClass().getResourceAsStream("/diagrams/processVariables.bpmn");
  InputStream inputStreampng = this.getClass().getResourceAsStream("/diagrams/processVariables.png");
  Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署对象相关的Service
      .createDeployment()//创建一个部署对象
      .name("请假流程定义")//添加部署的名称
      .addInputStream("processVariables.bpmn", inputStreambpmn)//使用资源文件的名称(要求:与资源文件的名称要一致),和输入流完成部署
      .addInputStream("processVariables.png", inputStreampng)//使用资源文件的名称(要求:与资源文件的名称要一致),和输入流完成部署
      .deploy();//完成部署
  System.out.println("部署ID:"+deployment.getId());//
  System.out.println("部署名称:"+deployment.getName());//
 }

启动流程实例

public void startProcessInstance() {
  ProcessInstance pi = processEngine.getRuntimeService()// 与正在执行的流程实例和执行对象相关的Service
    .startProcessInstanceByKey("processVariables");
  System.out.println("流程实例ID:" + pi.getId());// 流程实例ID
  System.out.println("流程定义ID:" + pi.getProcessDefinitionId());// 流程定义ID
 }

设置流程变量

public void setVariables(){
  TaskService taskService=processEngine.getTaskService();//获取service
  String taskId="904";
  taskService.setVariable(taskId,"请假天数",5);
  taskService.setVariableLocal(taskId,"请假原因","年假");
  taskService.setVariable(taskId,"请假时间",new Date());
  System.out.println("设置流程变量成功");
 }

获取流程变量

public void getVariables(){
  TaskService taskService = processEngine.getTaskService();
  String taskId="904";
  Integer days=(Integer) taskService.getVariable(taskId,"请假天数");
  Date date =(Date) taskService.getVariable(taskId,"请假时间");
  String reasean =(String) taskService.getVariable(taskId,"请假原因");
  System.out.println("请假天数:"+days);
  System.out.println("请假时间:"+date);
  System.out.println("请假原因:"+reasean);
 }

查询历史的流程变量

public void findHistoryProcessVariables(){
  List<HistoricVariableInstance> list =processEngine.getHistoryService()
                          .createHistoricVariableInstanceQuery()//创建一个历史的流程变量查询对象
                          .variableName("请假时间").list();
  if(list!=null && list.size()>0){
   for(HistoricVariableInstance hvi:list){
    System.out.println(hvi.getId()+"   "+hvi.getProcessInstanceId()+"   "+hvi.getVariableName()+"   "+hvi.getVariableTypeName()+"    "+hvi.getValue());
   }
  }
 }

总结

    1:流程变量

在流程执行或者任务执行的过程中,用于设置和获取变量,使用流程变量在流程传递的过程中传递业务参数。

对应的表:

act_ru_variable:正在执行的流程变量表

act_hi_varinst:流程变量历史表

该例子的bpmn文件内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" 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" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="processVariables" name="processVariablesProcess" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="提交申请" activiti:assignee="张晓晓"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <userTask id="usertask2" name="经理审批" activiti:assignee="李大大"></userTask>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_processVariables">
    <bpmndi:BPMNPlane bpmnElement="processVariables" id="BPMNPlane_processVariables">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="300.0" y="70.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="265.0" y="160.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="265.0" y="257.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="300.0" y="360.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="317.0" y="105.0"></omgdi:waypoint>
        <omgdi:waypoint x="317.0" y="160.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="317.0" y="215.0"></omgdi:waypoint>
        <omgdi:waypoint x="317.0" y="257.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="317.0" y="312.0"></omgdi:waypoint>
        <omgdi:waypoint x="317.0" y="360.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

2. 流程执行历史记录

查询历史流程实例

public void findHistoryActiviti(){
  List<HistoricActivityInstance> list =processEngine.getHistoryService()//创建历史活动实例的查询
    .createHistoricActivityInstanceQuery().processInstanceId("501")
    .orderByHistoricActivityInstanceStartTime().asc().list();
  if(list!=null && list.size()>0){
   for(HistoricActivityInstance hai:list){
    System.out.println(hai.getId()+"   "+hai.getProcessInstanceId()+"   "+hai.getActivityType()+"  "+hai.getStartTime()+"   "+hai.getEndTime()+"   "+hai.getDurationInMillis());
    System.out.println("#####################");
   }
  }
 }

/**查询历史任务*/
 @Test
 public void findHistoryTask(){
  String processInstanceId = "601";
  List<HistoricTaskInstance> list = processEngine.getHistoryService()//与历史数据(历史表)相关的Service
      .createHistoricTaskInstanceQuery()//创建历史任务实例查询
      .processInstanceId(processInstanceId)//
      .orderByHistoricTaskInstanceStartTime().asc()
      .list();
  if(list!=null && list.size()>0){
   for(HistoricTaskInstance hti:list){
    System.out.println(hti.getId()+"    "+hti.getName()+"    "+hti.getProcessInstanceId()+"   "+hti.getStartTime()+"   "+hti.getEndTime()+"   "+hti.getDurationInMillis());
    System.out.println("################################");
   }
  }
 }

/**查询历史流程变量*/
 //@Test
 public void findHistoryProcessVariables(){
  String processInstanceId = "601";
  List<HistoricVariableInstance> list = processEngine.getHistoryService()//
      .createHistoricVariableInstanceQuery()//创建一个历史的流程变量查询对象
      .processInstanceId(processInstanceId)//
      .list();
  if(list!=null && list.size()>0){
   for(HistoricVariableInstance hvi:list){
    System.out.println(hvi.getId()+"   "+hvi.getProcessInstanceId()+"   "+hvi.getVariableName()+"   "+hvi.getVariableTypeName()+"    "+hvi.getValue());
    System.out.println("###############################################");
   }
  }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值