Activiti學習筆記二:簡單順序流演示demo

衣舞晨風發表於2016-09-03
package jiankunking.helloworld;

import java.util.List;

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 HelloWorld {

    // 預設載入activiti.cfg.xml classLoader.getResources("activiti.cfg.xml")
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /** 部署流程定義 */
     @Test
     public void deploymentProcessDefinition() {
     Deployment deployment = processEngine.getRepositoryService()//與流程定義和部署物件相關的Service
     .createDeployment()// 建立一個部署物件
     .name("helloworld入門程式")// 新增部署的名稱
     .addClasspathResource("diagrams/helloworld.bpmn")//從classpath的資源中載入,一次只能載入一個檔案
     .addClasspathResource("diagrams/helloworld.png")//從classpath的資源中載入,一次只能載入一個檔案
     .deploy();// 完成部署
     System.out.println("部署ID:" + deployment.getId());// 1
     System.out.println("部署名稱:" + deployment.getName());// helloworld入門程式
     }

     /** 啟動流程例項 */
     @Test
     public void startProcessInstance() {
     // 流程定義的key
     String processDefinitionKey = "helloWorld";
     ProcessInstance pi = processEngine.getRuntimeService()//與正在執行的流程例項和執行物件相關的Service
     .startProcessInstanceByKey(processDefinitionKey);//使用流程定義的key啟動流程例項,key對應helloworld.bpmn檔案中id的屬性值,使用key值啟動,預設是按照最新版本的流程定義啟動
     System.out.println("流程例項ID:" + pi.getId());// 流程例項ID
     System.out.println("流程定義ID:" + pi.getProcessDefinitionId());// 流程定義ID
     }

    /** 查詢當前人的個人任務 */
    @Test
    public void findMyPersonalTask() {
        String assignee = "李四";
        List<Task> list = processEngine.getTaskService()// 與正在執行的任務管理相關的Service
                .createTaskQuery()// 建立任務查詢物件
                .taskAssignee(assignee)// 指定個人任務查詢,指定辦理人
                .list();
        if (list != null && list.size() > 0) {
            for (Task task : list) {
                System.out.println("任務ID:" + task.getId());
                System.out.println("任務名稱:" + task.getName());
                System.out.println("任務的建立時間:" + task.getCreateTime());
                System.out.println("任務的辦理人:" + task.getAssignee());
                System.out.println("流程例項ID:" + task.getProcessInstanceId());
                System.out.println("執行物件ID:" + task.getExecutionId());
                System.out.println("流程定義ID:" + task.getProcessDefinitionId());
                System.out.println("########################################################");
            }
        }
    }

     /** 完成我的任務 */
     @Test
     public void completeMyPersonalTask() {
     // 任務ID
     String taskId = "50004";
     processEngine.getTaskService()// 與正在執行的任務管理相關的Service
     .complete(taskId);
     System.out.println("完成任務:任務ID:" + taskId);
     }
}

流程圖xml如下:

<?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="helloWorld" name="helloWorldProcess" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <userTask id="usertask1" name="提交申請" activiti:assignee="小明1"></userTask>
    <userTask id="usertask2" name="部門經理審批" activiti:assignee="李四"></userTask>
    <userTask id="usertask3" name="總經理審批" activiti:assignee="王五"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow>
    <sequenceFlow id="flow4" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_helloWorld">
    <bpmndi:BPMNPlane bpmnElement="helloWorld" id="BPMNPlane_helloWorld">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="41.0" width="41.0" x="120.0" y="90.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="350.0" y="360.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="315.0" y="83.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="315.0" y="180.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
        <omgdc:Bounds height="55.0" width="105.0" x="315.0" y="270.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="161.0" y="110.0"></omgdi:waypoint>
        <omgdi:waypoint x="315.0" y="110.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="367.0" y="138.0"></omgdi:waypoint>
        <omgdi:waypoint x="367.0" y="180.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="367.0" y="235.0"></omgdi:waypoint>
        <omgdi:waypoint x="367.0" y="270.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="367.0" y="325.0"></omgdi:waypoint>
        <omgdi:waypoint x="367.0" y="360.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

demo原始碼下載:http://download.csdn.net/detail/xunzaosiyecao/9620644

本文部分內容整理自itcast講義,在此表示感謝。
作者:jiankunking 出處:http://blog.csdn.net/jiankunking

相關文章