starter 集成
依赖
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
<version>7.1.0.M3.1</version>
</dependency>
配置项
spring.activiti.database-schema-update=true
spring.activiti.process-definition-location-prefix=classpath:/process/
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/activiti
?useUnicode=true&
characterEncoding=utf-8&
useSSL=false&
serverTimezone=UTC&
nullCatalogMeansCurrent=true
spring.datasource.username=root
spring.datasource.password=root
不适用 start
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>6.0.0</version>
</dependency>
package com.tcloud.dubbo.config;
import org.activiti.engine.*;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.io.IOException;
@Configuration
public class ActivitiConfig {
/*
* 配置分为以下几步骤
* 1. 创建ActivitiConfig
* 2. 使用ActivitiConfig创建ProcessEngineFactoryBean
* 3. 使用ProcessEngineFactoryBean创建ProcessEngine对象
* 4. 使用ProcessEngine对象创建需要的服务对象
* */
private final DataSource dataSource;
private final PlatformTransactionManager platformTransactionManager;
@Autowired
public ActivitiConfig(DataSource dataSource, PlatformTransactionManager platformTransactionManager) {
this.dataSource = dataSource;
this.platformTransactionManager = platformTransactionManager;
}
/*
* 1. 创建配置文件,也就是提供一些配置信息,这样就可以自定义自己的创建信息了
* 需要一些参数,1. 数据源。2. 事务管理器。
* 这里还加入了自动扫描process包下的bpmn(流程定义文件)的设置,这样就可以省去了部署
* */
@Bean
public SpringProcessEngineConfiguration springProcessEngineConfiguration() {
SpringProcessEngineConfiguration spec = new SpringProcessEngineConfiguration();
spec.setDataSource(dataSource);
spec.setTransactionManager(platformTransactionManager);
spec.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
// spec.setAsyncExecutorActivate(false);
Resource[] resources = null;
// 启动自动部署流程
try {
resources = new PathMatchingResourcePatternResolver().getResources("classpath*:process/*.bpmn");
} catch (IOException e) {
e.printStackTrace();
}
spec.setDeploymentResources(resources);
return spec;
}
@Bean
public ProcessEngineFactoryBean processEngine() {
ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
processEngineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration());
return processEngineFactoryBean;
}
@Bean
public RepositoryService repositoryService() throws Exception {
return processEngine().getObject().getRepositoryService();
}
@Bean
public RuntimeService runtimeService() throws Exception {
return processEngine().getObject().getRuntimeService();
}
@Bean
public TaskService taskService() throws Exception {
return processEngine().getObject().getTaskService();
}
@Bean
public HistoryService historyService() throws Exception {
return processEngine().getObject().getHistoryService();
}
}
IDEA集成Activiti插件
ideal 官方 搜索 actiBpm 下载jar 导入
导入刚下载的actiBPM.jar包,再重启idea!完美解决!
如果在修改成xml 文件的时候,出现乱码情况,需要在idea安装目录下,找到idea.exe.vmoptions和idea64.exe.vmoptions两个文件。最文件的最后添加上 -Dfile.encoding=UTF-8,可以解决乱码问题
bpmn 配置工作流 核心类
表说明
act_ru_execution 执行对象表
act_ru_task 用户任务表
act_hi_actinst 活动节点历史表
act_hi_procinst 流程实例历史表
act_hi_taskinst 历史任务表
act_re_procdef 流程定义表
act_re_deployment 流程部署表
核心类
SpringProcessEngineConfiguration
配置数据源
配置bpmn 加载路径
配置自定义查询mapper
ProcessEngineFactoryBean
ProcessEngine
ManagementService
查询表结构元数据(TableMetaData)
通用表查询(TablePageQuery)
执行自定义的sql查询(executeCustomSql)
@Autowired
private ManagementService managementService;
TablePage tablePage = managementService.createTablePageQuery()
.tableName(managementService.getTableName(ProcessDefinitionEntity.class))
.listPage(0,100);
managementService.executeCustomSql(
new AbstractCustomSqlExecution<MyCustomMapper,List<Map<String,Object>>>(MyCustomMapper.class){
public List<Map<String, Object>> execute(MyCustomMapper myCustomMapper){
return myCustomMapper.findAll();
}
RepositoryService(流程存储服务)
act_re_deployment
act_re_model
act_re_procdef
@Autowired
private RepositoryService repositoryService
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("BPMN/part1_deployment.zip");
ZipInputStream zip = new ZipInputStream(resourceAsStream);
Deployment deployment = repositoryService.createDeployment()
.addZipInputStream(zip)
.name("流程部署测试_zip")
.deploy();
String fileName= "BPMN/part1_deployment.bpmn";
String imageName = "BPMN/part1_deployment.png";
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource(fileName)
.addClasspathResource(imageName)
.name("部署流程测试_v1")
.deploy();
。 Deployment deploy = repositoryService
.createDeployment()
.disableSchemaValidation()
.addClasspathResource("BPMN/Part6_UEL.bpmn")
.deploy();
//查询全部流程
List<Deployment> list = repositoryService.createDeploymentQuery().list()
//查询流程信息定义 act_re_procdef
ProcessDefinition def = repositoryService.createProcessDefinitionQuery()
.deploymentId(d.getId()).singleResult();
//如果需要设置 权限
repositoryService.addCandidateStarterGroup(def.getId(), "group1");
repositoryService.addCandidateStarterUser(def.getId(), "user1");
//查询流程图
InputStream is = repositoryService.getProcessDiagram(def.getId());
// 将输入流转换为图片对象
BufferedImage image = ImageIO.read(is);
// 保存为图片文件
File file = new File("resource/artifact/result.png");
if (!file.exists()) file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
ImageIO.write(image, "png", fos);
fos.close();
is.close();
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();
// 根据用户查询用权限的流程定义
List<ProcessDefinition> defs = repositoryService
.createProcessDefinitionQuery().startableByUser("user1").list();
//删除流程
repositoryService.deleteDeployment(deployment.getId());
RuntimeService(管理运行时)
// 启动服务 流程定义的key 和 传递参数 配置UEL
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(def.getKey(),assignee);
//查询 运行的服务
runtimeService
IdentityService 设置用户和组的服务
Group group = identityService.newGroup(id);
group.setName(name);
group.setType(type);
identityService.saveGroup(group);
User user = identityService.newUser(id);
// 设置用户的各个属性
user.setFirstName(first);
user.setLastName(last);
user.setEmail(email);
user.setPassword(passwd);
// 使用saveUser方法保存用户
identityService.saveUser(user);
TaskService 提交,流转,下一步
taskService.complete("8c790bcc-2eea-11eb-bd03-9c5c8e79c1e8");
UEL 统一表达式语言(Unified Expression Language)
${userName == ‘zhangsan’ and pwd == ‘999’}
- 启动流程配置参数
Map<String,Object> variables = new HashMap<String,Object>();
variables.put("assignee","zhangsan");
ProcessInstance processInstance = runtimeService
.startProcessInstanceByKey("myProcess_UEL", variables);
//修改 流程参数
// 通过 id 设置流程变量
// 参数1:流程实例id
// 参数2:流程变量名
// 参数3:流程变量所对应的值
runtimeService.setVariables("2501","assignee","李四");
Bpmn
1.UserTask 任务
2.ParallelGateway 并行网关,后续的task 并行触发 (菱形 +)
3.ExclusiveGateway 排他网关 流转线上配置 不同的条件 (菱形 ×) 类似 or
4.inclusivGateWay 包容网关 ,分支(fork):所有出口顺序流都会被计算,对于计算为true的分支都会被执行, 聚合(join):所有到达包容网关的并行执行,都会在网关处等待 (菱形 0)
userTask 上 candidate-user 属性,分割的多个用户,这个配置的字符串的人员都能进行处理,谁先处理就完成 候选人
userTask 上 assign 属性 用于定义当前task的参与者
usertask Task Listeners 配置 TaskListener 实现类 重写 notify 指定下一任务的配置信息