工作流——activiti
阿新 • • 發佈:2018-02-13
pid man -a @service web tis png sage cli
1.導入依賴
<!-- activiti工作流 --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>5.14</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>5.14</version> </dependency>
2.配置xml
a.新建spring-activiti.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <property name="activityFontName" value="微軟雅黑"></property> <!-- 引入druid數據源 --> <property name="dataSource" ref="dataSource"></property> <!-- 引入spring事務管理 --> <property name="transactionManager" ref="txManager"></property> <!-- 建表策略 --> <property name="databaseSchemaUpdate" value="true"></property> <!-- 歷史控制級別 --> <property name="history" value="full"></property> <!-- 自動部署 --> <property name="deploymentResources"> <list> <value>classpath*:MyProcess.bpmn</value> </list> </property> </bean> <bean id="processEngineFactoryBean" class="org.activiti.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration"></property> </bean> <bean id="repositoryService" factory-bean="processEngineFactoryBean" factory-method="getRepositoryService"></bean> <bean id="runtimeService" factory-bean="processEngineFactoryBean" factory-method="getRuntimeService"></bean> <bean id="taskService" factory-bean="processEngineFactoryBean" factory-method="getTaskService"></bean> <bean id="historyService" factory-bean="processEngineFactoryBean" factory-method="getHistoryService"></bean> <bean id="formService" factory-bean="processEngineFactoryBean" factory-method="getFormService"></bean> <bean id="identityService" factory-bean="processEngineFactoryBean" factory-method="getIdentityService"></bean> <bean id="managementService" factory-bean="processEngineFactoryBean" factory-method="getManagementService"></bean> </beans>
註意:其中 dataSource 和 txManager 為 spring-mybatis.xml 中配置的 “數據源(數據庫連接池)” 和 “事務管理”,所以不能在web.xml中引入,而應在spring-mybatis.xml中引入
b.在 spring-mybatis.xml 引入 spring-activiti.xml 配置
<!-- 導入activiti工作流配置 --> <import resource="spring-activiti.xml"/>
3.繪制流程圖
a.eclipse 中安裝插件: help —> Install New Software —> Add
Name:activiti
Location:http://activiti.org/designer/update/
b.新建流程圖:新建 MyProcess.bpmn
4.使用
a.在Service中使用
@Service public class ProcessService { public final static String PROCESS_ID = "myProcess"; //流程儲存服務組件 @Resource RepositoryService repositoryService; //運行時服務組件 @Resource RuntimeService runtimeService; //流程中的任務TASK組件 @Resource TaskService taskService; //部署流程(若xml已配置自動部署,則不需要此方法) public void deployProcess(){ // 部署流程,只要是符合BPMN2規範的XML文件,理論上都可以被ACTIVITI部署 repositoryService.createDeployment().addClasspathResource("MyProcess.bpmn").deploy(); } //啟動流程 public void startProcess(){ // 開啟流程,參數是流程的ID runtimeService.startProcessInstanceByKey(PROCESS_ID); } //根據Assignee查詢task public List<Task> findTaskByAssignee(String assignee){ List<Task> taskList = taskService.createTaskQuery(). taskAssignee(assignee). //根據辦理人查詢 orderByTaskCreateTime().asc(). //根據創建時間升序排列 list(); //返回列表 for(Task task : taskList){ 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("========================================="); } return taskList; } //完成任務 //variables 為流程變量,對應bpmn 中 Condition 設置的 ${message==‘不重要‘} public void completeProcess(String taskId, Map<String, Object> variables){ taskService.complete(taskId, variables); System.out.println("完成任務ID:"+taskId); } }
b.在Controller中測試
//提交流程 @RequestMapping("submitProcess") public @ResponseBody String submitProcess(HttpServletRequest request, HttpServletResponse response){ processService.startProcess(); return "提交流程成功"; } //部長審批 @RequestMapping("departmentProcess") public @ResponseBody String departmentProcess(HttpServletRequest request, HttpServletResponse response){ List<Task> taskIdList = processService.findTaskByAssignee("department"); for(Task task : taskIdList){ String taskId = task.getId(); processService.completeProcess(taskId, null); } return "部長審批成功"; } //副總審批 @RequestMapping("managerProcess") public @ResponseBody String managerProcess(HttpServletRequest request, HttpServletResponse response){ List<Task> taskIdList = processService.findTaskByAssignee("manager"); for(Task task : taskIdList){ String taskId = task.getId(); processService.completeProcess(taskId, null); } return "副總審批成功"; }
工作流——activiti