一、 创建流程规则,如下图
二、发布流程
1 /** 2 * 2、发布一个流程 3 */ 4 @Test 5 public void testDeployProcess() { 6 RepositoryService repositoryService = processEngine.getRepositoryService(); 7 DeploymentBuilder builder = repositoryService.createDeployment(); 8 // 加载发布资源 9 builder.name("机器自动执行流程测试") // 设置流程显示别名 10 .addClasspathResource("receiveTask.bpmn") // 设置流程规则文件 11 .addClasspathResource("receiveTask.png"); // 设置流程规则的图片 12 // 发布流程 13 builder.deploy(); 14 }
三、启动流程,并自动执行任务
1 /** 2 * 3、启动流程,并自动执行任务 3 */ 4 @Test 5 public void testStartProcess() { 6 // 获取服务对象的实例 7 RuntimeService runtimeService = processEngine.getRuntimeService(); 8 9 String processDefinitionKey = "receiveTask"; 10 // 自动执行与Key相对应的流程的最高版本 11 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey); 12 13 String processInstanceId = processInstance.getId(); 14 15 logger.info("Id:" + processInstance.getId()); 16 17 //获取当前流程下Execution对象 18 Execution e1 = runtimeService 19 .createExecutionQuery() 20 .processInstanceId(processInstanceId) //每个流程的唯一标识 21 .activityId("汇总当天销售额") //每个活动的唯一标识 22 .singleResult(); 23 Assert.assertNotNull(e1); 24 //自动逻辑的绑定-汇总当天销售额,进行一堆逻辑后,得到金额money 25 Double money = 888888.88d; 26 //把金额放入变量中 27 runtimeService.setVariable(e1.getId(), "销售额", money); 28 logger.info("汇总后当天的销售额是: " + money); 29 30 //推动流程流转 31 runtimeService.signal(e1.getId()); 32 33 //判断当前流程,是否仪在一个节点ID是“发短信给老板”的活动节点 34 Execution e2 = runtimeService 35 .createExecutionQuery() 36 .processInstanceId(processInstanceId) //每个流程的唯一标识 37 .activityId("发短信给老板") //每个活动的唯一标识 38 .singleResult(); 39 Assert.assertNotNull(e2); 40 Double m = (Double) runtimeService.getVariable(e2.getId(), "销售额"); 41 logger.info("老板,今天的销售额是:" +m); 42 43 //推动流程流转 44 runtimeService.signal(e2.getId()); 45 46 //检查结束状态 47 ProcessInstance pInstance = runtimeService 48 .createProcessInstanceQuery() 49 .processInstanceId(processInstanceId) 50 .singleResult(); 51 Assert.assertNull(pInstance); 52 logger.info("机器自动任务流程成功执行!"); 53 }
四、执行后的结果如下:
[INFO][2015-12-01 17:34:46,411][com.mcs.activiti.test.TestReceiveTask]Id:60001
[INFO][2015-12-01 17:34:46,570][com.mcs.activiti.test.TestReceiveTask]汇总后当天的销售额是: 888888.88
[INFO][2015-12-01 17:34:46,625][com.mcs.activiti.test.TestReceiveTask]老板,今天的销售额是:888888.88
[INFO][2015-12-01 17:34:46,783][com.mcs.activiti.test.TestReceiveTask]机器自动任务流程成功执行!