簡單建立Activiti工作流demo
阿新 • • 發佈:2019-02-04
建立簡單的Activiti工作流Demo,
其專案結構:
首先需要在maven裡新增依賴:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.Activiti</groupId> <artifactId>ActivitiDemo1</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>ActivitiDemo1 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- 引擎 --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>5.19.0.2</version> </dependency> <!-- 與spring整合 --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>5.19.0.2</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-bpmn-model</artifactId> <version>5.19.0.2</version> </dependency> <!-- mysql的連線 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies> <build> <finalName>ActivitiDemo1</finalName> </build> </project>
然後第二步:建立activiti.cfg.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_activiti" /> <property name="jdbcDriver" value="com.mysql.jdbc.Driver" /> <property name="jdbcUsername" value="root" /> <property name="jdbcPassword" value="root" /> <property name="databaseSchemaUpdate" value="true" /> </bean> </beans>
然後在main方法裡通過activiti.cfg.xml 進行配置,在資料庫裡生成25張表。程式碼如下
這些都做好以後,就可以進行一個簡單的工作流程的執行了。/** * 使用xml簡化配置 ,生成Activiti工作流所需要的25張表 */ @Test public void testCreateTableWithXml(){ // 引擎配置 ProcessEngineConfiguration pec=ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml"); // 獲取流程引擎物件 ProcessEngine processEngine=pec.buildProcessEngine(); }
package com.java1234.activiti.flow;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import java.util.List;
/**
* 學生process
* Created by T430 on 2017/8/9.
*/
public class StudentLeaveProcess {
/**
* 獲取預設流程引擎例項,會自動讀取activiti。config。xml檔案
*/
private ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();//獲取流程引擎
/**
* 第一步部署流程定義
*/
@Test
public void deploy(){
Deployment deployment= processEngine.getRepositoryService()//獲取部署相關的service
.createDeployment()//建立部署
.addClasspathResource("diagrams/StudentLeaveProcess.bpmn")//載入資原始檔
.addClasspathResource("diagrams/StudentLeaveProcess.png")//載入資原始檔
.name("學生請假流程")//流程名稱
.deploy();//部署
System.out.println("流程部署Id"+deployment.getId());
System.out.println("流程部署名字"+deployment.getName());
}
/**
* 第二步啟動流程例項
*/
@Test
public void start(){
ProcessInstance pi= processEngine.getRuntimeService()//執行時的service
.startProcessInstanceByKey("studentLeaveProcess");//流程定義表的KEY欄位值
System.out.println("流程例項id"+pi.getId());
}
/**
* 第三步檢視任務
*
*/
@Test
public void findTast(){
List<Task> taskList= processEngine.getTaskService()//任務相關service
.createTaskQuery()//建立任務查詢
.taskAssignee("張三")//指定莫個人
.list();
for (Task t:taskList
) {
System.out.println("任務Id"+t.getId());
System.out.println("任務名稱"+t.getName());
System.out.println("任務建立時間"+t.getCreateTime());
System.out.println("任務委派人"+t.getAssignee());
System.out.println("任務例項Id"+t.getProcessInstanceId());
}
}
/**
*第四步 完成任務
*/
@Test
public void completeTask(){
processEngine.getTaskService()//任務相關
.complete("2504");//任務ID
}
}
通過這些步驟,就可以完成簡單的學生請假流程了。
例子比較簡單。 不足之處請大神們見諒。