1、添加Activiti Maven依赖;
<!-- ==============================activiti=========================== --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>5.17.0</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>5.17.0</version> </dependency> <!-- activiti需要log4j日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.6</version> </dependency>
2、Spring集成Activiti;
<!-- ===================================activiti配置=================================== --> <!-- activiti配置 --> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> <!-- 配置事务管理器,统一事务 --> <property name="transactionManager" ref="transactionManager" /> <!-- 设置建表策略 --> <property name="databaseSchemaUpdate" value="true" /> <property name="jobExecutorActivate" value="false" /> <property name="asyncExecutorEnabled" value="true" /> <property name="asyncExecutorActivate" value="false" /> </bean> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration" /> </bean> <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" /> <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" /> <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" /> <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" /> <bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
3、由于使用了log4j,在src/main/resources目录下添加log4j配置文件log4j.properties;
log4j.rootLogger=WARN, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
4、添加bpmn流程配置文件Interview.bpmn;
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" targetNamespace="Examples" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL http://www.omg.org/spec/BPMN/2.0/20100501/BPMN20.xsd"> <process id="Interview" name="某公司2012年实习生招聘流程"> <documentation>招聘工作流程</documentation> <startEvent id="start" name="实习生招聘启动"/> <userTask id="bishi" name="笔试" activiti:candidateGroups="人力资源部"/> <sequenceFlow id="flow1" name="" sourceRef="start" targetRef="bishi"/> <userTask id="yimian" name="技术一面" activiti:candidateGroups="技术部"/> <sequenceFlow id="flow2" name="" sourceRef="bishi" targetRef="yimian"/> <userTask id="ermian" name="技术二面" activiti:candidateGroups="技术部"/> <sequenceFlow id="flow3" name="" sourceRef="yimian" targetRef="ermian"/> <userTask id="hrmian" name="HR面" activiti:candidateGroups="人力资源部"/> <sequenceFlow id="flow4" name="" sourceRef="ermian" targetRef="hrmian"/> <userTask id="luyong" name="录用,发放Offer" activiti:candidateGroups="人力资源部"/> <sequenceFlow id="flow5" name="" sourceRef="hrmian" targetRef="luyong"/> <endEvent id="end" name="实习生招聘结束"/> <sequenceFlow id="flow6" name="" sourceRef="luyong" targetRef="end"/> </process> </definitions>
5、添加Activiti测试类;
package com.coshaho.learn.service.test; import java.util.List; import org.activiti.engine.HistoryService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.history.HistoricProcessInstance; import org.activiti.engine.task.Task; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ActivitiTest { public static void main(String[] args) { @SuppressWarnings("resource") ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml"}); ProcessEngine processEngine = (ProcessEngine)ac.getBean("processEngine"); // 发布流程 RepositoryService repositoryService = processEngine.getRepositoryService(); repositoryService.createDeployment().addClasspathResource("Interview.bpmn").deploy(); // 启动流程实例 RuntimeService runtimeService = processEngine.getRuntimeService(); String processId = runtimeService.startProcessInstanceByKey("Interview").getId(); System.out.println("此次启动的流程实例为:" + processId); // 获取人力资源任务 TaskService taskService = processEngine.getTaskService(); List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("人力资源部").list(); System.out.println("******人力资源部任务列表******"); for (Task task : tasks) { if("笔试".equals(task.getName())) { System.out.println("张三领取笔试任务:" + task.getId()); taskService.claim(task.getId(), "张三"); } else if("HR面".equals(task.getName())) { System.out.println("李四领取HR面任务:" + task.getId()); taskService.claim(task.getId(), "李四"); } else { System.out.println("王五领取发放Offer任务:" + task.getId()); taskService.claim(task.getId(), "王五"); } } System.out.println("张三的笔试任务数量:"+taskService.createTaskQuery().taskAssignee("张三").count()); tasks = taskService.createTaskQuery().taskAssignee("张三").list(); for (Task task : tasks) { System.out.println("张三处理笔试任务, id:"+task.getId()); taskService.complete(task.getId()); } System.out.println("李四的HR面任务数量:"+taskService.createTaskQuery().taskAssignee("李四").count()); tasks = taskService.createTaskQuery().taskAssignee("李四").list(); for (Task task : tasks) { System.out.println("李四处理HR面任务, id:"+task.getId()); taskService.complete(task.getId()); } System.out.println("王五的发放Offer任务数量:"+taskService.createTaskQuery().taskAssignee("王五").count()); tasks = taskService.createTaskQuery().taskAssignee("王五").list(); for (Task task : tasks) { System.out.println("王五处理发放Offer任务, id:"+task.getId()); taskService.complete(task.getId()); } tasks = taskService.createTaskQuery().taskCandidateGroup("技术部").list(); System.out.println("******技术部任务列表******"); for (Task task : tasks) { if("技术一面".equals(task.getName())) { System.out.println("马六领取技术一面任务:" + task.getId()); taskService.claim(task.getId(), "马六"); } else { System.out.println("胜七领取技术二面任务:" + task.getId()); taskService.claim(task.getId(), "胜七"); } } System.out.println("马六的技术一面任务数量:"+taskService.createTaskQuery().taskAssignee("马六").count()); tasks = taskService.createTaskQuery().taskAssignee("马六").list(); for (Task task : tasks) { System.out.println("马六处理技术一面任务, id:"+task.getId()); taskService.complete(task.getId()); } System.out.println("胜七的技术二面任务数量:"+taskService.createTaskQuery().taskAssignee("胜七").count()); tasks = taskService.createTaskQuery().taskAssignee("胜七").list(); for (Task task : tasks) { System.out.println("胜七处理技术二面任务, id:"+task.getId()); taskService.complete(task.getId()); } HistoryService historyService = processEngine.getHistoryService(); List<HistoricProcessInstance> historicProcessInstanceList = historyService .createHistoricProcessInstanceQuery().list(); if(null != historicProcessInstanceList && 0 != historicProcessInstanceList.size()) { for(HistoricProcessInstance hisInstance : historicProcessInstanceList) { System.out.println("历史流程, id = "+hisInstance.getId() + ", end time = " + hisInstance.getEndTime()); } } } }
6、运行测试类,可得到如下结果。
此次启动的流程实例为:7504 ******人力资源部任务列表****** 王五领取发放Offer任务:5015 李四领取HR面任务:5023 张三领取笔试任务:7507 张三的笔试任务数量:1 张三处理笔试任务, id:7507 李四的HR面任务数量:1 李四处理HR面任务, id:5023 王五的发放Offer任务数量:1 王五处理发放Offer任务, id:5015 ******技术部任务列表****** 胜七领取技术二面任务:5020 马六领取技术一面任务:7513 马六的技术一面任务数量:1 马六处理技术一面任务, id:7513 胜七的技术二面任务数量:1 胜七处理技术二面任务, id:5020 历史流程, id = 2501, end time = null 历史流程, id = 4, end time = Sun Dec 27 18:31:14 COT 2015 历史流程, id = 5004, end time = null 历史流程, id = 7504, end time = null
同时,我们可以查看数据库,Activiti引擎自动创建以ACT开头的Activiti业务表。
时间: 2024-10-13 00:02:37