工作流的基本操作
创建数据库表
部署流程定义
流程定义查询
流程定义删除
流程资源文件的查询
package com.hyq;
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngineConfiguration; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.ProcessDefinition; import org.activiti.engine.repository.ProcessDefinitionQuery; import org.junit.Test;
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.List; import java.util.zip.ZipInputStream;
/**
- Created byq on 2017/7/1.
*/ public class Main {
/**获取流程引擎*/ private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); /** * 创建数据库表 */ @Test public void createDb(){ String source = "activiti.cfg.xml"; ProcessEngineConfiguration configuration = ProcessEngineConfiguration. createProcessEngineConfigurationFromResource(source); ProcessEngine processEngine = configuration.buildProcessEngine(); System.out.print(processEngine); } /** * 部署流程定义-方式一 */ @Test public void deployProcess(){ System.out.print(processEngine); RepositoryService repositoryService = processEngine.getRepositoryService(); String bpmn = "/diagram/purchasingflow01.bpmn"; String png = "/diagram/purchasingflow01.png"; InputStream bpmn_inputStream = this.getClass().getResourceAsStream(bpmn); InputStream png_inputStream = this.getClass().getResourceAsStream(png); Deployment deployment = repositoryService.createDeployment() .addInputStream(bpmn, bpmn_inputStream) .addInputStream(png, png_inputStream) .name("我的流程部署") .deploy(); String id = deployment.getId(); String name = deployment.getName(); System.out.print("部署id:"+id); System.out.print("部署name:"+name); } /** * 通过zip压缩包的方式部署 */ @Test public void deployByZip(){ RepositoryService repositoryService = processEngine.getRepositoryService(); InputStream inputStream = this.getClass().getResourceAsStream("/diagram/diagram.zip"); ZipInputStream zipInputStream = new ZipInputStream(inputStream); Deployment deploy = repositoryService.createDeployment() .addZipInputStream(zipInputStream).deploy(); System.out.print(deploy.getId()); } /** * 流程定义的查询 */ @Test public void queryProcessDefinetion(){ RepositoryService repositoryService = processEngine.getRepositoryService(); ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); //设置查询条件:流程定义的id组成:流程定义key+版本号+流水号 // processDefinitionQuery.processDefinitionId("purchasingflow:2:104"); processDefinitionQuery.processDefinitionKey("purchasingflow"); List<ProcessDefinition> processDefinitions = processDefinitionQuery.list(); for(ProcessDefinition pd : processDefinitions){ System.out.println("============================================"); System.out.println("流程定义id:"+pd.getId()); System.out.println("流程部署id:"+pd.getDeploymentId()); System.out.println("流程定义的key:"+pd.getKey()); System.out.println("资源名:"+pd.getDiagramResourceName()); System.out.println("流程定义名称:"+pd.getName()); System.out.println("流程定义的版本:"+pd.getVersion()); } } @Test /** * 流程文件的删除 */ public void deleteProcess(){ RepositoryService repositoryService = processEngine.getRepositoryService(); String processDefinitionId = "purchasingflow:2:104"; ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult(); repositoryService.deleteDeployment(processDefinition.getDeploymentId());
// repositoryService.deleteDeployment(processDefinition.getDeploymentId(),true); }
@Test /** * 流程资源文件的查询 */ public void queryFlowResource() throws Exception { String depolymentId = "1"; RepositoryService repositoryService = processEngine.getRepositoryService(); ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(depolymentId).singleResult(); String resourceName_bpmn =processDefinition.getResourceName(); String resourceName_png =processDefinition.getDiagramResourceName(); InputStream diagramStream = repositoryService.getResourceAsStream(depolymentId,resourceName_bpmn); InputStream resourcStream = repositoryService.getResourceAsStream(depolymentId,resourceName_png); byte[] buf = new byte[1024]; int line = -1; File file_bpmn = new File("d:"+resourceName_bpmn); File file_png = new File("d:"+resourceName_png); FileOutputStream fileOutputStream_bpmn = new FileOutputStream(file_bpmn); FileOutputStream fileOutputStream_png = new FileOutputStream(file_png);
// while((line=resourcStream.read(buf))>0){
// fileOutputStream_bpmn.write(buf,0,line); // } while((line=diagramStream.read(buf,0,1024))!=-1){ fileOutputStream_png.write(buf,0,line); } fileOutputStream_png.flush(); diagramStream.close(); fileOutputStream_png.close(); } }
本文分享 CSDN - 独行侠梦。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。