工作流学习之入门demo

  1 /**
  2  * Copyright (C), 2015-2018, XXX有限公司
  3  * FileName: DemoMain
  4  * Author:   happy
  5  * Date:     2018/6/23 16:33
  6  * Description:
  7  * History:
  8  * <author>          <time>          <version>          <desc>
  9  * 作者姓名           修改时间           版本号              描述
 10  */
 11 package com.imooc.activiti.helloworld;
 12
 13 import com.google.common.collect.Maps;
 14 import org.activiti.engine.*;
 15 import org.activiti.engine.form.FormProperty;
 16 import org.activiti.engine.form.TaskFormData;
 17 import org.activiti.engine.impl.form.DateFormType;
 18 import org.activiti.engine.impl.form.StringFormType;
 19 import org.activiti.engine.repository.Deployment;
 20 import org.activiti.engine.repository.DeploymentBuilder;
 21 import org.activiti.engine.repository.ProcessDefinition;
 22 import org.activiti.engine.runtime.ProcessInstance;
 23 import org.activiti.engine.task.Task;
 24 import org.slf4j.Logger;
 25 import org.slf4j.LoggerFactory;
 26
 27 import java.text.ParseException;
 28 import java.text.SimpleDateFormat;
 29 import java.util.Date;
 30 import java.util.List;
 31 import java.util.Map;
 32 import java.util.Scanner;
 33
 34 /**
 35  * 〈一句话功能简述〉<br>
 36  * 〈〉
 37  *
 38  * @author happy
 39  * @create 2018/6/23
 40  * @since 1.0.0
 41  */
 42 public class DemoMain {
 43
 44     public static final Logger log = LoggerFactory.getLogger(DemoMain.class);
 45
 46     public static void main(String[] args) {
 47
 48         log.info("启动我们的程序");
 49
 50         //创建流程引擎
 51         ProcessEngine processEngine = getProcessEngine();
 52
 53         //部署流程定义文件
 54         ProcessDefinition processDefinition = getProcessDefinition(processEngine);
 55
 56         //启动运行流程
 57         final ProcessInstance[] processInstance = {getProcessInstance(processEngine, processDefinition)};
 58
 59         //处理流程任务
 60         Scanner scanner = new Scanner(System.in);
 61
 62         while (processInstance[0] != null && !processInstance[0].isEnded()) {
 63
 64             TaskService taskService = processEngine.getTaskService();
 65             List<Task> taskList = taskService.createTaskQuery().list();
 66             log.info("待处理任务数量 [{}]", taskList.size());
 67             taskList.stream().forEach(task -> {
 68                 log.info("待处理任务 [{}]", task.getName());
 69                 FormService formService = processEngine.getFormService();
 70                 TaskFormData taskFormData = formService.getTaskFormData(task.getId());
 71                 List<FormProperty> formProperties = taskFormData.getFormProperties();
 72                 Map<String, Object> variables = Maps.newHashMap();
 73                 formProperties.stream().forEach(property -> {
 74                     String line = null;
 75                     if (StringFormType.class.isInstance(property.getType())) {
 76                         log.info("请输入 {} ?", property.getName());
 77                         line = scanner.nextLine();
 78                         variables.put(property.getId(), line);
 79                     } else if (DateFormType.class.isInstance(property.getType())) {
 80                         log.info("请输入 {} ? 格式 (yyyy-MM-dd)", property.getName());
 81                         line = scanner.nextLine();
 82                         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 83                         Date date = null;
 84                         try {
 85                             date = dateFormat.parse(line);
 86                         } catch (ParseException e) {
 87                             e.printStackTrace();
 88                         }
 89                         variables.put(property.getId(), date);
 90                     } else {
 91                         log.info("类型暂不支持 {}", property.getType());
 92                     }
 93                     log.info("您输入的内容是 [{}]", line);
 94                 });
 95                 taskService.complete(task.getId(), variables);
 96                     //Variable used in lambda expression should be final or effectively final                        //(解决参考 https://www.jianshu.com/p/d957dd5560e8)
 97                 processInstance[0] = processEngine.getRuntimeService().createProcessInstanceQuery().
 98                         processInstanceId(processInstance[0].getId()).singleResult();
 99             });
100
101         }
102
103         log.info("结束我们的程序");
104
105     }
106
107     private static ProcessInstance getProcessInstance(ProcessEngine processEngine, ProcessDefinition processDefinition) {
108         RuntimeService runtimeService = processEngine.getRuntimeService();
109         ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
110         log.info("启动流程 {}", processInstance.getProcessDefinitionKey());
111         return processInstance;
112     }
113
114     private static ProcessDefinition getProcessDefinition(ProcessEngine processEngine) {
115         RepositoryService repositoryService = processEngine.getRepositoryService();
116         DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
117         deploymentBuilder.addClasspathResource("second_approve.bpmn20.xml");
118         Deployment deployment = deploymentBuilder.deploy();
119         String deploymentId = deployment.getId();
120         ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
121                 .deploymentId(deploymentId)
122                 .singleResult();
123
124         log.info("流程定义文件 {},id {}", processDefinition.getName(), processDefinition.getId());
125         return processDefinition;
126     }
127
128     private static ProcessEngine getProcessEngine() {
129         ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
130         ProcessEngine processEngine = cfg.buildProcessEngine();
131         String name = processEngine.getName();
132         String version = ProcessEngine.VERSION;
133
134         log.info("流程引擎的名称 {},版本号 {}", name, version);
135         return processEngine;
136     }
137
138 }

<?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="second_approve" name="二级审批流程" isExecutable="true">
    <startEvent id="startEvent" name="开始"></startEvent>
    <userTask id="submitForm" 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>
    <sequenceFlow id="flow1" sourceRef="startEvent" targetRef="submitForm"></sequenceFlow>
    <exclusiveGateway id="decideSubmit" name="提交or取消"></exclusiveGateway>
    <sequenceFlow id="flow2" sourceRef="submitForm" targetRef="decideSubmit"></sequenceFlow>
    <endEvent id="endEventCancle" name="结束"></endEvent>
    <sequenceFlow id="flow3" sourceRef="decideSubmit" targetRef="endEventCancle">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType=="N" || submitType=="n"}]]></conditionExpression>
    </sequenceFlow>
    <userTask id="tlApprove" name="主管审批">
      <extensionElements>
        <activiti:formProperty id="tlApprove" name="主管审批结果" type="string" required="true"></activiti:formProperty>
        <activiti:formProperty id="tlMessage" name="主管备注" type="string" required="true"></activiti:formProperty>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow4" sourceRef="decideSubmit" targetRef="tlApprove">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType=="Y" || submitType=="y"}]]></conditionExpression>
    </sequenceFlow>
    <exclusiveGateway id="decideTlApprove" name="主管审批校验"></exclusiveGateway>
    <sequenceFlow id="flow5" sourceRef="tlApprove" targetRef="decideTlApprove"></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="flow6" sourceRef="decideTlApprove" targetRef="hr_approve">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove=="Y" || tlApprove=="y"}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow10" sourceRef="decideTlApprove" targetRef="submitForm">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove=="N" || tlApprove=="n"}]]></conditionExpression>
    </sequenceFlow>
    <exclusiveGateway id="decideHrApprove" name="人事审批校验"></exclusiveGateway>
    <sequenceFlow id="flow11" sourceRef="hr_approve" targetRef="decideHrApprove"></sequenceFlow>
    <endEvent id="endEvent" name="结束"></endEvent>
    <sequenceFlow id="flow12" sourceRef="decideHrApprove" targetRef="endEvent">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${hrApprove=="Y" ||hrApprove=="y"}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow13" sourceRef="decideHrApprove" targetRef="submitForm">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${hrApprove=="N" ||hrApprove=="n"}]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_second_approve">
    <bpmndi:BPMNPlane bpmnElement="second_approve" id="BPMNPlane_second_approve">
      <bpmndi:BPMNShape bpmnElement="startEvent" id="BPMNShape_startEvent">
        <omgdc:Bounds height="35.0" width="35.0" x="50.0" y="130.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="submitForm" id="BPMNShape_submitForm">
        <omgdc:Bounds height="55.0" width="105.0" x="130.0" y="120.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="decideSubmit" id="BPMNShape_decideSubmit">
        <omgdc:Bounds height="40.0" width="40.0" x="280.0" y="128.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endEventCancle" id="BPMNShape_endEventCancle">
        <omgdc:Bounds height="35.0" width="35.0" x="400.0" y="230.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="tlApprove" id="BPMNShape_tlApprove">
        <omgdc:Bounds height="55.0" width="105.0" x="365.0" y="121.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="decideTlApprove" id="BPMNShape_decideTlApprove">
        <omgdc:Bounds height="40.0" width="40.0" x="515.0" y="129.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="hr_approve" id="BPMNShape_hr_approve">
        <omgdc:Bounds height="55.0" width="105.0" x="601.0" y="123.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="decideHrApprove" id="BPMNShape_decideHrApprove">
        <omgdc:Bounds height="40.0" width="40.0" x="751.0" y="131.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endEvent" id="BPMNShape_endEvent">
        <omgdc:Bounds height="35.0" width="35.0" x="836.0" y="134.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="85.0" y="147.0"></omgdi:waypoint>
        <omgdi:waypoint x="130.0" y="147.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="235.0" y="147.0"></omgdi:waypoint>
        <omgdi:waypoint x="280.0" y="148.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="300.0" y="168.0"></omgdi:waypoint>
        <omgdi:waypoint x="299.0" y="247.0"></omgdi:waypoint>
        <omgdi:waypoint x="400.0" y="247.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="320.0" y="148.0"></omgdi:waypoint>
        <omgdi:waypoint x="365.0" y="148.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="470.0" y="148.0"></omgdi:waypoint>
        <omgdi:waypoint x="515.0" y="149.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
        <omgdi:waypoint x="555.0" y="149.0"></omgdi:waypoint>
        <omgdi:waypoint x="601.0" y="150.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
        <omgdi:waypoint x="535.0" y="169.0"></omgdi:waypoint>
        <omgdi:waypoint x="535.0" y="296.0"></omgdi:waypoint>
        <omgdi:waypoint x="182.0" y="296.0"></omgdi:waypoint>
        <omgdi:waypoint x="182.0" y="175.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow11" id="BPMNEdge_flow11">
        <omgdi:waypoint x="706.0" y="150.0"></omgdi:waypoint>
        <omgdi:waypoint x="751.0" y="151.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow12" id="BPMNEdge_flow12">
        <omgdi:waypoint x="791.0" y="151.0"></omgdi:waypoint>
        <omgdi:waypoint x="836.0" y="151.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow13" id="BPMNEdge_flow13">
        <omgdi:waypoint x="771.0" y="131.0"></omgdi:waypoint>
        <omgdi:waypoint x="770.0" y="50.0"></omgdi:waypoint>
        <omgdi:waypoint x="182.0" y="50.0"></omgdi:waypoint>
        <omgdi:waypoint x="182.0" y="120.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

原文地址:https://www.cnblogs.com/java-le/p/9219191.html

时间: 2024-12-24 02:10:52

工作流学习之入门demo的相关文章

学习react入门demo的总结。

在github上找到react入门学习比较好的demo,下面是那个链接: https://github.com/ruanyf/react-demos 然后接下来是每个demo的学习笔记: demo1: <body> <div id="example"></div> <script type="text/jsx"> //jsx类型可html和javasript一起写,遇到 HTML 标签,就用 HTML 规则解析:遇到代

mybaits开发环境准备及入门demo(一)

如果说你还在用eclipse做开发,只能说明你要么是一个已经编程很久但是不远改变习惯的老程序员,要么是一个刚开始学习java的新手,如果是前者,我无言以劝,因为习惯很难改变,但是如果你是一个刚入门的新手,那么希望你从这篇文章开始,学习使用----IntelliJ IDEA,忘记eclipse吧 开发环境: IDEA  + mysql + mybatis + spring + maven + git 在这里我想引用下网上对于mybatis和springd的一些优缺点的分析,个人觉得非常透彻. my

RabbitMq 集成 spring boot 消息队列 入门Demo

spring boot 集成 RabbitMq还是很方便的.现在来一个简单的例子来集成rabbitmq.入门demo. 主要概念: 其中比较重要的概念有 4 个,分别为:虚拟主机,交换机,队列,和绑定. 虚拟主机:一个虚拟主机持有一组交换机.队列和绑定.为什么需要多个虚拟主机呢?很简单,RabbitMQ当中,用户只能在虚拟主机的粒度进行权限控制. 因此,如果需要禁止A组访问B组的交换机/队列/绑定,必须为A和B分别创建一个虚拟主机.每一个RabbitMQ服务器都有一个默认的虚拟主机"/"

WebService入门Demo

以前写博客最主要的就是不知道写什么东西,现在感觉能写点东西,就是感觉博客随笔的标题挺难取的,最近工作中刚好用到了WebService,刚好可以写一篇博客.去年工作的时候自己也用到过,只是知道调用一些WebService中的方法,想想还是写篇博客的,也就是俗话说的不要只顾低头走路,还要注意抬头看天.还是写正文吧,现在每次写博客都会会扯点有的没的,不要见怪. WebService的基本概念 WebService看名字的简单点理解就是基于Web的服务,跟普通的web程序一样遵循Http协议,接收响应外

SpringBoot 入门 Demo

SpringBoot   入门 Demo Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者. 特点 1. 创建独立的Spring应用程序 2. 嵌入的Tomcat,无需部署WAR文件 3. 简化Maven配置

Vue学习笔记入门篇——组件的使用

本文为转载,原文:Vue学习笔记入门篇--组件的使用 组件定义 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能.在有些情况下,组件也可以是原生 HTML 元素的形式,以 is 特性扩展. 组件使用 注册 注册一个全局组件,你可以使用 Vue.component(tagName, options).组件在注册之后,便可以在父实例的模块中以自定义元素 的形式使用.

Vue学习笔记入门篇——组件的内容分发(slot)

本文为转载,原文:Vue学习笔记入门篇--组件的内容分发(slot) 介绍 为了让组件可以组合,我们需要一种方式来混合父组件的内容与子组件自己的模板.这个过程被称为 内容分发 (或 "transclusion" 如果你熟悉 Angular).Vue.js 实现了一个内容分发 API,使用特殊的 'slot' 元素作为原始内容的插槽. 编译作用域 在深入内容分发 API 之前,我们先明确内容在哪个作用域里编译.假定模板为: <child-component> {{ messa

mybaits入门demo配置文件详解(二)

第一篇文章: mybaits开发环境准备及入门demo(一) 一.配置文件mybatis-config.xml 官方是这么说的:MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息 在MyBatis 的配置文件中 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//myba

mybaits入门demo映射文件详解(三)

第二篇文章:  mybaits入门demo配置文件详解(二) Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% 的代码.MyBatis 就是针对 SQL 构建的,并且比普通的方法做的更好. SQL 映射文件有很少的几个顶级元素(按照它们应该被定义的顺序): cache – 给定命名空间的缓存配置. cache-ref –