Activiti 流程实例、任务的执行

package com.activiti.test;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

public class ProcessInstanceTest {

    //获取流程引擎,默认加载类路径下的activiti.cfg.xml文件
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 部署流程定义
     */
    @Test
    public void deploy() {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("diagrams/leave.zip");
        ZipInputStream zipInputStream = new ZipInputStream(in);
        Deployment deploy = processEngine.getRepositoryService()
                .createDeployment()
                .name("请假流程")
                .addZipInputStream(zipInputStream)
                .deploy();
        System.out.println("部署ID:" + deploy.getId());
        System.out.println("部署名称:" + deploy.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void startProcessInstance() {
        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("Leave");
        System.out.println("流程定义的ID:" + pi.getProcessDefinitionId());
        System.out.println("流程实例的ID:" + pi.getId());
    }

    /**
     * 查询个人任务
     */
    @Test
    public void queryPersonalTask() {
        String assignee = "张三";
        List list = processEngine.getTaskService()
                .createTaskQuery()
                .taskAssignee(assignee)
                .list();
        if (list != null && list.size() > 0) {
            for (Task task : list) {
                System.out.println("任务ID:" + task.getId());
                System.out.println("任务名称:" + task.getName());
                System.out.println("任务办理人:" + task.getAssignee());
                System.out.println("任务的创建时间:"+task.getCreateTime());
                System.out.println("流程定义ID:" + task.getProcessDefinitionId());
                System.out.println("流程实例ID:" + task.getProcessInstanceId());
                System.out.println("执行对象ID:" + task.getExecutionId());
                System.out.println("===========================================");
            }
        }
    }

    /**
     * 完成任务
     */
    @Test
    public void complete() {
        String taskId = "32502";
        processEngine.getTaskService().complete(taskId);
        System.out.println("完成任务");
    }

    /**
     * 查看流程状态
     */
    @Test
    public void queryProcessState() {
        String processInstanceId = "37501";
        //通过流程实例的ID查询流程实例
        ProcessInstance pi = processEngine.getRuntimeService()
                .createProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();
        if (pi != null) {
            System.out.println("当前流程在:" + pi.getActivityId());
        } else {
            System.out.println("流程已结束");
        }
    }

    /**
     * 查看历史任务
     */
    @Test
    public void queryHistoryTask() {
        String assignee = "张三"; //历史任务办理人
        List list = processEngine.getHistoryService()
                .createHistoricTaskInstanceQuery()//创建历史任务查询
                .taskAssignee(assignee)//指定办理人
                .list();
        if (list != null && list.size() > 0) {
            for (HistoricTaskInstance historicTaskInstance : list) {
                System.out.println(historicTaskInstance.getStartTime()+"\n"+historicTaskInstance.getEndTime()+"\n"+historicTaskInstance.getDurationInMillis());
                System.out.println("任务ID:" + historicTaskInstance.getId());
                System.out.println("任务名称:" + historicTaskInstance.getName());
                System.out.println("流程实例ID:" + historicTaskInstance.getProcessInstanceId());
                System.out.println("流程定义ID:" + historicTaskInstance.getProcessDefinitionId());
                System.out.println("执行对象ID:" + historicTaskInstance.getExecutionId());
                System.out.println("任务办理人:" + historicTaskInstance.getAssignee());
                System.out.println("==========================================================");
            }
        }
    }

    /**
     * 查看历史流程实例
     */
    @Test
    public void queryHistoryProcessInstance() {
        String processInstanceId = "37501";
        HistoricProcessInstance hpi = processEngine.getHistoryService()
                .createHistoricProcessInstanceQuery()//创建历史流程实例查询
                .processInstanceId(processInstanceId)
                .singleResult();
        System.out.println("流程定义ID:"+hpi.getProcessDefinitionId());
        System.out.println("流程实例ID:"+hpi.getId());
        System.out.println(hpi.getStartTime()+"    "+hpi.getEndTime()+"        "+hpi.getDurationInMillis());
    }
}

你可能感兴趣的:(activiti,工作流)