【Activiti学习之四】Activiti API(三)

环境
  JDK 1.8
  MySQL 5.6
  Tomcat 7
  Eclipse-Luna
  activiti 6.0

一、启动流程

多种方式启动

package com.wjy.pro;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;

public class Start {

    public static void main(String[] args) {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService rs = engine.getRepositoryService();
        // 运行时服务
        RuntimeService runService = engine.getRuntimeService();
        // 任务服务
        TaskService taskService = engine.getTaskService();
        // 部署
        Deployment dep = rs.createDeployment().addClasspathResource("start.bpmn").deploy();
        ProcessDefinition pd = rs.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();
        // 启动流程
        ProcessInstance pi = runService.startProcessInstanceById(pd.getId(), "abc");
        //根据流程文件中定义process节点的id来启动
        //runService.startProcessInstanceByKey("");
        //根据消息名称启动流程  在流程定义的开始事件中加入消息定义 <messageEventDefinition/>
        //runService.startProcessInstanceByMessage("");
       
        System.out.println(pi.getId());
    }

}

二、流程操作与数据查询

1、流程触发-ReceiveTask

package com.wjy.handle;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;

public class ReceiveTaskTest {

    public static void main(String[] args) {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService rs = engine.getRepositoryService();
        // 运行时服务
        RuntimeService runService = engine.getRuntimeService();
        // 任务服务
        TaskService taskService = engine.getTaskService();
        // 部署ReceiveTask类型任务
        Deployment dep = rs.createDeployment().addClasspathResource("receiveTask.bpmn").deploy();
        ProcessDefinition pd = rs.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();
        // 启动流程
        ProcessInstance pi = runService.startProcessInstanceById(pd.getId());
        // 查当前的子执行流(只有一个)
        Execution exe = runService.createExecutionQuery()
                .processInstanceId(pi.getId())
                .onlyChildExecutions()
                .singleResult();

        System.out.println(pi.getId() + ", 当前节点:" + exe.getActivityId());//37505, 当前节点:receivetask1
        
        // 让它往前走  同complete
        runService.trigger(exe.getId());
        
        exe = runService.createExecutionQuery()
                .processInstanceId(pi.getId())
                .onlyChildExecutions()
                .singleResult();
        System.out.println(pi.getId() + ", 当前节点:" + exe.getActivityId());//37505, 当前节点:usertask1
    }

}

2、触发信号事件

messaga

package com.wjy.handle;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;

public class MessageTest {

    public static void main(String[] args) {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService rs = engine.getRepositoryService();
        // 运行时服务
        RuntimeService runService = engine.getRuntimeService();
        // 任务服务
        TaskService taskService = engine.getTaskService();
        // 部署
        Deployment dep = rs.createDeployment()
                .addClasspathResource("MessageEvent.bpmn").deploy();
        ProcessDefinition pd = rs.createProcessDefinitionQuery()
                .deploymentId(dep.getId())
                .singleResult();
        // 启动流程
        ProcessInstance pi = runService.startProcessInstanceById(pd.getId());
        // 查当前的子执行流(只有一个)
        Execution exe = runService.createExecutionQuery()
                .processInstanceId(pi.getId())
                .onlyChildExecutions()
                .singleResult();

        System.out.println(pi.getId() + ", 当前节点:" + exe.getActivityId());
        
        // 等待外面发送的消息才会让它往前走
//        <intermediateCatchEvent id="messageintermediatecatchevent1" name="MessageCatchEvent">
//            <messageEventDefinition messageRef="testMsg"></messageEventDefinition>
//        </intermediateCatchEvent>
        runService.messageEventReceived("testMsg", exe.getId());
        
        exe = runService.createExecutionQuery()
                .processInstanceId(pi.getId())
                .onlyChildExecutions()
                .singleResult();
        System.out.println(pi.getId() + ", 当前节点:" + exe.getActivityId());
    }

}

signal

package com.wjy.handle;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;

public class SingleEventTest {

    public static void main(String[] args) {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService rs = engine.getRepositoryService();
        // 运行时服务
        RuntimeService runService = engine.getRuntimeService();
        // 任务服务
        TaskService taskService = engine.getTaskService();
        // 部署
        Deployment dep = rs.createDeployment()
                .addClasspathResource("SingleEvent.bpmn").deploy();
        ProcessDefinition pd = rs.createProcessDefinitionQuery()
                .deploymentId(dep.getId()).singleResult();
        // 启动流程
        ProcessInstance pi = runService.startProcessInstanceById(pd.getId());
        // 查当前的子执行流(只有一个) createExecutionQuery
        Execution exe = runService.createExecutionQuery()
                .processInstanceId(pi.getId())
                .onlyChildExecutions()
                .singleResult();

        System.out.println(pi.getId() + ", 当前节点:" + exe.getActivityId());
        //等待外面传送定义的信号才会执行
//        <intermediateCatchEvent id="signalintermediatecatchevent1" name="SignalCatchEvent">
//            <signalEventDefinition signalRef="testSignal"></signalEventDefinition>
//        </intermediateCatchEvent>
        runService.signalEventReceived("testSignal");
        
        //流程查询createExecutionQuery
        exe = runService.createExecutionQuery()
                .processInstanceId(pi.getId())
                .onlyChildExecutions()
                .singleResult();

        System.out.println(pi.getId() + ", 当前节点:" + exe.getActivityId());
    }

}

三、工作产生与管理

 1、异步任务

 

<?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="myProcess" name="My process" isExecutable="true">
        <startEvent id="startevent1" name="Start"></startEvent>
        <endEvent id="endevent1" name="End"></endEvent>
        <userTask id="usertask1" name="User Task"></userTask>
        <serviceTask id="servicetask1" name="Service Task" activiti:async="true" 
            activiti:class="org.crazyit.act.c10.MyJavaDelegate"></serviceTask>
        <sequenceFlow id="flow1" sourceRef="startevent1"
            targetRef="servicetask1"></sequenceFlow>
        <sequenceFlow id="flow2" sourceRef="servicetask1"
            targetRef="usertask1"></sequenceFlow>
        <sequenceFlow id="flow3" sourceRef="usertask1"
            targetRef="endevent1"></sequenceFlow>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
        <bpmndi:BPMNPlane bpmnElement="myProcess"
            id="BPMNPlane_myProcess">
            <bpmndi:BPMNShape bpmnElement="startevent1"
                id="BPMNShape_startevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="190.0"
                    y="160.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="endevent1"
                id="BPMNShape_endevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="620.0"
                    y="160.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="usertask1"
                id="BPMNShape_usertask1">
                <omgdc:Bounds height="55.0" width="105.0"
                    x="460.0" y="150.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="servicetask1"
                id="BPMNShape_servicetask1">
                <omgdc:Bounds height="55.0" width="105.0"
                    x="280.0" y="150.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNEdge bpmnElement="flow1"
                id="BPMNEdge_flow1">
                <omgdi:waypoint x="225.0" y="177.0"></omgdi:waypoint>
                <omgdi:waypoint x="280.0" y="177.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow2"
                id="BPMNEdge_flow2">
                <omgdi:waypoint x="385.0" y="177.0"></omgdi:waypoint>
                <omgdi:waypoint x="460.0" y="177.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow3"
                id="BPMNEdge_flow3">
                <omgdi:waypoint x="565.0" y="177.0"></omgdi:waypoint>
                <omgdi:waypoint x="620.0" y="177.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>
package com.wjy.job;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;

public class ServieTaskTest {

    public static void main(String[] args) {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService rs = engine.getRepositoryService();
        // 运行时服务
        RuntimeService runService = engine.getRuntimeService();
        // 任务服务
        TaskService taskService = engine.getTaskService();
        
        Deployment dep = rs.createDeployment().addClasspathResource("service_task.bpmn").deploy();
        ProcessDefinition pd = rs.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();
        
        ProcessInstance pi = runService.startProcessInstanceById(pd.getId());
        System.out.println(pi.getId());
    }

}
package com.wjy.job;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

public class MyJavaDelegate implements JavaDelegate {

    @Override
    public void execute(DelegateExecution arg0) {
        System.out.println("这是处理类");
    }
}

2、定时任务

<?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="myProcess" name="My process" isExecutable="true">
        <startEvent id="startevent1" name="Start"></startEvent>
        <endEvent id="endevent1" name="End"></endEvent>
        <intermediateCatchEvent id="timerintermediatecatchevent1"
            name="TimerCatchEvent">
            <timerEventDefinition>
                <timeDuration>PT1M</timeDuration>
            </timerEventDefinition>
        </intermediateCatchEvent>
        <userTask id="usertask1" name="User Task"></userTask>
        <sequenceFlow id="flow1" sourceRef="startevent1"
            targetRef="timerintermediatecatchevent1"></sequenceFlow>
        <sequenceFlow id="flow2" sourceRef="timerintermediatecatchevent1"
            targetRef="usertask1"></sequenceFlow>
        <sequenceFlow id="flow3" sourceRef="usertask1"
            targetRef="endevent1"></sequenceFlow>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
        <bpmndi:BPMNPlane bpmnElement="myProcess"
            id="BPMNPlane_myProcess">
            <bpmndi:BPMNShape bpmnElement="startevent1"
                id="BPMNShape_startevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="110.0"
                    y="160.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="endevent1"
                id="BPMNShape_endevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="550.0"
                    y="160.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="timerintermediatecatchevent1"
                id="BPMNShape_timerintermediatecatchevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="220.0"
                    y="160.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="usertask1"
                id="BPMNShape_usertask1">
                <omgdc:Bounds height="55.0" width="105.0"
                    x="350.0" y="150.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNEdge bpmnElement="flow1"
                id="BPMNEdge_flow1">
                <omgdi:waypoint x="145.0" y="177.0"></omgdi:waypoint>
                <omgdi:waypoint x="220.0" y="177.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow2"
                id="BPMNEdge_flow2">
                <omgdi:waypoint x="255.0" y="177.0"></omgdi:waypoint>
                <omgdi:waypoint x="350.0" y="177.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow3"
                id="BPMNEdge_flow3">
                <omgdi:waypoint x="455.0" y="177.0"></omgdi:waypoint>
                <omgdi:waypoint x="550.0" y="177.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>
package com.wjy.job;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;

public class TimerTest {

    public static void main(String[] args) {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService rs = engine.getRepositoryService();
        // 运行时服务
        RuntimeService runService = engine.getRuntimeService();
        // 任务服务
        TaskService taskService = engine.getTaskService();
        
        Deployment dep = rs.createDeployment().addClasspathResource("timer_test.bpmn").deploy();
        ProcessDefinition pd = rs.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();
        
        ProcessInstance pi = runService.startProcessInstanceById(pd.getId());
        System.out.println(pi.getId());
    }

}

PT1M:表示1分钟后执行

3、暂停/激活工作

package com.wjy.job;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;

public class SuspendTest {

    public static void main(String[] args) throws Exception {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService rs = engine.getRepositoryService();
        // 运行时服务
        RuntimeService runService = engine.getRuntimeService();
        // 任务服务
        TaskService taskService = engine.getTaskService();
        
        Deployment dep = rs.createDeployment().addClasspathResource("suspend_test.bpmn").deploy();
        ProcessDefinition pd = rs.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();
        
        ProcessInstance pi = runService.startProcessInstanceById(pd.getId());
        System.out.println(pi.getId());
        
        //暂停
        runService.suspendProcessInstanceById(pi.getId());
        //激活
        runService.activateProcessInstanceById(pi.getId());
    }

}

4、异常工作

<?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="myProcess" name="My process" isExecutable="true">
        <startEvent id="startevent1" name="Start"></startEvent>
        <endEvent id="endevent1" name="End"></endEvent>
        <userTask id="usertask1" name="User Task"></userTask>
        <serviceTask id="servicetask1" name="Service Task" 
            activiti:async="true" 
            activiti:class="com.wjy.job.ExceptionDelegate"></serviceTask>
        <sequenceFlow id="flow1" sourceRef="startevent1"
            targetRef="servicetask1"></sequenceFlow>
        <sequenceFlow id="flow2" sourceRef="servicetask1"
            targetRef="usertask1"></sequenceFlow>
        <sequenceFlow id="flow3" sourceRef="usertask1"
            targetRef="endevent1"></sequenceFlow>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
        <bpmndi:BPMNPlane bpmnElement="myProcess"
            id="BPMNPlane_myProcess">
            <bpmndi:BPMNShape bpmnElement="startevent1"
                id="BPMNShape_startevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="190.0"
                    y="160.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="endevent1"
                id="BPMNShape_endevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="620.0"
                    y="160.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="usertask1"
                id="BPMNShape_usertask1">
                <omgdc:Bounds height="55.0" width="105.0"
                    x="460.0" y="150.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="servicetask1"
                id="BPMNShape_servicetask1">
                <omgdc:Bounds height="55.0" width="105.0"
                    x="280.0" y="150.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNEdge bpmnElement="flow1"
                id="BPMNEdge_flow1">
                <omgdi:waypoint x="225.0" y="177.0"></omgdi:waypoint>
                <omgdi:waypoint x="280.0" y="177.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow2"
                id="BPMNEdge_flow2">
                <omgdi:waypoint x="385.0" y="177.0"></omgdi:waypoint>
                <omgdi:waypoint x="460.0" y="177.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow3"
                id="BPMNEdge_flow3">
                <omgdi:waypoint x="565.0" y="177.0"></omgdi:waypoint>
                <omgdi:waypoint x="620.0" y="177.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>

 

package com.wjy.job;

import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Job;
import org.activiti.engine.runtime.ProcessInstance;

public class ErrorServieTaskTest {

    public static void main(String[] args) {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService rs = engine.getRepositoryService();
        // 运行时服务
        RuntimeService runService = engine.getRuntimeService();
        // 任务服务
        TaskService taskService = engine.getTaskService();
        ManagementService managementService = engine.getManagementService();
        
        
        Deployment dep = rs.createDeployment().addClasspathResource("error_task.bpmn").deploy();
        ProcessDefinition pd = rs.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();
        
        ProcessInstance pi = runService.startProcessInstanceById(pd.getId());
        System.out.println(pi.getId());
        
        Job job = managementService.createJobQuery().singleResult();
        managementService.setJobRetries(job.getId(), 1);
        managementService.executeJob(job.getId());
    }

}
package com.wjy.job;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

public class ExceptionDelegate implements JavaDelegate {

    @Override
    public void execute(DelegateExecution arg0) {
        System.out.println("这是处理类");
        throw new RuntimeException("always exception");
    }
}

 

posted @ 2019-09-11 16:03  cac2020  阅读(564)  评论(0编辑  收藏  举报