Activiti-流程实例

1.什么是流程实例

  参与者(可以是用户也可以是程序)按照流程定义内容发起一个流程,这就是一个流程实例,是动态的;

  流程定义和流程实例的图解:

    

 2.启动流程实例

  流程定义部署在activiti后,就可以在系统中通过activiti去管理该流程的执行,执行流程表示流程的一次执行;比如部署系统请假流程后,如果某用户要申请请假这时就需要执行这个流程,如果,另外一个用户也要申请请假则也需要执行该流程,每个执行互不影响,每个执行是单独的流程实例;

package com.wn;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;

/*启动流程实例
*`act_hi_actinst`           已经开始和执行完毕的活动信息
*`act_hi_identitylink`      历史参与者信息
*`act_hi_procinst`          流程实例信息
*`act_hi_taskinst`          历史任务实例
*`act_ru_execution`         任务执行信息
*`act_ru_identitylink`      当前任务执行参与者
 *`act_ru_task`             任务信息
*
* */
public class StartInstance {
    public static void main(String[] args) {
        //获取ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取RuntimeService对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //创建流程实例
        ProcessInstance hoilday = runtimeService.startProcessInstanceByKey("hoilday");
        //输出实例信息
        System.out.println("获取流程部署ID:"+hoilday.getDeploymentId());
        System.out.println("获取流程实例ID:"+hoilday.getId());
        System.out.println("获取流程活动ID:"+hoilday.getActivityId());
    }
}

3.BusinessKey(业务标识)

  启动流程实例时,执行的businessKey就会在act_ru_execution流程实例的执行表中存储businessKey;

  BusinessKey:业务标识,通常为业务表的主键,业务标识和流程实例一一对应;业务标识来源于业务系统;存储业务标识就是根据业务标识来关联查询业务系统的数据;

  比如:请假流程启动一个流程实例,就可以将请假单的id作为业务标识存储到activiti中,将来查询activiti的流程实例信息就可以获取请假单的id而从关联查询业务系统数据库得到请假单的信息;

package com.wn.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;

/*启动流程实例和业务系统关联*/
public class StartInstance {
    public static void main(String[] args) {

        //获取ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取一个RuntimeService对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //生成流程
        runtimeService.startProcessInstanceByKey("hoilday","1");

        //通过流程 提交请假审批
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery().taskAssignee("zhangsan").processDefinitionKey("hoilday").singleResult();
        //任务审批
        taskService.complete(task.getId());
    }
}

    

4.查询流程实例

  流程在运行过程中可以查询流程实例的状态,当前运行结点等信息;

package com.wn.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;

import java.util.List;

public class QueryProcessInstance {
    public static void main(String[] args) {
        //获取processEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //流程定义
        String processDefinitionKeu="hoilday";
        //获取RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        List<ProcessInstance> list=runtimeService
                .createProcessInstanceQuery()
                .processDefinitionKey(processDefinitionKeu)
                .list();
        for (ProcessInstance processInstance:list){
            System.out.println("    ");
            System.out.println("流程实例id:"
                    + processInstance.getProcessInstanceId()); System.out.println("所属流程定义id:"
                    + processInstance.getProcessDefinitionId());
            System.out.println("是否执行完成:" + processInstance.isEnded());
            System.out.println("是否暂停:" + processInstance.isSuspended());
            System.out.println("当前活动表示:"+processInstance.getActivityId());
        }
    }
}

5.挂起,激活流程实例

  某些情况可能由于流程变更需要将当前运行的流程暂停而不是直接删除,流程暂停后将不会执行;

  5.1 全部流程实例挂起

    操作流程定义为挂起状态,giant流程定义下边所有的流程实例全部暂停;

    流程定义为挂起装填该流程定义将不允许启动新的流程实例,同时该流程定义下所有的流程实例将全部挂起暂停执行;

package com.wn.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;

/*所有流程实例挂起*/
public class AllSuspend {
    public static void main(String[] args) {
        //获取ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //获取流程定义
        ProcessDefinition hoilday = repositoryService.createProcessDefinitionQuery().processDefinitionKey("hoilday").singleResult();
        //获取到当前流程定义是否为暂停状态 suspended方法为true是暂停的,suspended方法为false是运行的
        boolean suspended = hoilday.isSuspended();
        if (suspended){
            repositoryService.activateProcessDefinitionById(hoilday.getId(),true,null);
            System.out.println("该流程定义激活");
        }else{
            repositoryService.suspendProcessDefinitionById(hoilday.getId(),true,null);
            System.out.println("该流程定义暂停");
        }
    }
}

  5.2 单个流程实例挂起

    操作流程实例对象,针对单个流程执行挂起操作,某个流程实例挂起则此流程不在继续执行,完成该流程实例的当前任务将报异常;

package com.wn.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;

/*单个流程实例挂起*/
/*act_hi_taskinst*/
public class SingleSuspend {
    public static void main(String[] args) {
        //获取processEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取RuntimeService对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId("32501").singleResult();
        //获取到当前流程定义是否为暂停状态   suspended方法为true代表为暂停   false就是运行的
        boolean suspended = processInstance.isSuspended();
        if(suspended){
            runtimeService.activateProcessInstanceById("32501");
            System.out.println("请假流程激活");
        }else{
            runtimeService.suspendProcessInstanceById("32501");
            System.out.println("请假流程挂起");
        }
    }
}
原文地址:https://www.cnblogs.com/wnwn/p/12619783.html