flowable学习二流程部署,定义,删除流程实例,启动一个流程实例,任务查询,完成当任务

package com.fzybh.test;

import com.sun.corba.se.impl.ior.OldJIDLObjectKeyTemplate;
import org.flowable.engine.*;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import sun.awt.geom.AreaOp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class test01 {


    ProcessEngineConfiguration configuration = null;

    @Before
    public void before(){
        //获取信息
        configuration =  new StandaloneProcessEngineConfiguration();
        //配置相关数据库的连接信息
        configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
        configuration.setJdbcUsername("root");
        configuration.setJdbcPassword("123456");
        configuration.setJdbcUrl("jdbc:mysql://localhost:3306/flowable-learn?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);


        //通过ProessEngineConfiguration 构建我们需求的processEngine
        // ProcessEngine processEngine = configuration.buildProcessEngine();
        //System.out.println("processEngine:"+processEngine);
    }


    @Test
    public void testProcessEngine(){

        //获取信息
        before();

        //通过ProessEngineConfiguration 构建我们需求的processEngine
        ProcessEngine processEngine = configuration.buildProcessEngine();
        System.out.println("processEngine:"+processEngine);

    }


    /**
     * 布置流程
     */
    @Test
    public void testDeploy(){
        //1.获取 processEngine 对象
        ProcessEngineConfiguration configuration =  new StandaloneProcessEngineConfiguration();
        //配置相关数据库的连接信息
        configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
        configuration.setJdbcUsername("root");
        configuration.setJdbcPassword("123456");
        configuration.setJdbcUrl("jdbc:mysql://localhost:3306/flowable-learn?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //2.获取RepositoryService 服务  processEngine对象下面服务(RepositoryService,RuntimeService,TaskService,HistoryService)
        RepositoryService repositoryService =processEngine.getRepositoryService();
        //3.完成流程的部署操作
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("holiday-request.bpmn20.xml")
                .name("请假流程")
                .deploy();
        System.out.println("deployment getID()"+deployment.getId());
        System.out.println("deployment getName()"+deployment.getName());
    }

    /**
     * 查询流程定义的信息
     */
    @Test
    public void testDeployQuery(){
        before();
        ProcessEngine processEngine = configuration.buildProcessEngine();
        System.out.println("processEngine:"+processEngine);
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //1.方法一
        //ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        //ProcessDefinition processDefinition = processDefinitionQuery.deploymentId("1").singleResult();
        //2.方法二
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId("1").singleResult();
        System.out.println("processDefinition.getDeploymentId():"+processDefinition.getDeploymentId());
        System.out.println("processDefinition.getName():"+processDefinition.getName());
        System.out.println("processDefinition.getDescription():"+processDefinition.getDescription());
        System.out.println("processDefinition.getId():"+processDefinition.getId());
    }

    /**
     * 删除流程实例
     */
    @Test
    public void testDelDeploy(){
        before();
        ProcessEngine processEngine = configuration.buildProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //删除部署的流程 第一个参数是ID,如果部署的流程启动了就不允许删除了
        //repositoryService.deleteDeployment("1");
        //第二个参数是级联删除,如果流程启动了,相关的任务一并删除掉(act_re_deployment id=2501)
        repositoryService.deleteDeployment("2501",true);

    }

    /**
     * 启动一个流程实例
     */
    @Test
    public void testRunProcess(){
        before();
        ProcessEngine processEngine = configuration.buildProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //构建流程实例变量
        Map<String,Object> varivale = new HashMap<>();
        varivale.put("employee","张三");
        varivale.put("nrOfHolidays",3);
        varivale.put("deciption","工作累了,出去玩玩");
        //启动流程实例
        ProcessInstance holidaysRequest = runtimeService.startProcessInstanceByKey("holidayRequest",varivale);
        System.out.println("holidayRequest holidaysRequest.getProcessDefinitionId():"+holidaysRequest.getProcessDefinitionId());
        System.out.println("holidayRequest holidaysRequest.getActivityId():"+holidaysRequest.getActivityId());
        System.out.println("holidayRequest holidaysRequest.getId()任务id:"+holidaysRequest.getId());
    }

    /**
     * 测试,张三:任务查询
     */
    @Test
    public void testQueryTask(){
        before();
        ProcessEngine processEngine = configuration.buildProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        List<Task> list=taskService.createTaskQuery()
                .processDefinitionKey("holidayRequest")//指定查询流程编程
                .taskAssignee("zhangsan")
                .list();
        for(Task task:list){
            System.out.println("任务id :"+ task.getProcessDefinitionId());
            System.out.println("任务task.getName() :"+ task.getName());
            System.out.println("任务getAssignee() :"+ task.getAssignee());
            System.out.println("task.getDescription() = " + task.getDescription());
            System.out.println("task.getId() = " + task.getId());

        }

    }

    /**
     * 完成当任务(不同,)
     */
    @Test
    public void testCompleteTask(){
        before();
        ProcessEngine processEngine = configuration.buildProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //找到任务
        Task task = taskService.createTaskQuery()
                .processDefinitionKey("holidayRequest")
                .taskAssignee("zhangsan")
                .singleResult();
        //创建流程变量
        Map<String, Object> map = new HashMap();
        map.put("approved",false);
        taskService.complete(task.getId(), map);

    }



}

  

posted @ 2022-03-29 17:25  A汉克先生  阅读(1109)  评论(0编辑  收藏  举报