Activiti經過API動態建立流程

我在網上搜到了,對代碼基本未動,能夠參考源代碼來源,下文只是對Demo運行的外圍環境進行了補充。
java

  • Activiti默認配置
    spring

@Rule public ActivitiRule activitiRule = new ActivitiRule();

該代碼會自動加載根目錄下的activiti.cfg.xml;下面是個人activiti.cfg.xml內容:express

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="processEngineConfiguration"
		class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">

		<property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
		<property name="jdbcDriver" value="org.h2.Driver" />
		<property name="jdbcUsername" value="sa" />
		<property name="jdbcPassword" value="" />

		<property name="databaseSchemaUpdate" value="create-drop" />

		<property name="jobExecutorActivate" value="true" />
		<property name="asyncExecutorEnabled" value="true" />
		<property name="asyncExecutorActivate" value="true" />
	</bean>

</beans>

加載該默認配置,還須要引入以下的Maven依賴:apache

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <version>1.4.184</version>
</dependency>

  • activiti-bpmn-layout引入async

源代碼中有以下的代碼
ui

// 2. Generate graphical information
new BpmnAutoLayout(model).execute();

其中的BpmnAutoLayout是須要引入以下的Maven依賴spa

<dependency>
  <groupId>org.activiti</groupId>
  <artifactId>activiti-bpmn-layout</artifactId>
  <version>5.17.0</version>
</dependency>

  • 執行結果code

原文中未給出執行結果,這裏記錄一下:結果文件有diagram.png和process.bpmn20.xml;若是不引入activiti-bpmn-layout,能夠把代碼註釋,那麼diagram.png不能生成,並且process.bpmn20.xml中沒有bpmn2的信息。orm

diagram.png的內容以下:xml

process.bpmn20.xml的內容以下:

<?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:activiti="http://activiti.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.activiti.org/test">
  <process id="my-process" isExecutable="true">
    <startEvent id="start"></startEvent>
    <userTask id="task1" name="First task" activiti:assignee="fred"></userTask>
    <userTask id="task2" name="Second task" activiti:assignee="john"></userTask>
    <endEvent id="end"></endEvent>
    <sequenceFlow id="sequenceFlow-7c412a0a-19b8-49bf-9f75-e752731c04dd" sourceRef="start" targetRef="task1"></sequenceFlow>
    <sequenceFlow id="sequenceFlow-3a5c4815-1b05-40a1-b1b6-f476aa24c360" sourceRef="task1" targetRef="task2"></sequenceFlow>
    <sequenceFlow id="sequenceFlow-6948dd23-85c6-462c-b383-972611c3f425" sourceRef="task2" targetRef="end"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_my-process">
    <bpmndi:BPMNPlane bpmnElement="my-process" id="BPMNPlane_my-process">
      <bpmndi:BPMNShape bpmnElement="task1" id="BPMNShape_task1">
        <omgdc:Bounds height="60.0" width="100.0" x="80.0" y="0.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task2" id="BPMNShape_task2">
        <omgdc:Bounds height="60.0" width="100.0" x="230.0" y="0.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">
        <omgdc:Bounds height="30.0" width="30.0" x="0.0" y="15.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">
        <omgdc:Bounds height="30.0" width="30.0" x="380.0" y="15.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-3a5c4815-1b05-40a1-b1b6-f476aa24c360" id="BPMNEdge_sequenceFlow-3a5c4815-1b05-40a1-b1b6-f476aa24c360">
        <omgdi:waypoint x="180.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="192.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="192.0" y="30.000000000000007"></omgdi:waypoint>
        <omgdi:waypoint x="230.0" y="30.000000000000007"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-6948dd23-85c6-462c-b383-972611c3f425" id="BPMNEdge_sequenceFlow-6948dd23-85c6-462c-b383-972611c3f425">
        <omgdi:waypoint x="330.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="342.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="342.0" y="30.000000000000004"></omgdi:waypoint>
        <omgdi:waypoint x="380.0" y="30.000000000000004"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-7c412a0a-19b8-49bf-9f75-e752731c04dd" id="BPMNEdge_sequenceFlow-7c412a0a-19b8-49bf-9f75-e752731c04dd">
        <omgdi:waypoint x="30.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="42.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="42.0" y="30.000000000000007"></omgdi:waypoint>
        <omgdi:waypoint x="80.0" y="30.000000000000007"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

  • 附上全部源代碼供查找

import java.io.File;
import java.io.InputStream;
import java.util.List;

import org.activiti.bpmn.BpmnAutoLayout;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.UserTask;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.ActivitiRule;
import org.apache.commons.io.FileUtils;

public class ActivitiApiFlowTest {
	
	@Rule
	public ActivitiRule activitiRule = new ActivitiRule();

	@Test
	public void testDynamicDeploy() throws Exception {
		// 1. Build up the model from scratch
		BpmnModel model = new BpmnModel();
		Process process = new Process();
		model.addProcess(process);
		process.setId("my-process");

		process.addFlowElement(createStartEvent());
		process.addFlowElement(createUserTask("task1", "First task", "fred"));
		process.addFlowElement(createUserTask("task2", "Second task", "john"));
		process.addFlowElement(createEndEvent());

		process.addFlowElement(createSequenceFlow("start", "task1"));
		process.addFlowElement(createSequenceFlow("task1", "task2"));
		process.addFlowElement(createSequenceFlow("task2", "end"));

		// 2. Generate graphical information
		new BpmnAutoLayout(model).execute();

		// 3. Deploy the process to the engine
		Deployment deployment = activitiRule.getRepositoryService()
				.createDeployment().addBpmnModel("dynamic-model.bpmn", model)
				.name("Dynamic process deployment").deploy();

		// 4. Start a process instance
		ProcessInstance processInstance = activitiRule.getRuntimeService()
				.startProcessInstanceByKey("my-process");

		// 5. Check if task is available
		List<Task> tasks = activitiRule.getTaskService().createTaskQuery()
				.processInstanceId(processInstance.getId()).list();

		Assert.assertEquals(1, tasks.size());
		Assert.assertEquals("First task", tasks.get(0).getName());
		Assert.assertEquals("fred", tasks.get(0).getAssignee());

		// 6. Save process diagram to a file
		InputStream processDiagram = activitiRule.getRepositoryService()
				.getProcessDiagram(processInstance.getProcessDefinitionId());
		FileUtils.copyInputStreamToFile(processDiagram, new File(
				"target/diagram.png"));

		// 7. Save resulting BPMN xml to a file
		InputStream processBpmn = activitiRule.getRepositoryService()
				.getResourceAsStream(deployment.getId(), "dynamic-model.bpmn");
		FileUtils.copyInputStreamToFile(processBpmn, new File(
				"target/process.bpmn20.xml"));
	}

	protected UserTask createUserTask(String id, String name, String assignee) {
		UserTask userTask = new UserTask();
		userTask.setName(name);
		userTask.setId(id);
		userTask.setAssignee(assignee);
		return userTask;
	}

	protected SequenceFlow createSequenceFlow(String from, String to) {
		SequenceFlow flow = new SequenceFlow();
		flow.setSourceRef(from);
		flow.setTargetRef(to);
		return flow;
	}

	protected StartEvent createStartEvent() {
		StartEvent startEvent = new StartEvent();
		startEvent.setId("start");
		return startEvent;
	}

	protected EndEvent createEndEvent() {
		EndEvent endEvent = new EndEvent();
		endEvent.setId("end");
		return endEvent;
	}
}
相關文章
相關標籤/搜索