这里写目录标题
- 一、开发环境
- 1.1 Java环境
- 1.2 数据库
- 1.3 开发工具
- 二、Activiti入门体验
- 2.1 新建项目
- 2.2 pom.xml文件
- 2.3 activiti.cfg.xml文件
- 2.4 添加日志配置文件
- 三、创建数据库
- 3.1 创建数据库
- 3.2 执行代码
- 3.2.1 通过代码实现创建表格
- 3.2.2 简化代码
- 四、绘制流程
- 4.1 新建bpmn文件
- 4.2 绘制流程图
- 4.3 将流程图保存为png图片
- 五、代码开发
- 5.1 流程部署
- 5.2 启动流程实例
- 5.3 任务查询
- 5.4 任务处理
一、开发环境
1.1 Java环境
Jdk1.8 或者以上版本。
1.2 数据库
Mysql5以上的版本。本地用的是5.7.27。
1.3 开发工具
Idea开发工具,本地用的版本是2022.2.4,所以插件选用的是Activiti BPMN visualizer。
二、Activiti入门体验
2.1 新建项目
2.2 pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>activiti01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
</properties>
<dependencies>
<!-- activiti香菇按jar包 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-json-converter</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-layout</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti.cloud</groupId>
<artifactId>activiti-cloud-services-api</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<!-- mysql连接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<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>
<!-- log end -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<repositories>
<!-- 从artifacts库里面下载香菇按信息 -->
<repository>
<id>alfresco</id>
<name>Activiti Releases</name>
<url>https://artifacts.alfresco.com/nexus/content/repositories/activiti-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</project>
2.3 activiti.cfg.xml文件
在resource文件夹下创建spring与activiti的中和配置文件: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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contex http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--数据源配置dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.247.131:3306/activiti?useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<!--activiti单独运行的ProcessEngine配置对象(processEngineConfiguration),使用单独启动方式
默认情况下:bean的id=processEngineConfiguration
-->
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<!--代表数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--代表是否生成表结构-->
<property name="databaseSchemaUpdate" value="true"/>
</bean>
</beans>
2.4 添加日志配置文件
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\log\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
三、创建数据库
3.1 创建数据库
create database activiti;
首先创建数据库,否则无法通过代码创建表。
3.2 执行代码
3.2.1 通过代码实现创建表格
public static void main(String[] args) {
//1、创建创建ProcessEngineConfiguration对象
ProcessEngineConfiguration configuration
=ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml","processEngineConfiguration");
//2、创建ProcesEngine对象
ProcessEngine processEngine = configuration.buildProcessEngine();
//3、输出ProcesEngine对象
System.out.println(processEngine);
}
3.2.2 简化代码
public static void main(String[] args) {
//条件:1.activiti配置文件名称:activiti.cfg.xml 2.bean的id="processEngineConfiguration"
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
System.out.println(processEngine);
// HistoryService historyService = processEngine.getHistoryService();
}
通过代码执行,实现数据库里面创建表格。
四、绘制流程
4.1 新建bpmn文件
4.2 绘制流程图
新建Start event,后面跟上User Task。
用xml文件查看该文件的话,其中主要流程是:
<startEvent id="sid-4db31c12-55c5-441a-ac49-9d5aa1d1da0a"/>
<userTask id="sid-090babc2-d4a2-48b2-b637-5fcfd95ca03f" name="填写请假申请单" activiti:assignee="zhangsan"/>
<userTask id="sid-3217c623-3a06-480f-93dc-3f5acf1dbb9d" name="部门经理审批" activiti:async="false" activiti:assignee="lisi"/>
<userTask id="sid-ba9a9eed-ba25-47c5-be89-6d646ed477d7" name="总经理审批" activiti:assignee="wangwu"/>
<endEvent id="sid-4aa51f9c-4579-4c90-8f99-582767e1f989"/>
4.3 将流程图保存为png图片
五、代码开发
5.1 流程部署
部署流程定义就是将绘制的流程定义图形(.bpmn)部署到工作流引擎中,方法如下:
public class ActivitiDeployment {
public static void main(String[] args) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取repositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
//部署对象
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("diagram/holiday.bpmn20.xml")//bpmn文件
.addClasspathResource("diagram/holiday.png")// 图片文件
.name("请假申请流程")
.deploy();
System.out.println("流程部署id:" + deployment.getId());
System.out.println("流程部署名称:" + deployment.getName());
}
}
执行此操作后activiti会将上边代码中指定的bpmn文件和图片保存在activiti数据库中。涉及的表有:
- act_re_deployment : 部署信息
- act_re_procdef : 流程定义的一些信息
- act_ge_bytearray : 流程定义的bpmn文件及png文件
5.2 启动流程实例
public class ActivitiStartInstance {
public static void main(String[] args) {
//1.得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//2.得到RunService对象
RuntimeService runtimeService = processEngine.getRuntimeService();
//3.创建流程实例 流程定义的key需要知道 holiday
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holiday");
//4.输出实例的相关信息
System.out.println("流程部署ID"+processInstance.getDeploymentId());//null
System.out.println("流程定义ID"+processInstance.getProcessDefinitionId());//holiday:1:4
System.out.println("流程实例ID"+processInstance.getId());//2501
System.out.println("活动ID"+processInstance.getActivityId());//null
}
}
- act_hi_actinst : 已完成的活动信息
- act_hi_identitylink : 参与者信息
- act_hi_procinst : 流程实例
- **act_hi_taskinst ** : 任务实例
- act_ru_execution : 执行表
- act_ru_identitylink : 参与者信息
- act_ru_task : 任务
5.3 任务查询
查询zhangsan的任务
public static void main(String[] args) {
//1.得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//2.得到TaskService对象
TaskService taskService = processEngine.getTaskService();
//3.根据流程定义的key,负责人assignee来实现当前用户的任务列表查询
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("holiday")
.taskAssignee("zhangsan")
.list();
//4.任务列表的展示
for(Task task :taskList){
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("任务ID:"+task.getId());
System.out.println("任务负责人:"+task.getAssignee());
System.out.println("任务名称:"+task.getName());
}
}
5.4 任务处理
public class ActivitiCompleteTask {
public static void main(String[] args) {
//1.得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//2.得到TaskService对象
TaskService taskService = processEngine.getTaskService();
//3.处理任务,结合当前用户任务列表的查询操作的话,任务ID:2505
taskService.complete("2505");
}
}