我们用 通过 Activiti 来写一个控制台输入输出的 工作流小例子
编程使用环境
- Activiti -6.0.0.zip
- jdk1.8.0_161
- apache-tomcat-8.0.50.zip
- 操作系统 macOS
- IDEA 作为编码工具
这是一个控制台程序,通过控制台输入体验工作流引擎执行过程。流程执行的图示如下
使用maven 创建 一个工程
Java程序代码
package cn.lucasma.activiti.helloworld;
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.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;
/**
* 启动程序类
*/
public class DemoMain {
private static final org.slf4j.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 = 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) throws ParseException {
FormService formService = processEngine.getFormService();
TaskFormData taskFormData = formService.getTaskFormData(task.getId());
List<FormProperty> formProperties = taskFormData.getFormProperties();
Map<String, Object> variables = Maps.newHashMap();
for (FormProperty property : formProperties) {
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 dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(line);
variables.put(property.getId(), date);
} else {
LOGGER.info("类型暂不支持 {}", property.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("second_approve.bpmn20.xml");
Deployment deployment = deploymentBuilder.deploy();
String deploymentId = deployment.getId();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.deploymentId(deploymentId).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;
}
}
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>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.lucasma.activiti</groupId>
<artifactId>activiti6-helloworld</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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.176</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</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>
logback.xml
这个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="cn.lucasma" >
<level value="DEBUG"/>
</logger>
<root>
<appender-ref ref="stdout" />
<appender-ref ref="file" />
</root>
</configuration>
流程引擎文件
命名原则 见名知意 此处命名second_approve.bpmn20.xml
,这个流程是可以在eclipse 配置好绘制回来的,这里就直接给出绘制好的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="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>
<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="flow3" sourceRef="decideSubmit" targetRef="tl_approve">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType == "y" || submitType == "Y"}]]></conditionExpression>
</sequenceFlow>
<exclusiveGateway id="decideTLApprove" name="主管审批校验"></exclusiveGateway>
<sequenceFlow id="flow4" sourceRef="tl_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>
<exclusiveGateway id="decideHRApprove" name="人事审批校验"></exclusiveGateway>
<sequenceFlow id="flow6" sourceRef="hr_approve" targetRef="decideHRApprove"></sequenceFlow>
<endEvent id="endEvent" name="结束"></endEvent>
<sequenceFlow id="flow7" sourceRef="decideHRApprove" targetRef="endEvent">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${hrApprove == "y" || hrApprove == "Y"}]]></conditionExpression>
</sequenceFlow>
<endEvent id="endEventCancel" name="取消"></endEvent>
<sequenceFlow id="flow8" sourceRef="decideSubmit" targetRef="endEventCancel">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType == "n" || submitType == "N"}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow9" sourceRef="decideTLApprove" targetRef="submitForm">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove == "n" || tlApprove == "N"}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow10" 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="160.0" y="180.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="submitForm" id="BPMNShape_submitForm">
<omgdc:Bounds height="55.0" width="105.0" x="240.0" y="170.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="decideSubmit" id="BPMNShape_decideSubmit">
<omgdc:Bounds height="40.0" width="40.0" x="390.0" y="178.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="tl_approve" id="BPMNShape_tl_approve">
<omgdc:Bounds height="55.0" width="105.0" x="475.0" y="171.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="decideTLApprove" id="BPMNShape_decideTLApprove">
<omgdc:Bounds height="40.0" width="40.0" x="625.0" y="179.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="hr_approve" id="BPMNShape_hr_approve">
<omgdc:Bounds height="55.0" width="105.0" x="710.0" y="172.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="decideHRApprove" id="BPMNShape_decideHRApprove">
<omgdc:Bounds height="40.0" width="40.0" x="860.0" y="180.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endEvent" id="BPMNShape_endEvent">
<omgdc:Bounds height="35.0" width="35.0" x="945.0" y="183.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endEventCancel" id="BPMNShape_endEventCancel">
<omgdc:Bounds height="35.0" width="35.0" x="510.0" y="250.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="195.0" y="197.0"></omgdi:waypoint>
<omgdi:waypoint x="240.0" y="197.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="345.0" y="197.0"></omgdi:waypoint>
<omgdi:waypoint x="390.0" y="198.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="430.0" y="198.0"></omgdi:waypoint>
<omgdi:waypoint x="475.0" y="198.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="580.0" y="198.0"></omgdi:waypoint>
<omgdi:waypoint x="625.0" y="199.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="665.0" y="199.0"></omgdi:waypoint>
<omgdi:waypoint x="710.0" y="199.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="815.0" y="199.0"></omgdi:waypoint>
<omgdi:waypoint x="860.0" y="200.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
<omgdi:waypoint x="900.0" y="200.0"></omgdi:waypoint>
<omgdi:waypoint x="945.0" y="200.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
<omgdi:waypoint x="410.0" y="218.0"></omgdi:waypoint>
<omgdi:waypoint x="410.0" y="266.0"></omgdi:waypoint>
<omgdi:waypoint x="510.0" y="267.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
<omgdi:waypoint x="645.0" y="219.0"></omgdi:waypoint>
<omgdi:waypoint x="644.0" y="297.0"></omgdi:waypoint>
<omgdi:waypoint x="292.0" y="296.0"></omgdi:waypoint>
<omgdi:waypoint x="292.0" y="225.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
<omgdi:waypoint x="880.0" y="180.0"></omgdi:waypoint>
<omgdi:waypoint x="880.0" y="133.0"></omgdi:waypoint>
<omgdi:waypoint x="290.0" y="132.0"></omgdi:waypoint>
<omgdi:waypoint x="292.0" y="170.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
程序运行如图,在这里要注意的是,审批结果只能输入 y or Y, n or N
,否则程序就出错了。
项目代码放在Github 上了,进入Github查看 能star 一下或者 fork 就好了
慕课课程链接 点击直接进入