activiti 7 + springboot2(五) 部署测试一个流程

一   部署流程定义,贴代码

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.junit.Test;

public class ActivitiTest {

    /**
     * 部署流程
     */
    @Test
    public void deploy(){
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        //创建ProcessEngine对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //得到RepositoryService实例
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //进行部署
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("processes/diagram_1.bpmn")  //添加bpmn资源
                .addClasspathResource("processes/diagram_1.png")
                .name("请假申请单流程")
                .deploy();

        System.out.println(deployment.getName());
        System.out.println(deployment.getId());
    }

}
activiti.cfg.xml 见 springboot2+activiti 7 整合(三) 工程依赖配置和创建表结构
运行完成后,打开数据库查看流程定义数据表ACT_RE_PROCDEF,可以看到 activiti 会将上边代码中指定的 bpm 文件和图片文件保存在数据库中。

 二 启动一个流程实例

/**
* 启动一个流程实例
*/
@Test
public void startProcessInstance(){
ProcessEngine processEngine = getProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance instance = runtimeService.startProcessInstanceByKey("myProcess_1");

System.out.println("流程实例ID:"+instance.getId());
System.out.println("流程定义ID:"+instance.getProcessDefinitionId());
}

public ProcessEngine getProcessEngine(){
//创建ProcessEngineConfiguration对象
ProcessEngineConfiguration configuration = ProcessEngineConfiguration
.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
//创建ProcessEngine对象
return configuration.buildProcessEngine();
}

运行后,打开运行时任务节点表ACT_RU_TASK

流程实例的执行表ACT_RU_EXECUTION

 

    一个流程就启动起来了。

 三 任务查询

流程启动后,各各任务的负责人就可以查询自己当前需要处理的任务,查询出来的任务都是该用户的待办任务

/**
* 查询用户的任务列表
*/
@Test
public void taskQuery() {
ProcessEngine processEngine = getProcessEngine();

TaskService taskService = processEngine.getTaskService();

//根据流程定义的key,负责人assignee来实现当前用户的任务列表查询
List<Task> list = taskService.createTaskQuery()
.processDefinitionKey("myProcess_1")
.taskAssignee("tom")
.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.getCreateTime());
System.out.println("任务的办理人:"+task.getAssignee());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行对象ID:"+task.getExecutionId());
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
System.out.println("getOwner:"+task.getOwner());
System.out.println("getCategory:"+task.getCategory());
System.out.println("getDescription:"+task.getDescription());
System.out.println("getFormKey:"+task.getFormKey());
Map<String, Object> map = task.getProcessVariables();
for (Map.Entry<String, Object> m : map.entrySet()) {
System.out.println("key:" + m.getKey() + " value:" + m.getValue());
}
for (Map.Entry<String, Object> m : task.getTaskLocalVariables().entrySet()) {
System.out.println("key:" + m.getKey() + " value:" + m.getValue());
}

}
}
}

四 完成任务 

    /**
     * 完成任务
     */
    @Test
    public void completeTask(){
        //任务ID
        String taskId = "2505";
        ProcessEngine processEngine = getProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        taskService.complete(taskId);
        System.out.println("完成任务:任务ID:"+taskId);
    }

 依次把部门经理审批和人事审批的流程完成,整个流程走完以后:

查看, 历史任务实例表ACT_HI_TASKINST

查看 ,历史节点表 ACT_HI_ACTINST

源码下载  https://download.csdn.net/download/zsg88/12093061

posted @ 2020-01-09 18:35  猪脚踏浪  阅读(4181)  评论(1编辑  收藏  举报