同文
购物流程模型化
填写id和name
StartEvent ------------- 开始
UserTask1 ------------- 提交审批信息
decideSubmit ------------- 提交OR取消
tl_approve ------------- 主管审批
decideTLApprove ------------- 主管审批校验
hr_approve ------------- 人事审批
decideHRApprove ------------- 人事审批校验
EndEvent ------------- 结束
点击form ,填写表单字段 (id,name,Tyle,Required)
点击form ,填写表单字段 (id,name,Tyle,Required)
点击form ,填写表单字段 (id,name,Tyle,Required)
在condition填入表达式: ${submitType == "y" || submitType == "Y"}
在condition填入表达式:
${tlApprove == "y" || tlApprove == "Y"}
在condition填入表达式:
${tlApprove == "n" || tlApprove == "N"}
${hrApprove == "y" || hrApprove == "Y"}
${hrApprove == "n" || hrApprove == "N"}
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
com.nn
activiti-test
1.0-SNAPSHOT
org.activiti
activiti-engine
6.0.0
junit
junit
4.11
ch.qos.logback
logback-classic
1.1.11
com.google.guava
guava
23.0
com.h2database
h2
1.3.176
org.apache.maven.plugins
maven-compiler-plugin
8
org.springframework.boot
spring-boot-maven-plugin
${plain}
${encoding}
${log.dir}/file.log
${log.dir}/file.%d{yyyy-MM-dd}.log
30
${std}
${encoding}
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 list = taskService.createTaskQuery().list();
LOGGER.info("待处理任务数量[{}]", list.size());
for (Task task : list) {
LOGGER.info("待处理任务[{}]", task.getName());
Map variables = getMap(processEngine, scanner, task);
taskService.complete(task.getId(), variables);
processInstance = processEngine.getRuntimeService()
.createProcessInstanceQuery()
.processInstanceId(processInstance.getId())
.singleResult();
}
}
scanner.close();
}
private static Map getMap(ProcessEngine processEngine, Scanner scanner, Task task) {
FormService formService = processEngine.getFormService();
TaskFormData taskFormData = formService.getTaskFormData(task.getId());
List formProperties = taskFormData.getFormProperties();
Map 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
activiti.cfg.xml