Activiti (1) - 动态发布工作流

很多实际业务功能需求中, 我们无法一开始预先定义好工作流程, 工作流程在可能需要及时定义, 所以我们就需要动态创建工作流程, 及动态发布.
接着 BPMN 2.0 - Activiti, 开始测试动态发布工作流.

  1. service层方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    @Service public class MyService {
    @Autowired private PersonRepository personRepository;
    @Autowired private RepositoryService repositoryService;
    @Autowired private RuntimeService runtimeService;
    @Autowired private TaskService taskService;

    public void createDynamicWorkflow(String name, String bpmnResourceType, String bpmnXMLString) {
    DeploymentBuilder builder = repositoryService.createDeployment();

    // 加载发布资源
    builder.name(name) // 设置流程显示别名
    .addString(bpmnResourceType, bpmnXMLString); // 设置流程规则文件

    // 发布流程
    builder.deploy();
    }

    public ProcessInstance findProcessInstance(String processInstanceId) {
    return runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
    }

    public List<Task> getTasks(String assignee) {
    return taskService.createTaskQuery().taskAssignee(assignee).list();
    }

    public ProcessInstance startProcess(String processDefinitionKey, String assignee) {
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("person", assignee);

    return runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);
    }
  2. BPMN的XML定义如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    <?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="userDynamicTask1" name="动态任务处理1" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="休假" name="休假" activiti:assignee="${person}"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="休假"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow2" sourceRef="休假" targetRef="endevent1"></sequenceFlow>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_userDynamicTask1">
    <bpmndi:BPMNPlane bpmnElement="userDynamicTask1" id="BPMNPlane_userDynamicTask1">
    <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
    <omgdc:Bounds height="35.0" width="35.0" x="189.5" y="194.5"></omgdc:Bounds>
    </bpmndi:BPMNShape>
    <bpmndi:BPMNShape bpmnElement="休假" id="BPMNShape_休假">
    <omgdc:Bounds height="55.0" width="105.0" x="155.5" y="276.5"></omgdc:Bounds>
    </bpmndi:BPMNShape>
    <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
    <omgdc:Bounds height="35.0" width="35.0" x="191.5" y="398.5"></omgdc:Bounds>
    </bpmndi:BPMNShape>
    <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
    <omgdi:waypoint x="357.0" y="145.0"></omgdi:waypoint>
    <omgdi:waypoint x="357.0" y="180.0"></omgdi:waypoint>
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
    <omgdi:waypoint x="357.0" y="235.0"></omgdi:waypoint>
    <omgdi:waypoint x="357.0" y="270.0"></omgdi:waypoint>
    </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
    </definitions>
  3. 通过代码动态部署工作流

    1
    myService.createDynamicWorkflow("test import xml workflow", "bpmn20.xml", bpmnXML);
  4. 上述代码让流程引擎将工作流动态部署上去了, 下面个我们接着测试运行新部署的工作流:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    @Test public void test2UserTask() {
    // 获取RuntimeService服务对象的实例
    String processDefinitionKey = "userDynamicTask1";

    String assignee = "trademakers";
    ProcessInstance processInstance = myService.startProcess(processDefinitionKey, assignee);
    String processInstanceId = processInstance.getId();
    logger.info("processInstanceId:" + processInstance.getId());

    // 获取TaskService服务对象的实例
    List<Task> tasks = myService.getTasks(assignee);
    Assert.assertEquals(1, tasks.size());

    Task task = tasks.get(0);
    logger.info("taskName:" + task.getName());
    logger.info("taskAssignee:" + task.getAssignee());
    Assert.assertEquals("休假", task.getName());

    // 完成任务
    myService.completeTask(task.getId());

    // 检查结束状态
    ProcessInstance pInstance = myService.findProcessInstance(processInstanceId);
    Assert.assertNull(pInstance);
    logger.info("动态任务处理流程1,使用${流程变量的Key}方式成功执行!");
    }

    测试完成.


参考:

  1. http://www.kafeitu.me/activiti/2013/05/27/dynamic-process-creation-and-deployment-in-100-lines.html