flowable基础二,
package com.fzybh.test; import com.sun.corba.se.impl.ior.OldJIDLObjectKeyTemplate; import org.flowable.engine.*; import org.flowable.engine.history.HistoricActivityInstance; import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration; import org.flowable.engine.repository.Deployment; import org.flowable.engine.repository.ProcessDefinition; import org.flowable.engine.repository.ProcessDefinitionQuery; import org.flowable.engine.runtime.ProcessInstance; import org.flowable.task.api.Task; import org.junit.Before; import org.junit.jupiter.api.Test; import sun.awt.geom.AreaOp; import java.util.HashMap; import java.util.List; import java.util.Map; public class test03 { ProcessEngineConfiguration configuration = null; @Before public void before(){ //获取信息 configuration = new StandaloneProcessEngineConfiguration(); //配置相关数据库的连接信息 configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver"); configuration.setJdbcUsername("root"); configuration.setJdbcPassword("123456"); configuration.setJdbcUrl("jdbc:mysql://localhost:3306/flowable-learn?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"); configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); //通过ProessEngineConfiguration 构建我们需求的processEngine // ProcessEngine processEngine = configuration.buildProcessEngine(); //System.out.println("processEngine:"+processEngine); } /** * */ @Test public void testProcessEngine(){ //获取信息 before(); //通过ProessEngineConfiguration 构建我们需求的processEngine ProcessEngine processEngine = configuration.buildProcessEngine(); System.out.println("processEngine:"+processEngine); } /** * 布置流程 */ @Test public void testDeploy(){ //1.获取 processEngine 对象 ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration(); //配置相关数据库的连接信息 configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver"); configuration.setJdbcUsername("root"); configuration.setJdbcPassword("123456"); configuration.setJdbcUrl("jdbc:mysql://localhost:3306/flowable-learn?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"); configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); ProcessEngine processEngine = configuration.buildProcessEngine(); //2.获取RepositoryService 服务 processEngine对象下面服务(RepositoryService,RuntimeService,TaskService,HistoryService) RepositoryService repositoryService =processEngine.getRepositoryService(); //3.完成流程的部署操作 Deployment deployment = repositoryService.createDeployment() .addClasspathResource("MyHoliday.bpmn20.xml")//.bar .name("XX请假流程") .deploy(); System.out.println("deployment getID()"+deployment.getId()); System.out.println("deployment getName()"+deployment.getName()); } /** * 查询流程定义的信息(act_re_deployment) */ @Test public void testDeployQuery(){ before(); ProcessEngine processEngine = configuration.buildProcessEngine(); System.out.println("processEngine:"+processEngine); RepositoryService repositoryService = processEngine.getRepositoryService(); //1.方法一 //ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); //ProcessDefinition processDefinition = processDefinitionQuery.deploymentId("1").singleResult(); //2.方法二 ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery() .deploymentId("17501").singleResult(); System.out.println("processDefinition.getDeploymentId():"+processDefinition.getDeploymentId()); System.out.println("processDefinition.getName():"+processDefinition.getName()); System.out.println("processDefinition.getDescription():"+processDefinition.getDescription()); System.out.println("processDefinition.getId():"+processDefinition.getId()); } /** * 删除流程实例 */ @Test public void testDelDeploy(){ before(); ProcessEngine processEngine = configuration.buildProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); //删除部署的流程 第一个参数是ID,如果部署的流程启动了就不允许删除了 //repositoryService.deleteDeployment("1"); //第二个参数是级联删除,如果流程启动了,相关的任务一并删除掉(act_re_deployment id=2501) repositoryService.deleteDeployment("2501",true); } /** * 启动一个流程实例 */ @Test public void testRunProcess(){ before(); ProcessEngine processEngine = configuration.buildProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); //构建流程实例变量 Map<String,Object> varivale = new HashMap<>(); varivale.put("employee","user"); varivale.put("nrOfHolidays",3); varivale.put("deciption","工作累了,出去玩玩"); //启动流程实例 ProcessInstance holidaysRequest = runtimeService.startProcessInstanceByKey("MyHolidayUI"); System.out.println("holidayRequest holidaysRequest.getProcessDefinitionId():"+holidaysRequest.getProcessDefinitionId()); System.out.println("holidayRequest holidaysRequest.getActivityId():"+holidaysRequest.getActivityId()); System.out.println("holidayRequest holidaysRequest.getId()任务id:"+holidaysRequest.getId()); } /** * 测试,user1:任务查询 * */ @Test public void testQueryTask(){ before(); ProcessEngine processEngine = configuration.buildProcessEngine(); TaskService taskService = processEngine.getTaskService(); List<Task> list=taskService.createTaskQuery() .processDefinitionKey("MyHolidayUI")//指定查询流程编程 .taskAssignee("user1") .list(); for(Task task:list){ System.out.println("任务id :"+ task.getProcessDefinitionId()); System.out.println("任务task.getName() :"+ task.getName()); System.out.println("任务getAssignee() :"+ task.getAssignee()); System.out.println("task.getDescription() = " + task.getDescription()); System.out.println("task.getId() = " + task.getId()); } /** * 任务id :MyHolidayUI:1:17504 * 任务task.getName() :提交请假流程 * 任务getAssignee() :user1 * task.getDescription() = null * task.getId() = 20005 */ } /** * 完成当任务(不同,) */ @Test public void testCompleteTask(){ before(); ProcessEngine processEngine = configuration.buildProcessEngine(); TaskService taskService = processEngine.getTaskService(); //找到任务 Task task = taskService.createTaskQuery() .processDefinitionKey("MyHolidayUI") .taskAssignee("user2") .singleResult(); //创建流程变量 Map<String, Object> map = new HashMap(); map.put("approved",false); taskService.complete(task.getId());//, map } /** * 查看历史信息 act_hi_actinst 表 */ @Test public void test(){ before(); ProcessEngine processEngine = configuration.buildProcessEngine(); HistoryService historyService = processEngine.getHistoryService(); List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery() .processDefinitionId("MyHolidayUI:1:17504")//查询的历史记录的状态,是已经完成的获取活跃实例对象 .finished() .orderByHistoricActivityInstanceEndTime().asc() .list(); for(HistoricActivityInstance history:list){ System.out.println("========================"); System.out.println("history.getActivityId() = " + history.getActivityId()); System.out.println("history.getProcessDefinitionId() = " + history.getProcessDefinitionId()); System.out.println("时间 = " + history.getDurationInMillis()+"ms"); System.out.println("处理人 = " + history.getAssignee()); } } }
MyHoliday.bpmn20.xml -BPMN文件
<?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:flowable="http://flowable.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.flowable.org/processdef"> <process id="MyHolidayUI" name="MyHolidayUI" isExecutable="true"> <documentation>XX请假流程</documentation> <startEvent id="startEvent1" flowable:formFieldValidation="true"></startEvent> <userTask id="sid-2172ECB1-BDB1-4BB3-B119-6B2DBA7DD439" name="提交请假流程" flowable:assignee="user1" flowable:formFieldValidation="true"> <extensionElements> <modeler:activiti-idm-assignee xmlns:modeler="http://flowable.org/modeler"><![CDATA[true]]></modeler:activiti-idm-assignee> <modeler:assignee-info-email xmlns:modeler="http://flowable.org/modeler"><![CDATA[578749037@qq.com]]></modeler:assignee-info-email> <modeler:assignee-info-firstname xmlns:modeler="http://flowable.org/modeler"><![CDATA[user1]]></modeler:assignee-info-firstname> <modeler:assignee-info-lastname xmlns:modeler="http://flowable.org/modeler"><![CDATA[guo]]></modeler:assignee-info-lastname> <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete> </extensionElements> </userTask> <userTask id="sid-4F9E4071-77CB-4A27-8BA9-3FC137ACBF03" name="总经理审核请假流程" flowable:assignee="user2" flowable:formFieldValidation="true"> <extensionElements> <modeler:activiti-idm-assignee xmlns:modeler="http://flowable.org/modeler"><![CDATA[true]]></modeler:activiti-idm-assignee> <modeler:assignee-info-email xmlns:modeler="http://flowable.org/modeler"><![CDATA[user2@qq.com]]></modeler:assignee-info-email> <modeler:assignee-info-firstname xmlns:modeler="http://flowable.org/modeler"><![CDATA[user2]]></modeler:assignee-info-firstname> <modeler:assignee-info-lastname xmlns:modeler="http://flowable.org/modeler"><![CDATA[u]]></modeler:assignee-info-lastname> <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete> </extensionElements> </userTask> <endEvent id="sid-BD3113F8-7D29-4218-9ABA-DB037D708337"></endEvent> <sequenceFlow id="sid-12B852B9-347E-441E-8A49-3EA9D773AB63" sourceRef="sid-4F9E4071-77CB-4A27-8BA9-3FC137ACBF03" targetRef="sid-BD3113F8-7D29-4218-9ABA-DB037D708337"></sequenceFlow> <sequenceFlow id="sid-F701A2D8-D2C9-45FA-B13A-6E8E1B2F8684" sourceRef="sid-2172ECB1-BDB1-4BB3-B119-6B2DBA7DD439" targetRef="sid-4F9E4071-77CB-4A27-8BA9-3FC137ACBF03"></sequenceFlow> <sequenceFlow id="sid-C8671643-4841-47E1-8332-D29135E7B9F0" sourceRef="startEvent1" targetRef="sid-2172ECB1-BDB1-4BB3-B119-6B2DBA7DD439"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_MyHoliday"> <bpmndi:BPMNPlane bpmnElement="MyHoliday" id="BPMNPlane_MyHoliday"> <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1"> <omgdc:Bounds height="30.0" width="30.0" x="90.0" y="145.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="sid-2172ECB1-BDB1-4BB3-B119-6B2DBA7DD439" id="BPMNShape_sid-2172ECB1-BDB1-4BB3-B119-6B2DBA7DD439"> <omgdc:Bounds height="80.0" width="100.0" x="240.0" y="120.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="sid-4F9E4071-77CB-4A27-8BA9-3FC137ACBF03" id="BPMNShape_sid-4F9E4071-77CB-4A27-8BA9-3FC137ACBF03"> <omgdc:Bounds height="80.0" width="100.0" x="510.0" y="120.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="sid-BD3113F8-7D29-4218-9ABA-DB037D708337" id="BPMNShape_sid-BD3113F8-7D29-4218-9ABA-DB037D708337"> <omgdc:Bounds height="28.0" width="28.0" x="690.0" y="146.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="sid-12B852B9-347E-441E-8A49-3EA9D773AB63" id="BPMNEdge_sid-12B852B9-347E-441E-8A49-3EA9D773AB63"> <omgdi:waypoint x="609.9499999999827" y="160.0"></omgdi:waypoint> <omgdi:waypoint x="690.0" y="160.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="sid-C8671643-4841-47E1-8332-D29135E7B9F0" id="BPMNEdge_sid-C8671643-4841-47E1-8332-D29135E7B9F0"> <omgdi:waypoint x="119.94999946593475" y="160.0"></omgdi:waypoint> <omgdi:waypoint x="239.9999999999298" y="160.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="sid-F701A2D8-D2C9-45FA-B13A-6E8E1B2F8684" id="BPMNEdge_sid-F701A2D8-D2C9-45FA-B13A-6E8E1B2F8684"> <omgdi:waypoint x="339.9499999999288" y="160.0"></omgdi:waypoint> <omgdi:waypoint x="510.0" y="160.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>