Activiti

Posted mxj961116

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activiti相关的知识,希望对你有一定的参考价值。

/**
 * 管理流程定义
 * @author MA
 */
public class TestProcessDef 
    //得到流程引擎
    private ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();

    /**
     * 部署流程使用classpath
     */
    public void deployProcess01()
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        Deployment deploy = (Deployment) repositoryService.createDeployment().name("请假流程001")
                .addClasspathResource("HelloWorld.bpmn")
                .addClasspathResource("HelloWorld.png")
                .deploy();
        System.out.println("部署成功:流程部署ID:"+((org.activiti.engine.repository.Deployment) deploy).getId());
    

    /**
     * 部署流程使用zip
     * 流程图的文件必须是***.zip结尾
     */
    public void deployProcess02()
        //如果不加/在当前包里找文件;加/代表在根目录里找文件
        InputStream inputStream = this.getClass().getResourceAsStream("***.zip");
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        Deployment deploy = (Deployment) repositoryService.createDeployment().name("请假流程001")
                .addZipInputStream(zipInputStream)//添加流程图流
                .deploy();
        System.out.println("部署成功:流程部署ID:"+((org.activiti.engine.repository.Deployment) deploy).getId());
    

    /**
     * 查询流程部署信息
     */
    @Test
    public void queryProcessDef()
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        //创建部署信息的查询
        String deploymentId = "1";
        Deployment deploy = (Deployment) repositoryService.createDeploymentQuery()
        //条件
        //.deploymentId(deploymentId) //根据部署ID查询
        //.deploymentName(name)   //根据部署名称查询
        //.deploymentTenantId(tenantId)   //根据tenantId
        //.deploymentNameLike(nameLike)   //根据部署名称模糊查询
        //.deploymentTenantIdLike(tenantIdLike)   //根据tentID模糊查询
        //排序
        //.orderByDeploymentId().asc()    //根据部署ID升序
        //orderByDeploymentTime().desc() //根据部署时间降序
        //.orderByDeploymentName()    //根据部署名称升序
        //结果集
        //.list()   //查询返回list集合
        //.listPage(firstResult,maxResults) 分页查询返回list集合
        .singleResult();   //返回单个对象
        //.count()

        System.out.println(deploy);
    

    /**
     * 查询流程定义
     */
    public void queryProcDef()
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        repositoryService.createProcessDefinitionQuery();
        //条件
        //.deploymentId(deploymentId) //根据部署ID查询
        //.deploymentIds(deploymentIds) //根据部署ID的集合查询Set<String> deploymentIds
        //.processDefinitionId(processDefinitionId) //根据流程定义ID查询
        //.processDefinitionIds(processDefinitionIds) //根据流程定义IDS查询
        //.processDefinitionkey(processDefinitionkey) //根据流程定义key查询
        //...
        //排序
        //.orderByDeploymentId()
        //.orderByProcessDefinitionId()
        //.orderByProcessDefinitionKey()
        //.orderByProcessDefinitionName()
        //.orderByProcessDefinitionVersion()
        //结果集
//        .list()
//        .listPage(firstResult,maxResults)
//        .count()
    

    /**
     * 删除流程定义
     */
    public void deleteProcessDef()
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        String deploymentId="2501";
        //根据流程部署id删除流程定义,如果当前id的流程正在进行,会报错
        repositoryService.deleteDeployment(deploymentId);
        //根据流程部署id删除流程定义,如果当前id的流程正在进行,会把正在执行的数据删除
        repositoryService.deleteDeployment(deploymentId,true);
    

    /**
     * 修改流程定义信息
     * 修改流程图后重新部署,只要key不变,版本号就会+1
     */

    /**
     * 查询流程图
     */
    public void viewProcessImg()
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        //根据流程部署ID查询流程定义对象
        String deploymentId="1";
        ProcessDefinition processDefinition = (ProcessDefinition) repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId);
        //从流程定义对象里面查出流程定义ID
        String processDefinitionId=processDefinition.getId();
        InputStream inputStream = repositoryService.getProcessDiagram(processDefinitionId);

        File file=new File("d:/HelloWorld.png");
        try 
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
            int len=0;
            byte[] b=new byte[1024];
            while((len=inputStream.read(b))!=-1)
                outputStream.write(b,0,len);
                outputStream.flush();
            
            outputStream.close();
            inputStream.close();
            System.out.println("查询成功");
         catch (FileNotFoundException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
        

    

    /**
     * 查询最新版本流程定义
     */
    public void queryNewProcessDef() 
        Map<String, ProcessDefinition> map = new HashMap<String, ProcessDefinition>();

        //查询所有的流程定义根据版本号升序
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .orderByProcessDefinitionVersion().asc().list();
        if (null != list && list.size() > 0) 
            for (ProcessDefinition pd : list) 
                map.put(pd.getKey(), pd);
            
        
        //循环map集合
        Collection<ProcessDefinition> values = map.values();
        for (ProcessDefinition pd : values) 
            System.out.println("流程定义ID" + pd.getId());
            //...
        
    

    /**
     * 附加功能,删除流程定义(删除keu相同的所有不同版本的流程定义)
     */
    public void deleteAllSameVersion()
        String processDefKey="HelloWorld";
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        //根据流程定义的key查询流程集合
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionKey(processDefKey).list();

        if(null!=list&&list.size()>0)
            for(ProcessDefinition pd: list)
                repositoryService.deleteDeployment(pd.getDeploymentId(),true);
            
        
    

 

public class TestProcessInstance 
    //得到流程引擎
    private ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();

    /**
     * 部署流程使用classpath
     */
    @Test
    public void deployProcess()
        //得到流程部署service
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        Deployment deploy = (Deployment) repositoryService.createDeployment().name("请假流程001")
                .addClasspathResource("HelloWorld.bpmn")
                .addClasspathResource("HelloWorld.png")
                .deploy();
        System.out.println("部署成功:流程部署ID:" + ((org.activiti.engine.repository.Deployment) deploy).getId());
    

    /**
     * 启动流程
     * 实例开发中使用
     * runtimeService.startProcessInstanceByKey(processDefinitionId,businessKey,variable)
     * runtimeService.startProcessInstanceByKey(processDefinitionId,businessKey)
     */
    public void startProcess()
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
        String processDefinitionId="HelloWorld:1:4";
        runtimeService.startProcessInstanceById(processDefinitionId);

        /**
         * 参数1:流程定义ID
         * 参数2:Map<String,Object> 流程变量
         */
        //runtimeService.startProcessInstanceById(processDefinitionId,variable);

        /**
         * 参数1:流程定义ID
         * 参数2:String 业务ID 把业务ID和流程执行实例进行绑定
         */
        //runtimeService.startProcessInstanceById(processDefinitionId,businessKey);

        /**
         * 参数1:流程定义ID
         * 参数2:String 业务ID 把业务ID和流程实例进行绑定
         * 参数3:Map<String,Object> 流程变量
         */
        //runtimeService.startProcessInstanceById(processDefinitionId,businessKey,variable);

        System.out.println("流程启动成功");

    

    /**
     * 查询我的个人任务
     */
    public void queryMyTask()
        TaskService  taskService = this.processEngine.getTaskService();
        String assignee="张三";
        List<Task> list = taskService.createTaskQuery()
        //条件
        //.taskAssignee(assignee);//根据任务办理人查询任务
        //.deploymentId(deploymentId)//根据部署ID查询
        //.deploymentIdIn(deploymentIds)//根据部署ID查询
        //.executionId(executionId) //根据执行实例ID
        //.processDefinitionId(processDefinitionId)//根据流程定义ID
        //。。。
        //排序
        .orderByTaskCreateTime().desc()
        //结果集
        .list();

        if(null!=list&&list.size()>0)
            for(Task task: list)
                System.out.println("任务ID:"+task.getId());
                System.out.println("任务办理人:"+task.getAssignee());
                System.out.println("执行实例ID:"+task.getExecutionId());
                System.out.println("任务名称:"+task.getName());
                System.out.println("流程定义ID:"+task.getProcessDefinitionId());
                System.out.println("流程实例ID:"+task.getProcessInstanceId());
                System.out.println("任务创建时间:"+task.getCreateTime());
            
        
    

    /**
     * 办理任务
     */
    public void completeTask()
        TaskService taskService = this.processEngine.getTaskService();
        //任务ID
        String taskId="2504";
        //根据任务ID去完成
        //taskService.complete(taskId);
        //根据任务ID去完成任务并指定流程变量
        //taskService.complete(taskId,variables);

        System.out.println("任务完成");
    

    /**
     * 判断流程是否结束
     * 作用:更新业务表里面的状态
     */
    public void isComplete()
        //已知流程实例ID
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
        String processInstanceId = "2501";
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId(processInstanceId).singleResult();
        if(null!=processInstance)
            System.out.println("流程未结束");
        else
            System.out.println("流程已结束");
        


        //已知任务ID
        //根据任务ID查询任务实例对象
        TaskService taskService = this.processEngine.getTaskService();
        String taskId = "5002";
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        //从任务实例里面取出流程实例
        String processInstanceId2 = task.getProcessInstanceId();
        //使用流程实例的ID去流程实例查询有没有数据
        ProcessInstanceQuery processInstance2 = runtimeService.createProcessInstanceQuery()
                .processInstanceId(processInstanceId2);
        if(null!=processInstance2)
            System.out.println("流程未结束");
        else
            System.out.println("流程已结束");
        
    

    /**
     * 查询正在执行的流程实例
     */
    public void queryProcessInstance()
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().list();
        if(null!=list&&list.size()>0)
            for (ProcessInstance pi: list)
                System.out.println("执行实例ID:"+pi.getId());
                System.out.println("流程定义ID:"+pi.getProcessDefinitionId());
                System.out.println("流程实例ID:"+pi.getProcessInstanceId());
            
        
    

    /**
     * 查询历史任务
     */
    public void queryHistoryTask()
        HistoryService historyService = this.processEngine.getHistoryService();
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().list();
        if(null!=list&&list.size()>0)
            for(HistoricTaskInstance hi: list)
                System.out.println("任务ID:"+hi.getId());
                System.out.println("任务办理人:"+hi.getAssignee());
                System.out.println("执行实例ID:"+hi.getExecutionId());
                System.out.println("任务名称:"+hi.getName());
                System.out.println("流程定义ID:"+hi.getProcessDefinitionId());
                System.out.println("流程实例ID:"+hi.getProcessInstanceId());
                System.out.println("任务创建时间:"+hi.getCreateTime());
                System.out.println("任务持续时间:"+hi.getDurationInMillis());
                System.out.println("任务结束时间:"+hi.getEndTime());
            
        
    

    /**
     * 查询历史流程实例
     */
    public void queryHistoryProcessInstance()
        HistoryService historyService = this.processEngine.getHistoryService();
        List<HistoricProcessInstance> list =  historyService.createHistoricProcessInstanceQuery().list();
        if(null!=list && list.size()>0)
            for(HistoricProcessInstance hi: list)
                System.out.println("执行实例ID:"+hi.getId());
                System.out.println("流程定义ID:"+hi.getProcessDefinitionId());
                System.out.println("流程启动时间:"+hi.getStartTime());
            
        
    

 

/**
 * 流程变量测试
 *
 * 相关表:act_ru_variable、avt_hi_varinst
 * @author MA
 */
public class TestProcessVariables 

    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    /**
     * 部署流程使用classpath
     */
    @Test
    public void deployProcess()
        //得到流程部署service
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        Deployment deploy = (Deployment) repositoryService.createDeployment().name("请假流程001")
                .addClasspathResource("HelloWorld.bpmn")
                .addClasspathResource("HelloWorld.png")
                .deploy();
        System.out.println("部署成功:流程部署ID:" + ((org.activiti.engine.repository.Deployment) deploy).getId());
    

    /**
     * 启动流程
     * 实例开发中使用
     */
    public void startProcess()
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
        String processDefinitionKey="HelloWorld";
        //创建流程变量对象
        Map<String,Object> variables = new HashMap<String, Object>();
        variables.put("请假天数",5);
        variables.put("请假原因","生病");
        variables.put("请假时间",new Date());

        runtimeService.startProcessInstanceByKey(processDefinitionKey,variables);


        System.out.println("流程启动成功");

    

    /**
     * 设置流程变量1
     */
    public void setVariables1()
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
        String executionId="2501";
        //runtimeService.setVariable(executionId,"请假人","小明");
        Map<String,Object> variables = new HashMap<String, Object>();
        variables.put("请假天数",5);
        variables.put("请假原因","生病");
        variables.put("请假时间",new Date());
        runtimeService.setVariables(executionId,variables);
    

    /**
     * 设置流程变量2
     */
    public void setVariables2()
        TaskService taskService = this.processEngine.getTaskService();
        String taskId="8507";
        Map<String,Object> variables = new HashMap<String, Object>();
        variables.put("任务ID设置",9);
        taskService.setVariables(taskId,variables);
    

    /**
     * 获取流程变量
     */
    public void getVariables()
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
        String executionId="2501";
        Integer days = (Integer) runtimeService.getVariable(executionId,"请假天数");
        Date date= (Date) runtimeService.getVariable(executionId,"请假时间");
        User user = (User) runtimeService.getVariable(executionId,"用户对象");

    

    /**
     * 查询历史流程变量
     */
    public void getHistoryVariables()
        HistoryService historyService = this.processEngine.getHistoryService();

        //HistoricTaskInstance singleResulr = (HistoricTaskInstance) historyService.createHistoricVariableInstanceQuery().id("2503").singleResult();
        String processInstanceId="2501";
        List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId).list();
        for(HistoricVariableInstance hvs:list)
            System.out.println("ID"+hvs.getId());
            System.out.println("变量值"+hvs.getValue());
            System.out.println("变量名称"+hvs.getVariableName());
            System.out.println("变量类型"+hvs.getVariableTypeName());
        
    


 

/**
 * 查询历史流程实例
 * 查询历史活动
 * 查询历史任务
 * 查询历史流程变量
 * @author MA
 */
public class TestHistoryQuery 

    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    HistoryService historyService = this.processEngine.getHistoryService();

    //查询历史流程实例
    public void historyProcessInstance()
        List<HistoricProcessInstance> list = (List<HistoricProcessInstance>) historyService.createHistoricProcessInstanceQuery();
        //条件
//        .processDefinitionId(processDefinitionId)
//        .processDefinitionKey(processDefinitionKey)
//        .processDefinitionKeyIn(processDefinitionKeys)
//        .processDefinitionName(processDefinitionName)
//        .processDefinitionVersion(processDefinitionVersion)
//        .ProcessInstanceBusinessKey(processInstanceBusinessKey)
//        .processInstanceId(processInstanceIds)
//        .processInstanceIds(processInstanceIds)
        //排序
//        .orderByProcessDefinitionId()
//        .orderByProcessInstanceBusinessKey()
//        .orderByProcessInstanceDuration()
//        .orderByProcessInstanceStartTime()
//        .orderByProcessDefinitionId()
        //结果集
//      .list()
//      .listPage()
//      .count()
//      .singleResult()
        if(null!=list&&list.size()>0)
            for(HistoricProcessInstance hpi:list)
                System.out.println("历史流程实例ID"+hpi.getId());
                System.out.println("流程定义ID"+hpi.getProcessDefinitionId());
                System.out.println("流程流程实例业务ID"+hpi.getBusinessKey());
                System.out.println("流程部署ID"+hpi.getDeploymentId());
                System.out.println("流程定义KEY"+hpi.getProcessDefinitionKey());
                System.out.println("开始活动ID"+hpi.getStartActivityId());
                System.out.println("结束活动ID"+hpi.getEndActivityId());
            
        
    

    //查询历史活动
    public void queryHistoryAct()
        List<HistoricActivityInstance> list = this.historyService.createHistoricActivityInstanceQuery()
        //条件
//        .activityId(actitityId)
//        .activityInstanceId()
//        .activityName()
        //排序
//        .orderByActivityId()
//        .orderByActivityName()
        //结果集
        .list();
        if(null!=list&&list.size()>0)
            for(HistoricActivityInstance hai: list)
                System.out.println("ID"+hai.getId());
                System.out.println("流程定义ID"+hai.getProcessDefinitionId());
                System.out.println("流程实例ID"+hai.getProcessInstanceId());
                System.out.println("执行实例ID"+hai.getExecutionId());
                System.out.println("活动ID"+hai.getActivityId());
                System.out.println("任务ID"+hai.getTaskId());
                System.out.println("活动名称"+hai.getActivityName());
                System.out.println("活动类别"+hai.getActivityType());
                System.out.println("任务办理人"+hai.getAssignee());
                System.out.println("开始时间"+hai.getStartTime());
                System.out.println("结束时间"+hai.getEndTime());
                System.out.println("持续时间"+hai.getId());
            
        
    

    //查询历史任务 act_hi_taskinst
    public void queryHistoryTask()
        List<HistoricTaskInstance> list = (List<HistoricTaskInstance>) this.historyService.createHistoricTaskInstanceQuery();
        //条件
//        .deploymentId(deploymentId)
//        .deploymentIdIn(deploymentIds)
//        .executionId(executionId)
//        .processDefinitionId(processDefinitionId)
//        .processDefinitionKey(processDefinitionKey)
//        .processDefinitionKeyIn(ProcessDefinitionKeys)
//        .processDefinitionKeyLike(processDefinitionKeyLike)
//        .processDefinitionName(processDefinitionName)
//        .processDefinitionNameLike()
        //排序
//        .orderByTaskDefinitionKey()
        //结果集
//        .list();
//        .listPage(firstResult,maxResult)
//        .count()
//        .singleResult()
        if(null!=list&&list.size()>0)
            for(HistoricTaskInstance hti: list)
                System.out.println("任务ID:"+hti.getId());
                System.out.println("任务办理人:"+hti.getAssignee());
                System.out.println("执行实例ID:"+hti.getExecutionId());
                System.out.println("任务名称:"+hti.getName());
                System.out.println("流程定义ID:"+hti.getProcessDefinitionId());
                System.out.println("流程实例ID:"+hti.getProcessInstanceId());
                System.out.println("任务创建时间:"+hti.getCreateTime());
            
        
    

    //查询历史流程变量

 

以上是关于Activiti的主要内容,如果未能解决你的问题,请参考以下文章