1. 程式人生 > >關於自學activiti流程引擎的一點點感悟和程式碼分享

關於自學activiti流程引擎的一點點感悟和程式碼分享

最近辭職,在交接工作,沒事就自己預言了一下activiti,activiti相對於JBPM還是強悍蠻多的(網上別人說的)。

哈哈,不扯淡了,首先看一下專案總體結構圖如下:


環境配置:

eclipse+mysql+activiti+struts2+spring+mybatis+ligerui.

struts2、spring零配置,全註解實現。

我相信這個專案相對於有2~3年的程式猿有很大的幫助.

初始化資料庫,新建一個activiti-demo的資料庫。

初始化專案中的初始化指令碼,在web-initscript裡面的sql檔案全部初始化。注意不能用mysql-fornt工具初始化,最好用HeidiSQL工具初始化指令碼。

啟動eclipse匯入專案,修改專案根目錄下的jdbc.properties檔案,URL換成自己的資料庫的連線url,修改為自己的使用者名稱、密碼等等。

配置tomcat伺服器,執行專案。開啟瀏覽器,登入-釋出流程。

資料庫定義了三個使用者:

zhangsan   123

sa                123

admin         123

如圖:



釋出流程後就可以進行activiti流程引擎之旅。(流程檔案在initscript中的xml)

用zhangsan登入-提交請假申請。

如圖:


sa登入-檢視個人代辦,可以看到zhangsan提交的申請,可以對記錄進行審批或者檢視流程圖。

如圖:


sa通過後流程會流向admin,如果不同意會流向zhangsan,此時zhangsan代辦可以看到自己提交的記錄,這是不合理的,因為我是做個自己預研的demo,所以

這個沒有處理,如果是等待zhangsan審批,zhangsan是可以對記錄進行修改,然後提交,繼續走流程。

對程式碼進行截圖,本人覺得程式碼註釋還是可以滴。

package org.activiti.demo.engine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.HistoryService;
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.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.bpmn.diagram.ProcessDiagramGenerator;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 
 * @Title: ProcessEngineCore.java
 * @Description: 流程引擎核心類
 * @Package org.activiti.demo.engine
 * @author 
[email protected]
* @date 2013-3-13 * @version V1.0 * */ @Service("processEngineCore") public class ProcessEngineCore { /**************** 日誌物件 ****************/ private static final Logger logger = LoggerFactory.getLogger(ProcessEngineCore.class); @Resource(name = "repositoryService") private RepositoryService repositoryService; @Resource(name = "runtimeService") private RuntimeService runtimeService; @Resource(name = "taskService") private TaskService taskService; @Resource(name = "historyService") private HistoryService historyService; /** * 部署流程例項 * * @param processFilePath * 流程檔案路徑 * @throws FileNotFoundException */ public void deploymentInstance(String processFilePath) throws FileNotFoundException { InputStream in = new FileInputStream(new File(processFilePath)); // 從檔案流部署流程 Deployment deployment = repositoryService.createDeployment().addInputStream(processFilePath.substring(processFilePath.lastIndexOf(File.separator) + 1, processFilePath.length()), in).deploy(); logger.info("process deployment success & processID is " + deployment.getId() + ",createTime is " + deployment.getDeploymentTime()); } /** * 根據任務ID獲取流程定義 * * @param taskId * 任務ID * @return * @throws Exception */ public ProcessDefinitionEntity findProcessDefinitionEntityByTaskId(String taskId) throws Exception { // 取得流程定義 ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService).getDeployedProcessDefinition(findTaskById(taskId).getProcessDefinitionId()); if (processDefinition == null) { throw new Exception("流程定義未找到!"); } return processDefinition; } /** * 啟動流程例項 * * @param processInstanceByKey * 流程部署KEY * @param proMap * 流程變數 */ public ProcessInstance startInstance(String processInstanceByKey, Map<String, Object> proMap) { // 啟動流程例項 ,注意這裡的key是我們流程檔案中的<process id="myProcess" // ,id屬性,在Activiti術語叫key // yangjie 啟動一個請假流程例項 ProcessInstance processInstance = null; if (proMap != null) { processInstance = runtimeService.startProcessInstanceByKey(processInstanceByKey, proMap); } else { processInstance = runtimeService.startProcessInstanceByKey(processInstanceByKey); } logger.info("process start success key [" + processInstance.getBusinessKey() + "]"); return processInstance; } /** * 根據任務ID獲取對應的流程例項 * * @param taskId * 任務ID * @return * @throws Exception */ public ProcessInstance findProcessInstanceByTaskId(String taskId) throws Exception { // 找到流程例項 ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(findTaskById(taskId).getProcessInstanceId()).singleResult(); if (processInstance == null) { throw new Exception("流程例項未找到!"); } return processInstance; } /** * 根據任務ID獲得任務例項 * * @param taskId * 任務ID * @return * @throws Exception */ private TaskEntity findTaskById(String taskId) throws Exception { TaskEntity task = (TaskEntity) taskService.createTaskQuery().taskId(taskId).singleResult(); if (task == null) { throw new Exception("任務例項未找到!"); } return task; } /** * 根據executionId查詢task * * @param executionId * @return */ public List<Task> queryByExecutionId(String executionId) { List<Task> taskList = taskService.createTaskQuery().executionId(executionId).list(); return taskList; } /** * 根據executionId查詢task * * @param executionId * @return */ public Task queryByExecutionIdSingle(String executionId) { Task task = taskService.createTaskQuery().executionId(executionId).singleResult(); return task; } /** * 查詢UserTask列表 * * @param userName * @return */ public List<Task> queryUserTaskList(String userName) { // 查詢當前使用者任務列表 List<Task> taskList = taskService.createTaskQuery().taskAssignee(userName).list(); return taskList; } /** * 完成UserTask * * @param taskId * 任務ID * @param proMap * 流程變數 */ public void completeUserTask(String taskId, Map<String, Object> proMap) { if (proMap != null) { taskService.complete(taskId, proMap); } else { taskService.complete(taskId); } } /** * 根據流程ID檢視流程是否結束 * * @param processInstanceId * 流程ID */ public boolean queryProcessIsEnd(String processInstanceId) { HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult(); if (historicProcessInstance != null && historicProcessInstance.getStartTime() != null && historicProcessInstance.getEndTime() != null) { return true; } return false; } /** * 獲取圖片流 * * @param taskId * @return * @throws Exception */ public InputStream getImageStream(String taskId) throws Exception { ProcessDefinitionEntity pde = findProcessDefinitionEntityByTaskId(taskId); InputStream imageStream = ProcessDiagramGenerator.generateDiagram(pde, "png", runtimeService.getActiveActivityIds(findProcessInstanceByTaskId(taskId).getId())); return imageStream; } public void setRepositoryService(RepositoryService repositoryService) { this.repositoryService = repositoryService; } public void setRuntimeService(RuntimeService runtimeService) { this.runtimeService = runtimeService; } public void setTaskService(TaskService taskService) { this.taskService = taskService; } public void setHistoryService(HistoryService historyService) { this.historyService = historyService; } }

資源地址:http://download.csdn.net/detail/hncdyj/5183895

heidiSQL工具:http://download.csdn.net/detail/hncdyj/5183902

高手請勿噴。純屬個人對技術的熱愛。

轉載請註明出處:http://blog.csdn.net/hncdyj