activiti基本使用
1、安装activiti插件
- 进入idea设置
- 点击Plugins
- 找到Install plugin from disk选中本地插件jar包(actibpm.jar)
- 重启idea
新建文件有bpmn的文件选项则安装成功
2、导依赖
<properties>
<slf4j.version>1.7.2</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<!-- activiti start -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>5.21.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.21.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-image-generator</artifactId>
<version>5.21.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-process-validation</artifactId>
<version>5.21.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.21.0</version>
<exclusions>
<exclusion>
<artifactId>commons-dbcp</artifactId>
<groupId>commons-dbcp</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-diagram-rest</artifactId>
<version>5.21.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-json-converter</artifactId>
<version>5.21.0</version>
<exclusions>
<exclusion>
<artifactId>commons-collections</artifactId>
<groupId>commons-collections</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- LOGGING begin -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- LOGGING end -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
3、在resources资源目录下创建bpmn文件
4、新建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 -->
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?characterEncoding=utf8&serverTimezone=GMT%2B8"/>
<property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUsername" value="root"/>
<property name="jdbcPassword" value="123456"/>
<!--关闭Deployment时删除表,第一次部署流程文件时开启,部署完毕后关闭-->
<property name="databaseSchemaUpdate" value="create-drop"/>
</bean>
</beans>e
5、导入log4j配置文件
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.gec.bookinfo.dao.BookInfoDao=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
6、测试
package com.yl;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
public class ActivitiTest {
//创建工作流引擎
ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();
//管理流程文件的对象
RepositoryService repositoryService=processEngine.getRepositoryService();
//管理流程实例的对象
RuntimeService runtimeService=processEngine.getRuntimeService();
//管理任务节点的对象
TaskService taskService=processEngine.getTaskService();
/**
* 部署流程文件
*/
@Test
public void deployment() {
//部署bpmn文件到数据库表
Deployment deployment=repositoryService.createDeployment().addClasspathResource("hello.bpmn").deploy();
System.out.println("部署文件id="+deployment.getId());
}
/**
* 删除流程文件
*/
@Test
public void delete(){
repositoryService.deleteDeployment("1");
}
/**
* 启动流程实例
*/
@Test
public void start(){
ProcessInstance processInstance=runtimeService.startProcessInstanceByKey("myProcess_1");
System.out.println("流程实例id="+processInstance.getId());
}
/**
* 获取当前任务节点对象及完成此任务节点
*/
@Test
public void complete(){
//获取当前流程实例id=2501的当前任务节点对象
Task task= taskService.createTaskQuery().processInstanceId("2501").singleResult();
System.out.println(task.getName());
//完成此任务节点
taskService.complete(task.getId());
}
/**
* 判断流程实例是否结束
*/
@Test
public void endOrNot(){
//获取当前流程实例id=2501的当前任务节点对象
Task task= taskService.createTaskQuery().processInstanceId("2501").singleResult();
if (task==null){
System.out.println("当前实例已经结束");
}else {
System.out.println("当前实例还没有结束");
}
}
}