工作流引擎Activiti学习---使用流程变量进行个人任务分配

来源:互联网 发布:solid works for mac 编辑:程序博客网 时间:2024/05/06 12:49

package com.activiti.study.personalTask;import java.io.InputStream;import java.util.HashMap;import java.util.List;import java.util.Map;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngines;import org.activiti.engine.repository.Deployment;import org.activiti.engine.runtime.ProcessInstance;import org.activiti.engine.task.Task;import org.junit.Test;public class PersonalTask2 {/** *加载默认的流程引擎: 加载 activiti.cfg.xml 文件  */ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();/** * 部署流程定义 */@Testpublic void testDeployProcessDefination_inputStream(){/** -- 从classPath根目录下加载指定名称的文件this.getClass().getClassLoader().getResourceAsStream("/diagrams/ProcessVariables.bpmn");-- 从当前包下加载指定名称的文件this.getClass().getResourceAsStream("diagrams/ProcessVariables.bpmn");-- 从classPath根目录下加载指定名称的文件this.getClass().getResourceAsStream("/diagrams/ProcessVariables.bpmn"); */InputStream inputStreamBpmn = this.getClass().getResourceAsStream("task2.bpmn");InputStream inputStreamPng = this.getClass().getResourceAsStream("task2.png");Deployment deployment = processEngine.getRepositoryService().createDeployment().name("个人任务分配")     //设定流程名称.addInputStream("task2.bpmn", inputStreamBpmn).addInputStream("task2.png", inputStreamPng).deploy();System.out.println("Id: " + deployment.getId());System.out.println("Name: " + deployment.getName());System.out.println("Category: " + deployment.getCategory());}/** * 启动流程 */@Testpublic void testStartProcessDefination(){String processDefinationKey = "personalTask2";Map<String,Object> variables = new HashMap<String,Object>();variables.put("userId", "Lily");ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinationKey,variables);System.out.println("Key--->>>" + pi.getBusinessKey());System.out.println("ActivityId--->>>" + pi.getActivityId());System.out.println("DefinitionId--->>>" + pi.getProcessDefinitionId());System.out.println("ProcessInstanceId--->>>" + pi.getProcessInstanceId());}/** * 查询个人任务 */@Testpublic void findPersonalTask(){/**获取任务id*/String assignee = "Lily";List<Task> tasks = processEngine.getTaskService().createTaskQuery().taskAssignee(assignee).list();for(Task task: tasks){System.out.println("\n------------------\nId: " + task.getId()+ "\nAssignee: " + task.getAssignee() + "\nProcessInstanceId: " + task.getProcessInstanceId());}}/** * 完成任务 */@Testpublic void testCompleteTask(){String taskId = "2405";processEngine.getTaskService().complete(taskId);System.out.println(taskId + " has Done.");}}


<?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:xsd="http://www.w3.org/2001/XMLSchema" 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="personalTask2" name="Personal Task2" isExecutable="true">    <startEvent id="startevent1" name="Start"></startEvent>    <userTask id="usertask2" name="User Task2" activiti:assignee="#{userId}"></userTask>    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask2"></sequenceFlow>    <endEvent id="endevent1" name="End"></endEvent>    <sequenceFlow id="flow2" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>  </process>  <bpmndi:BPMNDiagram id="BPMNDiagram_personalTask2">    <bpmndi:BPMNPlane bpmnElement="personalTask2" id="BPMNPlane_personalTask2">      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">        <omgdc:Bounds height="35.0" width="35.0" x="310.0" y="80.0"></omgdc:Bounds>      </bpmndi:BPMNShape>      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">        <omgdc:Bounds height="55.0" width="105.0" x="390.0" y="70.0"></omgdc:Bounds>      </bpmndi:BPMNShape>      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">        <omgdc:Bounds height="35.0" width="35.0" x="540.0" y="80.0"></omgdc:Bounds>      </bpmndi:BPMNShape>      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">        <omgdi:waypoint x="345.0" y="97.0"></omgdi:waypoint>        <omgdi:waypoint x="390.0" y="97.0"></omgdi:waypoint>      </bpmndi:BPMNEdge>      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">        <omgdi:waypoint x="495.0" y="97.0"></omgdi:waypoint>        <omgdi:waypoint x="540.0" y="97.0"></omgdi:waypoint>      </bpmndi:BPMNEdge>    </bpmndi:BPMNPlane>  </bpmndi:BPMNDiagram></definitions>