avtiviti工作流demo,activiti入门

package com.zhike.activiti.helloworld;


import org.activiti.engine.*;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.impl.form.DateFormType;
import org.activiti.engine.impl.form.StringFormType;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * author:liuguoxin
 * created time : 2019/4/18 14:15
 */
public class DemoMain {

    private static final Logger LOGGER = LoggerFactory.getLogger(DemoMain.class);

    public static void main(String[] args) throws ParseException {
        LOGGER.info("启动我们的程序");
        //部署流程定义文件
        ProcessEngine processEngine = getProcessEngine();
        //创建流程引擎
        ProcessDefinition processDefinition = getProcessDefinition(processEngine);

        //启动流程
        ProcessInstance processInstance = getProcessInstance(processEngine, processDefinition);

        //处理流程任务
        processTask(processEngine, processInstance);
        LOGGER.info("结束我们的程序");
}

    private static void processTask(ProcessEngine processEngine, ProcessInstance processInstance) throws ParseException {
        Scanner scanner = new Scanner(System.in);
        while (processInstance != null && !processInstance.isEnded()) {
            //处理流程任务
            TaskService taskService = processEngine.getTaskService();
            List<Task> list = taskService.createTaskQuery().list();
            LOGGER.info("处理任务数量 {}", list.size());
            for (Task task : list) {
                LOGGER.info("待处理任务 {}", task.getName());
                Map<String, Object> variables = getStringObjectMap(processEngine, scanner, task);
                taskService.complete(task.getId(),variables);
                processInstance = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();
            }
        }
        scanner.close();
    }

    private static Map<String, Object> getStringObjectMap(ProcessEngine processEngine, Scanner scanner, Task task) throws ParseException {
        FormService formService = processEngine.getFormService();
        TaskFormData taskFormData = formService.getTaskFormData(task.getId());
        List<FormProperty> formProperties = taskFormData.getFormProperties();
        Map<String, Object> variables = new HashMap<String, Object>();
        for (FormProperty formProperty : formProperties) {
            //判断是否是String类型
            String line = null;
            if (StringFormType.class.isInstance(formProperty.getType())) {
                LOGGER.info("请输入 {} ?", formProperty.getName());
                line = scanner.nextLine();
                variables.put(formProperty.getId(), line);
            } else if (DateFormType.class.isInstance(formProperty.getType())) {
                LOGGER.info("请输入 {} ? 格式(yyyy-MM-dd)", formProperty.getName());
                line = scanner.nextLine();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date parse = dateFormat.parse(line);
                variables.put(formProperty.getId(), parse);
            } else {
                LOGGER.info("类型暂不支持 {}", formProperty.getType());
            }
            LOGGER.info("您输入的内容是 【{}】", line);

        }
        return variables;
    }


    private static ProcessInstance getProcessInstance(ProcessEngine processEngine, ProcessDefinition processDefinition) {
        //启动流程
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
        LOGGER.info("启动流程 {}", processInstance.getProcessDefinitionKey());
        return processInstance;
    }

    private static ProcessDefinition getProcessDefinition(ProcessEngine processEngine) {
        //部署流程定义文件
        RepositoryService repositoryService = processEngine.getRepositoryService();
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        deploymentBuilder.addClasspathResource("secondApprove.bpmn20.xml");
        Deployment deployment = deploymentBuilder.deploy();
        String id = deployment.getId();
        ProcessDefinition processDefinition = repositoryService.
                createProcessDefinitionQuery().
                deploymentId(id).
                singleResult();
        LOGGER.info("流程定义对象 {}, 流程ID {}", processDefinition.getName(), processDefinition.getId());
        return processDefinition;
    }

    private static ProcessEngine getProcessEngine() {
        //创建流程引擎
        ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
        ProcessEngine processEngine = cfg.buildProcessEngine();
        String name = processEngine.getName();
        String version = ProcessEngine.VERSION;
        LOGGER.info("流程引擎名称{},版本{}", name, version);
        return processEngine;
    }
}

流程文件

<?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="secound_apporve" name="二级审批" isExecutable="true">
    <startEvent id="startEvent" name="开始节点"></startEvent>
    <userTask id="submitFrom" name="填写审批信息">
      <extensionElements>
        <activiti:formProperty id="message" name="申请信息" type="string" required="true"></activiti:formProperty>
        <activiti:formProperty id="name" name="申请人姓名" type="string" required="true"></activiti:formProperty>
        <activiti:formProperty id="submitTime" name="提交时间" type="date" datePattern="yyyy-MM-dd" required="true"></activiti:formProperty>
        <activiti:formProperty id="submitType" name="确认申请" type="string" required="true"></activiti:formProperty>
      </extensionElements>
    </userTask>
    <exclusiveGateway id="decldeSubmit" name="提交OR取消"></exclusiveGateway>
    <sequenceFlow id="flow3" sourceRef="submitFrom" targetRef="decldeSubmit"></sequenceFlow>
    <userTask id="tl_approve" name="主管审批">
      <extensionElements>
        <activiti:formProperty id="tlApprove" name="主管审批结果" type="string"></activiti:formProperty>
        <activiti:formProperty id="tlMessage" name="主管审批备注" type="string" required="true"></activiti:formProperty>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow4" name="提交" sourceRef="decldeSubmit" targetRef="tl_approve">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType=="y" || submitType == "Y" }]]></conditionExpression>
    </sequenceFlow>
    <exclusiveGateway id="decldeTLApprove" name="主管审批校验"></exclusiveGateway>
    <sequenceFlow id="flow5" sourceRef="tl_approve" targetRef="decldeTLApprove"></sequenceFlow>
    <sequenceFlow id="flow6" name="打回" sourceRef="decldeTLApprove" targetRef="submitFrom">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove=="n" || tlApprove == "N" }]]></conditionExpression>
    </sequenceFlow>
    <userTask id="hr_approve" name="人事审批">
      <extensionElements>
        <activiti:formProperty id="hrApprove" name="人事审批结果" type="string" required="true"></activiti:formProperty>
        <activiti:formProperty id="hrMessage" name="人事审批备注" type="string" required="true"></activiti:formProperty>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow7" name="通过" sourceRef="decldeTLApprove" targetRef="hr_approve">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove=="y" || tlApprove == "Y" }]]></conditionExpression>
    </sequenceFlow>
    <exclusiveGateway id="decldeHRApprove" name="人事审批校验"></exclusiveGateway>
    <sequenceFlow id="flow8" sourceRef="hr_approve" targetRef="decldeHRApprove"></sequenceFlow>
    <endEvent id="endEvent" name="结束"></endEvent>
    <sequenceFlow id="flow9" name="通过" sourceRef="decldeHRApprove" targetRef="endEventCancel">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove=="y" || tlApprove == "Y" }]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow10" name="打回" sourceRef="decldeHRApprove" targetRef="submitFrom">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove=="n" || tlApprove == "N" }]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow11" sourceRef="startEvent" targetRef="submitFrom"></sequenceFlow>
    <endEvent id="endEventCancel" name="取消"></endEvent>
    <sequenceFlow id="flow12" name="取消" sourceRef="decldeSubmit" targetRef="endEventCancel">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType=="n" || submitType == "N" }]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_secound_apporve">
    <bpmndi:BPMNPlane bpmnElement="secound_apporve" id="BPMNPlane_secound_apporve">
      <bpmndi:BPMNShape bpmnElement="startEvent" id="BPMNShape_startEvent">
        <omgdc:Bounds height="35.0" width="35.0" x="208.0" y="191.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="submitFrom" id="BPMNShape_submitFrom">
        <omgdc:Bounds height="55.0" width="105.0" x="355.0" y="181.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="decldeSubmit" id="BPMNShape_decldeSubmit">
        <omgdc:Bounds height="40.0" width="40.0" x="505.0" y="189.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="tl_approve" id="BPMNShape_tl_approve">
        <omgdc:Bounds height="55.0" width="105.0" x="590.0" y="182.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="decldeTLApprove" id="BPMNShape_decldeTLApprove">
        <omgdc:Bounds height="40.0" width="40.0" x="740.0" y="190.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="hr_approve" id="BPMNShape_hr_approve">
        <omgdc:Bounds height="55.0" width="105.0" x="825.0" y="183.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="decldeHRApprove" id="BPMNShape_decldeHRApprove">
        <omgdc:Bounds height="40.0" width="40.0" x="975.0" y="191.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endEvent" id="BPMNShape_endEvent">
        <omgdc:Bounds height="35.0" width="35.0" x="1060.0" y="194.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endEventCancel" id="BPMNShape_endEventCancel">
        <omgdc:Bounds height="35.0" width="35.0" x="610.0" y="270.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="460.0" y="208.0"></omgdi:waypoint>
        <omgdi:waypoint x="505.0" y="209.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="545.0" y="209.0"></omgdi:waypoint>
        <omgdi:waypoint x="590.0" y="209.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="545.0" y="209.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="695.0" y="209.0"></omgdi:waypoint>
        <omgdi:waypoint x="740.0" y="210.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
        <omgdi:waypoint x="760.0" y="230.0"></omgdi:waypoint>
        <omgdi:waypoint x="760.0" y="351.0"></omgdi:waypoint>
        <omgdi:waypoint x="407.0" y="351.0"></omgdi:waypoint>
        <omgdi:waypoint x="407.0" y="236.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="760.0" y="230.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
        <omgdi:waypoint x="780.0" y="210.0"></omgdi:waypoint>
        <omgdi:waypoint x="825.0" y="210.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="780.0" y="210.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
        <omgdi:waypoint x="930.0" y="210.0"></omgdi:waypoint>
        <omgdi:waypoint x="975.0" y="211.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
        <omgdi:waypoint x="995.0" y="231.0"></omgdi:waypoint>
        <omgdi:waypoint x="627.0" y="270.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="995.0" y="231.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
        <omgdi:waypoint x="995.0" y="191.0"></omgdi:waypoint>
        <omgdi:waypoint x="995.0" y="90.0"></omgdi:waypoint>
        <omgdi:waypoint x="407.0" y="90.0"></omgdi:waypoint>
        <omgdi:waypoint x="407.0" y="181.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="995.0" y="191.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow11" id="BPMNEdge_flow11">
        <omgdi:waypoint x="243.0" y="208.0"></omgdi:waypoint>
        <omgdi:waypoint x="355.0" y="208.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow12" id="BPMNEdge_flow12">
        <omgdi:waypoint x="525.0" y="229.0"></omgdi:waypoint>
        <omgdi:waypoint x="525.0" y="287.0"></omgdi:waypoint>
        <omgdi:waypoint x="610.0" y="287.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="525.0" y="229.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zhike.activiti</groupId>
    <artifactId>acitviti6-helloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.11</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.3.176</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值