activiti学习笔记(五) 流程部署

时间:2022-05-03
本文章向大家介绍activiti学习笔记(五) 流程部署,主要内容包括activiti流程部署、activiti部署前提、activiti部署过程、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

activiti流程部署

activit部署方法api

    activiti共有六种部署方式,最终实现都是DeploymentEntityImpl的addResource方法

public interface DeploymentBuilder {

  //根据流进行部署,获取bpmn文件的流进行部署,resourceName是资源名可以自定义
  DeploymentBuilder addInputStream(String resourceName, InputStream inputStream);

  //通过文件路径进行部署
  DeploymentBuilder addClasspathResource(String resource);

  //根据文本字符串进行部署,文本字符串可以是读取文件的,也可以自己手写
  DeploymentBuilder addString(String resourceName, String text);

  //根据字节码尽心部署
  DeploymentBuilder addBytes(String resourceName, byte[] bytes);

  //根据zip流进行部署
  DeploymentBuilder addZipInputStream(ZipInputStream zipInputStream);

  //根据BpmnModel实例进行部署
  DeploymentBuilder addBpmnModel(String resourceName, BpmnModel bpmnModel);
}

activiti部署前提

       预设某些情况有了bpmn文件,首先我们需要repositoryService这个实例,接下来进行部署使用的都是这个实例

@Before
	public void init() {
		processEngine = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource(
                        "com/activiti_study/ch10/activiti1.cfg.xml")
				.buildProcessEngine();

		repositoryService = processEngine.getRepositoryService();
	}

activiti部署过程

zip流部署

       这种方式是把其压缩成zip文件,然后进行部署

        @Test
	public void testAddZipInputStream() throws Exception {
		InputStream in = App.class.getClassLoader().getResourceAsStream(
                "com/activiti_study/ch10/common.zip");
		ZipInputStream zipInputStream = new ZipInputStream(in);
		DeploymentBuilder deployment = repositoryService.createDeployment();
		deployment.addZipInputStream(zipInputStream).deploy();

	}

普通流部署

    根据资源文件路径,获取其流,进行部署

        @Test
	public void testAddInputStream() throws Exception {
		DeploymentBuilder deployment = repositoryService.createDeployment();

		InputStream inputStream = App.class.getClassLoader()
				.getResource("com/activiti_study/ch10/common.bpmn")
				.openStream();
		deployment.addInputStream(resourceName, inputStream).deploy();
	}

根据资源文件路径进行部署

        @Test
	public void testAddClasspathResource() {
		DeploymentBuilder deployment = repositoryService.createDeployment();
		deployment.addClasspathResource(
                "com/activiti_study/ch10/common.bpmn").deploy();
	}

根据bpmnModel实例进行部署

        @Test
	public void testAddBpmnModel() {
		DeploymentBuilder deployment = repositoryService.createDeployment();
		BpmnModel bpmnModel = getBpmnModel();
		deployment.addBpmnModel(resourceName, bpmnModel).deploy();
	}
        private BpmnModel getBpmnModel() {
		BpmnModel bpmnModel = new BpmnModel();
		// 连线1
		SequenceFlow flow1 = new SequenceFlow();
		flow1.setId("flow1");
		flow1.setName("开始节点-->任务节点1");
		flow1.setSourceRef("start1");
		flow1.setTargetRef("userTask1");

		// 连线2
		SequenceFlow flow2 = new SequenceFlow();
		flow2.setId("flow2");
		flow2.setName("任务节点1-->任务节点2");
		flow2.setSourceRef("userTask1");
		flow2.setTargetRef("userTask2");

		// 连线3
		SequenceFlow flow3 = new SequenceFlow();
		flow3.setId("flow3");
		flow3.setName("任务节点2-->结束节点");
		flow3.setSourceRef("userTask2");
		flow3.setTargetRef("endEvent");
		// 开始节点
		StartEvent start = new StartEvent();
		start.setId("start1");
		start.setName("开始节点");
		start.setOutgoingFlows(Arrays.asList(flow1));
		// 任务节点1
		UserTask userTask1 = new UserTask();
		userTask1.setId("userTask1");
		userTask1.setName("任务节点1");
		userTask1.setIncomingFlows(Arrays.asList(flow1));
		userTask1.setOutgoingFlows(Arrays.asList(flow2));

		// 任务节点2
		UserTask userTask2 = new UserTask();
		userTask2.setId("userTask2");
		userTask2.setName("任务节点2");
		userTask2.setIncomingFlows(Arrays.asList(flow2));
		userTask2.setOutgoingFlows(Arrays.asList(flow3));
		//结束节点
		EndEvent endEvent = new org.activiti.bpmn.model.EndEvent();
		endEvent.setId("endEvent");
		endEvent.setName("结束节点");
		endEvent.setIncomingFlows(Arrays.asList(flow3));
		
		Process process=new Process();
		process.setId("process1");
		process.addFlowElement(start);
		process.addFlowElement(flow1);
		process.addFlowElement(flow2);
		process.addFlowElement(flow3);
		process.addFlowElement(userTask1);
		process.addFlowElement(userTask2);
		process.addFlowElement(endEvent);
		bpmnModel.addProcess(process);
		return bpmnModel;
	}








根据纯文本进行部署
        @Test
	public void testAddSrtring() {
		DeploymentBuilder deployment = repositoryService.createDeployment();
		String text = readTxtFile("E:/activitilearing/activiti-study/src/main/java/com/shareniu/activiti_study/ch10/common.bpmn");
		deployment.addString(resourceName, text).deploy();
	}


        public static String readTxtFile(String filePath) {
		StringBuffer stringBuffer = new StringBuffer();
		InputStreamReader read = null;
		try {
			String encoding = "UTF-8";// UTF-8编码
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				read = new InputStreamReader(new FileInputStream(file),
						encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					stringBuffer.append(lineTxt);
				}
				return stringBuffer.toString();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
		} finally {
			try {
				read.close();
			} catch (IOException e) {
			}
		}
		return "";
	}

 
activiti部署后表数据
       a)     act_re_deployment
      存放流程定义的显示名和部署时间,每部署一次增加一条记录
 
    b)    act_re_procdef
      存放流程定义的属性信息,部署每个新的流程定义都会在这张表中增加一条记录。
 
    c)    act_ge_bytearray
      存储流程定义相关的部署信息。即流程定义文档的存放地。每部署一次就会增加两条记录,一条是关于bpmn规则文件的,一条是图片的(如果部署时只指定了bpmn一个文件,activiti会在部署时解析bpmn文件内容自动生成流程图)。两个文件不是很大,都是以二进制形式存储在数据库中。