package activitiTest; 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; /** * activiti基础操作 */ public class ActTest_jichucaozuo { // ProcessEngines.getDefaultProcessEngine()会在第一次调用时 // 初始化并创建一个流程引擎,以后再调用就会返回相同的流程引擎。 // 使用对应的方法可以创建和关闭所有流程引擎:ProcessEngines.init() 和 // ProcessEngines.destroy()。 private ProcessEngine processEngine = ProcessEngines .getDefaultProcessEngine(); /** * 简单非空判断 * * @author:tuzongxun * @Title: isEmpty * @param @return * @return boolean * @date Feb 29, 2016 12:55:15 PM * @throws */ public boolean isEmpty(Object object) { if (object instanceof List) { List list = (List) object; if (list != null && list.size() > 0) { return false; } else { return true; } } else { if (object != null) { return false; } else { return true; } } } /** * 部署流程定义(发布流程) * * @author:tuzongxun * @Title: actDeployement * @param * @return void * @date Feb 29, 2016 12:23:48 PM * @throws */ public void actDeployement() { // 使用zip文件形式部署流程定义 InputStream inputStream = this.getClass().getClassLoader() .getResourceAsStream("actTest1.zip"); ZipInputStream zipInputStream = new ZipInputStream(inputStream); processEngine.getRepositoryService().createDeployment() .name("activiti测试").addZipInputStream(zipInputStream).deploy(); } /** * 删除流程定义 * * @author:tuzongxun * @Title: deleteDeployement * @param * @return void * @date Feb 29, 2016 12:36:15 PM * @throws */ public void deleteDeployement() { List<Deployment> lists = processEngine.getRepositoryService() .createDeploymentQuery().list(); if (!isEmpty(lists)) { for (Deployment deployment : lists) { processEngine.getRepositoryService().deleteDeployment( deployment.getId()); } } } /** * 启动流程实例 * * @author:tuzongxun * @Title: startProcessInstance * @param * @return void * @date Feb 29, 2016 12:30:06 PM * @throws */ public void startProcessInstance() { // 这里根据流程定义的key启动,也可以根据id,还可以在启动的时候加入流程变量, // 启动流程实例后会获得一个任务task,这里是在流程图中已经写死了任务所有者是张三,因此启动的时候会创建一个任务给张三 String processDefinitionKey = "myProcess"; processEngine.getRuntimeService().startProcessInstanceByKey( processDefinitionKey); } /** * 查询当前活动的流程实例 * * @author:tuzongxun * @Title: findCurrentProInstance * @param * @return void * @date Feb 29, 2016 12:53:04 PM * @throws */ public void findCurrentProInstance() { List<ProcessInstance> lists = processEngine.getRuntimeService() .createProcessInstanceQuery().list(); if (!isEmpty(lists)) { for (ProcessInstance processInstance : lists) { System.out.println(processInstance.getId()); } } } /** * 查询个人任务及相关信息 * * @author:tuzongxun * @Title: findMyTask * @param * @return void * @date Feb 29, 2016 12:50:05 PM * @throws */ public void findMyTask() { // String userName = "张三"; String userName = "李四"; List<Task> lists = processEngine.getTaskService().createTaskQuery() .taskAssignee(userName).list(); if (!isEmpty(lists)) { for (Task task : lists) { System.out.println(task.getId() + "," + task.getName() + "," + task.getAssignee() + "," + task.getCreateTime()); } } } /** * 完成个人任务 * * @author:tuzongxun * @Title: endMyTask * @param * @return void * @date Feb 29, 2016 1:17:21 PM * @throws */ public void endMyTask() { String taskId = "5002"; processEngine.getTaskService().complete(taskId); } /** * 查询历史流程实例 * * @author:tuzongxun * @Title: findHisProInstance * @param * @return void * @date Feb 29, 2016 2:09:14 PM * @throws */ public void findHisProInstance() { List<HistoricProcessInstance> lists = processEngine.getHistoryService() .createHistoricProcessInstanceQuery().list(); if (!isEmpty(lists)) { for (HistoricProcessInstance hisPro : lists) { System.out.println(hisPro.getId() + "," + hisPro.getStartTime() + "," + hisPro.getEndTime()); } } } /** * 查询历史任务列表 * * @author:tuzongxun * @Title: findHisTask * @param * @return void * @date Feb 29, 2016 2:13:05 PM * @throws */ public void findHisTask() { List<HistoricTaskInstance> lists = processEngine.getHistoryService() .createHistoricTaskInstanceQuery().list(); if (!isEmpty(lists)) { for (HistoricTaskInstance hisTask : lists) { System.out.println(hisTask.getId() + "," + hisTask.getAssignee() + "," + hisTask.getName() + "," + hisTask.getStartTime() + "," + hisTask.getEndTime()); } } } }