1.绘制BPMN
填写id和name
StartEvent ------------- 开始
UserTask1 ------------- 提交审批信息
decideSubmit ------------- 提交OR取消
tl_approve ------------- 主管审批
decideTLApprove ------------- 主管审批校验
hr_approve ------------- 人事审批
decideHRApprove ------------- 人事审批校验
EndEvent ------------- 结束
2.表单绘制
2.1点击提交审批信息
点击form ,填写表单字段 (id,name,Tyle,Required)
2.2点击 主管审批
点击form ,填写表单字段 (id,name,Tyle,Required)
2.3 点击 人事审批
点击form ,填写表单字段 (id,name,Tyle,Required)
2.4 点击 提交OR取消 和 主管审批 中间的连接线,编辑
在condition填入表达式: ${submitType == "y" || submitType == "Y"}
2.5点击主管审批校验和人事审批中间的连接线,编辑
在condition填入表达式:
${tlApprove == "y" || tlApprove == "Y"}
2.6点击主管审批校验和提交审批信息中间的连接线,编辑
在condition填入表达式:
${tlApprove == "n" || tlApprove == "N"}
2.7 同理 人事审批校验的两条线编辑
${hrApprove == "y" || hrApprove == "Y"}
${hrApprove == "n" || hrApprove == "N"}
2.8保存文件,修改文件后缀名bpmn为xml,右键
3.java编码
3.1.maven配置
<?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>2.0.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.nn</groupId>
<artifactId>activiti-test</artifactId>
<version>1.0-SNAPSHOT</version>
<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.11</version>
</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.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 logback.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="30 seconds">
<property name="log.dir" value="target/logs" />
<property name="encoding" value="UTF-8" />
<property name="plain" value="%msg%n" />
<property name="std" value="%d{HH:mm:ss.SSS}[%thread][%-5level]%msg %X{user} %logger{10}.%M:%L%n" />
<property name="normal" value="%d{yyyy-MM-dd:HH:mm:ss.SSS}[%thread][%-5level] %logger{10}.%M:%L %msg%n" />
<!-- 控制台输出 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${plain}</pattern>
<charset>${encoding}</charset>
</encoder>
</appender>
<!-- 时间滚动输出 level为 ALL 日志 -->
<appender name="file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log.dir}/file.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.dir}/file.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>${std}</pattern>
<charset>${encoding}</charset>
</encoder>
</appender>
<logger name="root" >
<level value="ERROR"/>
</logger>
<!--就是下面这个配置,我的是cn.lucasma,你需要修改成你的-->
<logger name="com.nn" >
<level value="DEBUG"/>
</logger>
<root>
<appender-ref ref="stdout" />
<appender-ref ref="file" />
</root>
</configuration>
3.3 DemoMain
package com.nn.activiti;
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.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.Date;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author huangRuYuan 2019/10/9
*/
public class DemoMain {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoMain.class);
public static void main(String[] args) {
// 创建流程引擎
ProcessEngine processEngine = getProcessEngine();
//部署流程定义文件
ProcessDefinition processDefinition = getProcessDefinition(processEngine);
//启动运行流程
ProcessInstance processInstance = processInstance(processEngine, processDefinition);
//处理流程任务
processTask(processEngine, processInstance);
LOGGER.info("结束程序");
}
private static void processTask(ProcessEngine processEngine, ProcessInstance processInstance) {
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 = getMap(processEngine, scanner, task);
taskService.complete(task.getId(), variables);
processInstance = processEngine.getRuntimeService()
.createProcessInstanceQuery()
.processInstanceId(processInstance.getId())
.singleResult();
}
}
scanner.close();
}
private static Map<String, Object> getMap(ProcessEngine processEngine, Scanner scanner, Task task) {
FormService formService = processEngine.getFormService();
TaskFormData taskFormData = formService.getTaskFormData(task.getId());
List<FormProperty> formProperties = taskFormData.getFormProperties();
Map<String, Object> variables = Maps.newHashMap();
formProperties.forEach((property) -> {
String line = null;
if (StringFormType.class.isInstance(property.getType())) {
LOGGER.info("请输入{}?", property.getName());
line = scanner.nextLine();
variables.put(property.getId(), line);
} else if (DateFormType.class.isInstance(property.getType())) {
LOGGER.info("请输入{}?格式 (yyyy-MM-dd)", property.getName());
line = scanner.nextLine();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = simpleDateFormat.parse(line);
variables.put(property.getId(), date);
} catch (ParseException e) {
e.printStackTrace();
}
} else {
LOGGER.info("类型暂不支持{}", property.getType());
}
LOGGER.info("您输入的是[{}]", line);
});
return variables;
}
private static ProcessInstance processInstance(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("diagrams/second_approve.bpmn20.xml");
Deployment deploy = deploymentBuilder.deploy();
String deployId = deploy.getId();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployId).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;
}
}
附
second_approve.bpmn20.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 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" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1568972225452" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
<process id="second_approve" isClosed="false" isExecutable="true" name="二级审批" processType="None">
<startEvent id="StartEvent" name="开始"/>
<userTask activiti:exclusive="true" id="UserTask1" name="提交审批信息">
<extensionElements>
<activiti:formProperty id="message" name="申请信息" required="true" type="string"/>
<activiti:formProperty id="name" name="申请人姓名" required="true" type="string"/>
<activiti:formProperty datePattern="yyyy-MM-dd" id="submitTime" name="提交时间" required="true" type="date"/>
<activiti:formProperty id="submitType" name="确认申请" required="true" type="string"/>
</extensionElements>
</userTask>
<endEvent id="EndEvent" name="结束"/>
<sequenceFlow id="_6" sourceRef="StartEvent" targetRef="UserTask1"/>
<exclusiveGateway gatewayDirection="Unspecified" id="decideSubmit" name="提交OR取消"/>
<userTask activiti:exclusive="true" id="tl_approve" name="主管审批">
<extensionElements>
<activiti:formProperty id="tlApprove" name="主管审批结果" type="string"/>
<activiti:formProperty id="tlMessage" name="主管备注" required="true" type="string"/>
</extensionElements>
</userTask>
<sequenceFlow id="_10" sourceRef="UserTask1" targetRef="decideSubmit"/>
<sequenceFlow id="_11" sourceRef="decideSubmit" targetRef="tl_approve">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType == "y" || submitType == "Y"}]]></conditionExpression>
</sequenceFlow>
<exclusiveGateway gatewayDirection="Unspecified" id="decideHRApprove" name="人事审批校验"/>
<sequenceFlow id="_14" sourceRef="decideHRApprove" targetRef="EndEvent">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${hrApprove == "y" || hrApprove == "Y"}]]></conditionExpression>
</sequenceFlow>
<endEvent id="EndEventCancle" name="结束"/>
<sequenceFlow id="_16" sourceRef="decideSubmit" targetRef="EndEventCancle"/>
<exclusiveGateway gatewayDirection="Unspecified" id="decideTLApprove" name="主管审批校验"/>
<userTask activiti:exclusive="true" id="hr_approve" name="人事审批">
<extensionElements>
<activiti:formProperty id="hrApprove" name="人事审批结果" type="string"/>
<activiti:formProperty id="hrMessage" name="人事审批备注" required="true" type="string"/>
</extensionElements>
</userTask>
<sequenceFlow id="_19" sourceRef="tl_approve" targetRef="decideTLApprove"/>
<sequenceFlow id="_20" sourceRef="decideTLApprove" targetRef="hr_approve">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove == "y" || tlApprove == "Y"}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="_21" sourceRef="hr_approve" targetRef="decideHRApprove"/>
<sequenceFlow id="_22" sourceRef="decideTLApprove" targetRef="UserTask1">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove == "n" || tlApprove == "N"}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="_23" sourceRef="decideHRApprove" targetRef="UserTask1">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${hrApprove == "n" || hrApprove == "N"}]]></conditionExpression>
</sequenceFlow>
</process>
<bpmndi:BPMNDiagram documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
<bpmndi:BPMNPlane bpmnElement="second_approve">
<bpmndi:BPMNShape bpmnElement="StartEvent" id="Shape-StartEvent">
<omgdc:Bounds height="32.0" width="32.0" x="10.0" y="215.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="UserTask1" id="Shape-UserTask1">
<omgdc:Bounds height="55.0" width="85.0" x="110.0" y="200.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="EndEvent" id="Shape-EndEvent">
<omgdc:Bounds height="32.0" width="32.0" x="795.0" y="215.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="decideSubmit" id="Shape-decideSubmit" isMarkerVisible="false">
<omgdc:Bounds height="32.0" width="32.0" x="260.0" y="215.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="tl_approve" id="Shape-tl_approve">
<omgdc:Bounds height="55.0" width="85.0" x="340.0" y="200.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="decideHRApprove" id="Shape-decideHRApprove" isMarkerVisible="false">
<omgdc:Bounds height="32.0" width="32.0" x="715.0" y="220.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="EndEventCancle" id="Shape-EndEventCancle">
<omgdc:Bounds height="32.0" width="32.0" x="350.0" y="340.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="decideTLApprove" id="Shape-decideTLApprove" isMarkerVisible="false">
<omgdc:Bounds height="32.0" width="32.0" x="480.0" y="210.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="hr_approve" id="Shape-hr_approve">
<omgdc:Bounds height="55.0" width="85.0" x="590.0" y="200.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="_23" id="BPMNEdge__23" sourceElement="decideHRApprove" targetElement="UserTask1">
<omgdi:waypoint x="731.0" y="252.0"/>
<omgdi:waypoint x="435.0" y="435.0"/>
<omgdi:waypoint x="152.5" y="255.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_14" id="BPMNEdge__14" sourceElement="decideHRApprove" targetElement="EndEvent">
<omgdi:waypoint x="747.0" y="236.0"/>
<omgdi:waypoint x="795.0" y="231.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_16" id="BPMNEdge__16" sourceElement="decideSubmit" targetElement="EndEventCancle">
<omgdi:waypoint x="275.0" y="246.0"/>
<omgdi:waypoint x="275.0" y="360.0"/>
<omgdi:waypoint x="350.5080666151703" y="360.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_19" id="BPMNEdge__19" sourceElement="tl_approve" targetElement="decideTLApprove">
<omgdi:waypoint x="425.0" y="227.5"/>
<omgdi:waypoint x="480.0" y="226.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6" sourceElement="StartEvent" targetElement="UserTask1">
<omgdi:waypoint x="42.0" y="231.0"/>
<omgdi:waypoint x="110.0" y="227.5"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_20" id="BPMNEdge__20" sourceElement="decideTLApprove" targetElement="hr_approve">
<omgdi:waypoint x="511.0" y="225.0"/>
<omgdi:waypoint x="540.0" y="225.0"/>
<omgdi:waypoint x="590.0" y="225.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_11" id="BPMNEdge__11" sourceElement="decideSubmit" targetElement="tl_approve">
<omgdi:waypoint x="292.0" y="231.0"/>
<omgdi:waypoint x="340.0" y="227.5"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="-19.0" width="0.0" x="0.0" y="46.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_22" id="BPMNEdge__22" sourceElement="decideTLApprove" targetElement="UserTask1">
<omgdi:waypoint x="496.0" y="210.0"/>
<omgdi:waypoint x="350.0" y="100.0"/>
<omgdi:waypoint x="152.5" y="200.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_10" id="BPMNEdge__10" sourceElement="UserTask1" targetElement="decideSubmit">
<omgdi:waypoint x="195.0" y="227.5"/>
<omgdi:waypoint x="260.0" y="231.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_21" id="BPMNEdge__21" sourceElement="hr_approve" targetElement="decideHRApprove">
<omgdi:waypoint x="675.0" y="227.5"/>
<omgdi:waypoint x="715.0" y="236.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
activiti.cfg.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
<property name="jdbcDriver" value="org.h2.Driver" />
<property name="jdbcUsername" value="sa" />
<property name="jdbcPassword" value="" />
<!--<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf-8&useSSL=false" />-->
<!--<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />-->
<!--<property name="jdbcUsername" value="root" />-->
<!--<property name="jdbcPassword" value="root" />-->
<property name="databaseSchemaUpdate" value="true" />
</bean>
</beans>