package com.tabchanj.activiti; import java.util.List; import org.activiti.engine.HistoryService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.history.HistoricActivityInstance; import org.activiti.engine.history.HistoricActivityInstanceQuery; import org.activiti.engine.repository.DeploymentBuilder; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.activiti.engine.task.TaskQuery; import org.junit.Test; /** * 测试流程规则的创建和发布 Activiti整体思路: * 1.获取核心的ProcessEngine对象 2.根据需求,获取对应的服务对象 * 3.使用服务对象的方法完成相应的功能 Activiti的查询思路: 1.获取需求获取对应的服务对象 2.适应服务对象创建查询对象 3.设置查询条件 * 3.使用查询对象进行查找 * * @author tab * */ public class FlowTest { private ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); /** * 创建规则:使用仓库对象,用于存储规则 * * @throws Exception */ @Test public void testDeployProcess() throws Exception { // 1,获取仓库服务对象 RepositoryService rs = engine.getRepositoryService(); // --------完成功能------------------ DeploymentBuilder builder = rs.createDeployment();// 创建部署build对象 // 获取流程规则图 builder.addClasspathResource("LeaveFlow.bpmn"); builder.addClasspathResource("LeaveFlow.png"); // 部署 builder.deploy(); } /** * 开启一个流程 * * @throws Exception */ @Test public void testStartProcess() throws Exception { // 获取启动服务 RuntimeService run = engine.getRuntimeService(); // 开启Process String processDefinitionId = "LeaveFlow:1:4"; ProcessInstance instance = run.startProcessInstanceById(processDefinitionId); System.out.println("instance==" + instance); } @SuppressWarnings("deprecation") @Test public void testQueryTask() throws Exception { //获取任务服务对象 TaskService ts = engine.getTaskService(); //创建查询对象 TaskQuery tq = ts.createTaskQuery(); //设置过滤条件 String assignee = "张三经理"; tq.taskAssignee(assignee).orderByTaskCreateTime().asc(); //执行查询 List<Task> tasks = tq.list(); System.out.println("========================"+assignee+"的任务列表==================="); for (Task task : tasks) { System.out.println("执行人:"+task.getAssignee()+"任务id:"+task.getId()+",任务名称:"+task.getName()+",任务建立时间:"+task.getCreateTime().toLocaleString()); } } /** * 处理任务 * @throws Exception */ @Test public void testExecuteTask() throws Exception { //获取任务服务对象 TaskService ts = engine.getTaskService(); //执行任务 // ts.complete("7502"); ts.complete("10002"); testQueryTask(); } /** * 查询历史任务 * @throws Exception */ @Test public void testHistory() throws Exception { //获取历史服务对象 HistoryService hs = engine.getHistoryService(); //创建历史查询对象 HistoricActivityInstanceQuery ha = hs.createHistoricActivityInstanceQuery(); //设置过滤 String processInstanceId= "2501"; List<HistoricActivityInstance> hiss = ha.processInstanceId(processInstanceId).orderByHistoricActivityInstanceEndTime().desc().list(); System.out.println(hiss.size()); for (HistoricActivityInstance historicActivityInstance : hiss) { System.out.println(historicActivityInstance); } } }
时间: 2024-11-07 02:05:06