2017.2.20 activiti实战--第二章--搭建Activiti开发环境及简单示例(二)简单示例
学习资料:《Activiti实战》
第一章 搭建Activiti开发环境及简单示例
2.5 简单流程图及其执行过程
(1)leave.bpmn
后缀名必须是bpmn。安装了activiti的插件(eclipse:activiti designer,intellij:actiBPM)后,就可以用设计器的形式进行流程的绘制。
注意:在activiti部署的时候,必须同时存在leave.bpmn及leave.png。因此在eclipse设置:window—>preferences—>activiti->save->选中create process definition image when saving the diagram
绘制完leave.bpmn后,就会自动生成对应的png文件。(intellij的插件actiBPM还没有找到设置的地方)
(2)leave.xml
eclipse中还可以以xml形式查看(目前还没有找到intellij中如何以xml格式查看,而且intellij中activiti插件的使用明显比eclipse中使用的用户体验差)。对应的xml文件如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> 3 <process id="leave" name="Leave"> 4 <documentation>请假流程</documentation> 5 <startEvent id="startevent1" name="Start"></startEvent> 6 <userTask id="deptLeaveAudit" name="领导审批" activiti:assignee="leader"></userTask> 7 <userTask id="hrAudit" name="人事审批" activiti:assignee="hr"></userTask> 8 <userTask id="reportBack" name="销假" activiti:assignee="${proposer}"></userTask> 9 <endEvent id="endevent1" name="End"></endEvent> 10 <sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="deptLeaveAudit"></sequenceFlow> 11 <sequenceFlow id="flow2" name="" sourceRef="deptLeaveAudit" targetRef="hrAudit"></sequenceFlow> 12 <sequenceFlow id="flow3" name="" sourceRef="hrAudit" targetRef="reportBack"></sequenceFlow> 13 <sequenceFlow id="flow4" name="" sourceRef="reportBack" targetRef="endevent1"></sequenceFlow> 14 </process> 15 </definitions>
(3)测试代码
获取processEngine --》 获取各种Service --》部署bpmn文件 --》 创建流程定义 --》 执行流程定义 --》 获取某个用户拥有的task --》用户签收该task --》用户完成该task --》 查看历史信息
1 public class SayHelloToLeaveTest { 2 3 @Test 4 public void testStartProcess() throws Exception { 5 ProcessEngine processEngine = ProcessEngineConfiguration 6 .createStandaloneInMemProcessEngineConfiguration() 7 .buildProcessEngine(); 8 9 RepositoryService repositoryService = processEngine.getRepositoryService(); 10 String bpmnFileName = "me/kafeitu/activiti/helloworld/SayHelloToLeave.bpmn"; 11 repositoryServic.createDeployment()
.addInputStream("SayHelloToLeave.bpmn",this.getClass().getClassLoader().getResourceAsStream(bpmnFileName))
.deploy(); 17 18 ProcessDefinition processDefinition = repositoryService 19 .createProcessDefinitionQuery().singleResult(); 20 assertEquals("SayHelloToLeave", processDefinition.getKey()); 21 22 RuntimeService runtimeService = processEngine.getRuntimeService(); 23 24 Map<String, Object> variables = new HashMap<String, Object>(); 25 variables.put("applyUser", "employee1"); 26 variables.put("days", 3); 27 28 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("SayHelloToLeave", variables); 30 assertNotNull(processInstance); 31 System.out.println("pid=" + processInstance.getId() + ", pdid=" 32 + processInstance.getProcessDefinitionId()); 33 34 TaskService taskService = processEngine.getTaskService(); 35 Task taskOfDeptLeader = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult(); 37 assertNotNull(taskOfDeptLeader); 38 assertEquals("领导审批", taskOfDeptLeader.getName()); 39 40 taskService.claim(taskOfDeptLeader.getId(), "leaderUser"); 41 variables = new HashMap<String, Object>(); 42 variables.put("approved", true); 43 taskService.complete(taskOfDeptLeader.getId(), variables); 44 45 taskOfDeptLeader = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult(); 47 assertNull(taskOfDeptLeader); 48 49 HistoryService historyService = processEngine.getHistoryService(); 50 long count = historyService.createHistoricProcessInstanceQuery().finished().count(); 52 assertEquals(1, count); 53 } 54 }
2.6 Activiti Explorer
Activiti Explorer是activiti为了让开发人员快速入门所设计的示例程序。
查看之前下载的activiti-5.22.0.zip,可以看到:
(1)运行explorer
将activiti-explorer.war包部署到tomcat下。访问地址:http://localhost:8080/activiti-explorer。
默认有一个账号:kermit/kermit
登录进去后的页面:(详细的使用参看用户手册)
(2)修改explorer的参数
没做任何修改的explorer,db.properties默认的h2数据库,如果要用自己的数据库,还要修改db.properties。
然后把databse里的create文件夹里的sql文件,在自己的数据库里执行一遍。执行完后,表结构如图。
(3)使用explorer(存在中文乱码问题)