【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

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

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

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

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

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

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、异步任务

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

 

2、定时任务

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

 

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

上一篇:windows 修改Administrator管理员账户名


下一篇:SIA-GateWay之API网关安装部署指南