提示:流程部署后很多种,这里,只讲解自动部署与动态BPMN部署
前言
创建一个 Activit 工作流,并启动这个流程,主要包含2个步骤:
1、定义流程,按照 BPMN 的规范,使用流程定义工具,用流程符号把整个流程描述出来
2、部署流程,把画好的流程定义文件,加载到数据库中,生成表的数据
下面我重点讲解动态BPMN模型部署,不需要流程设计器,直接在代码里实现。
一、自动部署
Activiti7 可以自动部署流程,前提是在 resources 目录下,创建一个新的目录 processes,用来放置 bpmn 文件(*.bpmx20.xml)。
二、动态BPMN模型部署
动态创建 BPMN,需要在程序中指定每一个元素,并给出元素间的关系,最后将这些元素整合为一个完整的 BPMN。(说白了就是把 bpmn 文件用代码来编写,参考 bpmn 文件结构,通过代码动态创建BPMN)
代码例子
代码如下(示例):
public class CeshiTest {
private static final String FILE_SUFFIX = ".bpmn20.xml";//文件Suffix
@Test
public void testDeploymentByBPMN() {
// 1. 创建 BPMN 模型实例
BpmnModel bpmnModel = new BpmnModel();
// 2. 创建流程定义
Process process = new Process();
process.setId("process1");
process.setName("test");
// 3.创建流程节点
createFlowElement(process);
bpmnModel.addProcess(process);
// 4. 部署
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment()
.name("测试申请流程")
.addBpmnModel("ceshi.bpmn20.xml", bpmnModel)
.deploy();
// 5.生成的图形信息
createProcessBpmn(bpmnModel, process.getId(), deploy);
}
/**
* 动态创建流程节点(事件节点)
*/
public void createFlowElement(Process process) {
// 1. 创建开始事件
StartEvent startEvent = new StartEvent();
startEvent.setId("startEvent");
startEvent.setName("创建开始节点");
// 2. 创建用户任务
UserTask userTask = new UserTask();
userTask.setId("userTask1");
userTask.setName("用户任务节点1");
// 3. 创建结束事件
EndEvent endEvent = new EndEvent();
endEvent.setId("endEvent");
endEvent.setName("创建结束节点");
// 4. 定义连接
ArrayList<SequenceFlow> sequenceFlows = new ArrayList<>();
ArrayList<SequenceFlow> toEnd = new ArrayList<>();
SequenceFlow s1 = new SequenceFlow();
s1.setId("sF1");
s1.setName("开始节点--》用户任务节点");
s1.setSourceRef("startEvent");
s1.setTargetRef("userTask1");
sequenceFlows.add(s1);
SequenceFlow s2 = new SequenceFlow();
s2.setId("sF2");
s2.setName("用户任务节点--》结束节点");
s2.setSourceRef("userTask1");
s2.setTargetRef("endEvent");
toEnd.add(s2);
startEvent.setOutgoingFlows(sequenceFlows);
userTask.setOutgoingFlows(toEnd);
userTask.setIncomingFlows(sequenceFlows);
endEvent.setIncomingFlows(toEnd);
process.addFlowElement(startEvent);
process.addFlowElement(s1);
process.addFlowElement(userTask);
process.addFlowElement(s2);
process.addFlowElement(endEvent);
}
/**
* 根据流程图验证流程是否正确
*/
private void createProcessBpmn(BpmnModel model, String processId, Deployment deployment) {
//2. 生成的图形信息
new BpmnAutoLayout(model).execute();
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
//4. 启动一个流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processId);
InputStream processDiagram = repositoryService.getProcessDiagram(processInstance.getProcessDefinitionId());
//7. 保存BPMN.xml到本地文件
InputStream processBpmn = repositoryService.getResourceAsStream(deployment.getId(), processId + FILE_SUFFIX);
try {
FileUtils.copyInputStreamToFile(processDiagram, new File("C:/file/temp/" + processId + ".png"));
FileUtils.copyInputStreamToFile(processBpmn, new File("C:/file/temp/" + processId + FILE_SUFFIX));
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果你对工作流感兴趣,想了解更多,请点击
Java工作流管理系统(activity6.0)