将这两个文件压缩成zip格式的压缩包
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; import org.junit.Test; import java.io.InputStream; import java.util.zip.ZipInputStream; public class Test2 { @Test public void deploymentByZip(){ //获取processEngine对象 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取一个RepositoryService对象 RepositoryService repositoryService = processEngine.getRepositoryService(); //定义一个输入流,加载Zip文件 InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("bpmn/holiday.zip"); //定义一个ZIPInputStream对象 ZipInputStream zipInputStream=new ZipInputStream(inputStream); //流程部署 Deployment deploy = repositoryService.createDeployment() .addZipInputStream(zipInputStream) .name("请求审批流程") .key("holidayKey") .deploy(); System.out.println("流程名称:"+deploy.getName()); } }
二:启动流程实例
@Test //启动流程实例 public void startProcessInstance() { RuntimeService runtimeService = processEngine.getRuntimeService(); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_1"); System.out.println(processInstance.getName()); System.out.println(processInstance.getDeploymentId()); System.out.println(processInstance.getActivityId()); }
三:流程定义的查看
@Test public void getProDef(){ //获取processEngine对象 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取一个RepositoryService对象 RepositoryService repositoryService = processEngine.getRepositoryService(); //得到流程定义查看对象ProcessDefinitionQuery ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); //指定查看的流程 processDefinitionKey()指定流程定义的key值 orderByProcessDefinitionVersion根据流程定义版本号排序 List<ProcessDefinition> holiday = processDefinitionQuery.processDefinitionKey("holiday").orderByProcessDefinitionVersion().desc().list(); for (ProcessDefinition proDef:holiday) { System.out.println("流程定义ID:"+proDef.getId()); System.out.println("流程定义的名称:"+proDef.getName()); System.out.println("流程定义的Key:"+proDef.getKey()); System.out.println("流程定义的版本号:"+proDef.getVersion()); } }
四:删除流程
/** * 删除流程: * 假设在删除时,当前正在有该流程实例执行,那么会导致删除失败 * 如果强制要求,则可以使用repositoryService.deleteDeployment("1",true); true代表级联删除 */ @Test public void deleteDeployment(){ //获取processEngine对象 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取一个RepositoryService对象 RepositoryService repositoryService = processEngine.getRepositoryService(); //删除流程 repositoryService.deleteDeployment("32501",true); }
五:从数据库读取资源
/** * 从数据中读取资源文件到本地 */ @Test public void getResource() throws IOException { //获取processEngine对象 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取一个RepositoryService对象 RepositoryService repositoryService = processEngine.getRepositoryService(); //获取到流程查询对象 ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); //获取到流程资源 processDefinitionQuery.processDefinitionKey("myProcess_1"); //获取单一结果 ProcessDefinition processDefinition = processDefinitionQuery.singleResult(); //流程部署的ID String deploymentId = processDefinition.getDeploymentId(); //获取到bpmnResource InputStream bpmnStream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName()); //获取到png InputStream pngStream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName()); //构建输出流 OutputStream bpmnOut = new FileOutputStream("F:\\study\\IDEAY2\\MyTest\\Active\\Active\\Day01\\src\\main\\resources\\" + processDefinition.getResourceName()); OutputStream pngOut = new FileOutputStream("F:\\study\\IDEAY2\\MyTest\\Active\\Active\\Day01\\src\\main\\resources\\" + processDefinition.getDiagramResourceName()); //将数据输出到磁盘当中 IOUtils.copy(bpmnStream,bpmnOut); IOUtils.copy(pngStream,pngOut); //关流 pngOut.close(); bpmnOut.close(); pngStream.close(); bpmnStream.close(); }
六:启动流程实例和业务系统关联
/** * 启动流程实例和业务系统关联 * 正常应该先添加数据到业务表当中,拿到当前添加数据的主键ID,通过启动流程实例,将主键ID交给BuessniessKkey * 代表和流程系统进行关联 */ @Test public void startInstance(){ //获取processEngine对象 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); //生成流程 ProcessInstance holiday = runtimeService.startProcessInstanceByKey("myProcess_1","2"); //获取一个TaskService对象 TaskService taskService = processEngine.getTaskService(); List<Task> list = taskService.createTaskQuery().taskAssignee("ty").processDefinitionKey("myProcess_1").list(); //任务审批 for (Task task:list){ taskService.complete(task.getId()); } }
七:流程挂起
/** * 所有流程定义的挂起 */ @Test public void allSuspend(){ //获取processEngine对象 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取repositoryService对象 RepositoryService repositoryService = processEngine.getRepositoryService(); //获取流程定义 ProcessDefinition holiday = repositoryService.createProcessDefinitionQuery().processDefinitionKey("myProcess_1").singleResult(); //获取到当前流程定义是否为暂停状态 suspended方法为true代表为暂停 false就是运行的 boolean suspended = holiday.isSuspended(); if(suspended){ repositoryService.activateProcessDefinitionById(holiday.getId(),true,null); System.out.println("该流程定义激活"); }else{ repositoryService.suspendProcessDefinitionById(holiday.getId(),true,null); System.out.println("该流程定义暂停"); } }
八:单个流程的挂起
/** * 单个流程实例的挂起 */ @Test public void singleSuspend(){ //获取processEngine对象 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取RuntimeService对象 RuntimeService runtimeService = processEngine.getRuntimeService(); ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId("7501").singleResult(); //获取到当前流程定义是否为暂停状态 suspended方法为true代表为暂停 false就是运行的 boolean suspended = processInstance.isSuspended(); if(suspended){ runtimeService.activateProcessInstanceById("7501"); System.out.println("xx的请假流程激活"); }else{ runtimeService.suspendProcessInstanceById("7501"); System.out.println("xx的请假流程挂起"); } }
原文:https://www.cnblogs.com/yjc1605961523/p/12621223.html