java 执行 activiti简单执行

java 执行 activiti 第一个例子

说明:自定义一个简单流程,然后在main中直接通过控制台输入信息然后控制流程。

1、创建一个maven项目 引入依赖:

pom.xml

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

    <groupId>com.jy.activiti</groupId>
    <artifactId>activiti6-helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--activiti 引擎-->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>6.0.0</version>
        </dependency>
        <!-- junit 测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</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>23.0</version>
        </dependency>

        <!-- 数据看看-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.3.176</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、添加流程模板文件:

MyProcess.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: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"></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="提交或取消"></exclusiveGateway>
    <sequenceFlow id="flow2" sourceRef="submitForm" targetRef="decideSubmit"></sequenceFlow>
    <userTask id="ti_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="flow3" sourceRef="decideSubmit" targetRef="ti_approve">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType=="y"||submitType=="Y"}]]></conditionExpression>
    </sequenceFlow>
    <exclusiveGateway id="decideTLapprove" name="主管审批校验"></exclusiveGateway>
    <sequenceFlow id="flow4" sourceRef="ti_approve" 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="flow5" sourceRef="decideTLapprove" targetRef="hr_approve">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove=="y"||tlApprove=="Y"}]]></conditionExpression>
    </sequenceFlow>
    <endEvent id="endeventCancle" name="取消"></endEvent>
    <sequenceFlow id="flow7" sourceRef="decideSubmit" targetRef="endeventCancle">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType=="n"||submitType=="N"}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow9" sourceRef="decideTLapprove" targetRef="submitForm"></sequenceFlow>
    <exclusiveGateway id="hrDecideApprove" name="人事审批校验"></exclusiveGateway>
    <sequenceFlow id="flow10" sourceRef="hr_approve" targetRef="hrDecideApprove"></sequenceFlow>
    <endEvent id="endevent" name="结束"></endEvent>
    <sequenceFlow id="flow11" sourceRef="hrDecideApprove" targetRef="endeventCancle">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${hrApprove=="y"||hrApprove=="Y"}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow12" sourceRef="hrDecideApprove" targetRef="submitForm"></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="200.0" y="230.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="submitForm" id="BPMNShape_submitForm">
        <omgdc:Bounds height="55.0" width="105.0" x="280.0" y="220.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="decideSubmit" id="BPMNShape_decideSubmit">
        <omgdc:Bounds height="40.0" width="40.0" x="430.0" y="228.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="ti_approve" id="BPMNShape_ti_approve">
        <omgdc:Bounds height="55.0" width="105.0" x="515.0" y="221.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="decideTLapprove" id="BPMNShape_decideTLapprove">
        <omgdc:Bounds height="40.0" width="40.0" x="665.0" y="229.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="hr_approve" id="BPMNShape_hr_approve">
        <omgdc:Bounds height="55.0" width="105.0" x="750.0" y="222.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endeventCancle" id="BPMNShape_endeventCancle">
        <omgdc:Bounds height="35.0" width="35.0" x="540.0" y="310.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="hrDecideApprove" id="BPMNShape_hrDecideApprove">
        <omgdc:Bounds height="40.0" width="40.0" x="900.0" y="230.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent" id="BPMNShape_endevent">
        <omgdc:Bounds height="35.0" width="35.0" x="985.0" y="233.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="235.0" y="247.0"></omgdi:waypoint>
        <omgdi:waypoint x="280.0" y="247.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="385.0" y="247.0"></omgdi:waypoint>
        <omgdi:waypoint x="430.0" y="248.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="470.0" y="248.0"></omgdi:waypoint>
        <omgdi:waypoint x="515.0" y="248.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="620.0" y="248.0"></omgdi:waypoint>
        <omgdi:waypoint x="665.0" y="249.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="705.0" y="249.0"></omgdi:waypoint>
        <omgdi:waypoint x="750.0" y="249.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
        <omgdi:waypoint x="450.0" y="268.0"></omgdi:waypoint>
        <omgdi:waypoint x="450.0" y="327.0"></omgdi:waypoint>
        <omgdi:waypoint x="540.0" y="327.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
        <omgdi:waypoint x="685.0" y="269.0"></omgdi:waypoint>
        <omgdi:waypoint x="684.0" y="380.0"></omgdi:waypoint>
        <omgdi:waypoint x="522.0" y="380.0"></omgdi:waypoint>
        <omgdi:waypoint x="332.0" y="380.0"></omgdi:waypoint>
        <omgdi:waypoint x="332.0" y="275.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
        <omgdi:waypoint x="855.0" y="249.0"></omgdi:waypoint>
        <omgdi:waypoint x="900.0" y="250.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow11" id="BPMNEdge_flow11">
        <omgdi:waypoint x="920.0" y="270.0"></omgdi:waypoint>
        <omgdi:waypoint x="557.0" y="310.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow12" id="BPMNEdge_flow12">
        <omgdi:waypoint x="920.0" y="230.0"></omgdi:waypoint>
        <omgdi:waypoint x="919.0" y="177.0"></omgdi:waypoint>
        <omgdi:waypoint x="629.0" y="177.0"></omgdi:waypoint>
        <omgdi:waypoint x="332.0" y="177.0"></omgdi:waypoint>
        <omgdi:waypoint x="332.0" y="220.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

也可以自己创建模板文件,参考:

https://blog.csdn.net/java_study_/article/details/104904861

3、创建一个类MyActiviti,直接main方法测试:

package com.jy.main;

import com.google.common.collect.Maps;
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.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.*;

public class MyActiviti {
    private static final Logger logger = LoggerFactory.getLogger(DemoMain1.class);
    private static String filePath = "MyProcess.bpmn";
    public static void main(String[] args) throws ParseException {
        logger.info("启动程序------------");
        //1创建流程引擎
        ProcessEngine processEngine = ProcessEngineConfiguration
                .createStandaloneInMemProcessEngineConfiguration()
                .buildProcessEngine();
        logger.info("流程引擎名称[{}],版本[{}]",processEngine.getName(),processEngine.VERSION);
        //2、部署流程定义文件
        Deployment deploy = processEngine.getRepositoryService().createDeployment()
                .addClasspathResource(filePath).deploy();
        ProcessDefinition processDefinition = processEngine.getRepositoryService()
                .createProcessDefinitionQuery()
                .deploymentId(deploy.getId())
                .singleResult();
        //3、启动流程
        ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceById(processDefinition.getId());
        logger.info("流程启动{}",processInstance.getProcessDefinitionKey());
        //4.处理流程任务
        Scanner scanner = new Scanner(System.in);

        while (processInstance!=null&&!processInstance.isEnded()){
            TaskService taskService = processEngine.getTaskService();
            List<Task> list = taskService.createTaskQuery().list();
            for (Task task : list){
                TaskFormData taskFormData = processEngine.getFormService().getTaskFormData(task.getId());
                List<FormProperty> formProperties = taskFormData.getFormProperties();
                Map<String,Object> map = Maps.newHashMap();
                for (FormProperty pro:formProperties) {
                    String line = null;
                    if(StringFormType.class.isInstance(pro.getType())){
                        logger.info("请输入:{}",pro.getName());
                        line = scanner.nextLine();
                        logger.info("您输入的是:{}",line);
                        map.put(pro.getId(),line);
                    }else if (DateFormType.class.isInstance(pro.getType())){
                        logger.info("请输入:{},格式{yyyy-MM-dd}",pro.getName());
                        line = scanner.nextLine();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
                        Date date = simpleDateFormat.parse(line);
                        map.put(pro.getId(),date);
                    }else{
                        logger.info("类型暂不支持{}",pro.getType());
                    }
                }
                taskService.complete(task.getId(),map);
                //更新任务节点
                processInstance = processEngine
                        .getRuntimeService()
                        .createProcessInstanceQuery()
                        .processInstanceId(processInstance.getId())
                        .singleResult();
            }
        }
        logger.info("结束程序------------");
    }
}

注意:有的地方方法,部署流程定义文件时直接改为.xml文件,当时我也使用过,但不行,只能使用
.bpmn文件。
效果图:
java 执行 activiti简单执行
java 执行 activiti简单执行
java 执行 activiti简单执行

上一篇:WC 2018 题解


下一篇:《笨方法学Python》加分题38