在这里记录一下我的Activiti历程:(以下示例不涉及真实业务,所有逻辑均建立在学习的基础上)
bpmn图
发起任务我设置了一个权限组user1,只要是这个权限的用户都可以发起任务
分发任务我设置了一个用户组,用户组中每个用户都可以处理这步流程,只要有一个人处理这步任务,分发的流程就算结束了
分发任务这一环节还有个判断,允许任务下发和不允许任务下发
任务分发完成则来到子流程,每个被分到任务的人需要执行任务,所以需要派生出多个实例。子流程里面用户集users包含着每一个user
子流程里面开始的事件需要接收用户参数生产每个实例
后面的步骤跟前面差不多,就不细说了
代码部分
配置文件xml
<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="jdbcDriver" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/activiti?characterEncoding=utf8&useSSL=false&nullCatalogMeansCurrent=true" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="MySQLadmin1!" />
<!-- 建表策略:若没有工作流表则创建,若存在工作流表则更新 -->
<property name="databaseSchemaUpdate" value="true" />
</bean>
<!-- 配置一个流程引擎工厂bean,用于创建流程引擎对象 -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<!-- 通过set方法注入流程引擎配置对象 -->
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
</beans>
ActivitiConfig.java
package com.avanty.activiti;
import org.activiti.engine.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ActivitiConfig {
/**
* 配合xml配置文件使用
*/
@Bean
public ProcessEngine processEngine() {
String resource = "activiti/activitiConfig.xml";
String beanName = "processEngineConfiguration";
ProcessEngineConfiguration conf = ProcessEngineConfiguration
.createProcessEngineConfigurationFromResource(resource, beanName);
return conf.buildProcessEngine();
}
/**
* @Bean可以用xml配置,也可以代码配置
* @Bean public ProcessEngine processEngine() { //创建Activiti配置对象实例
* ProcessEngineConfiguration configuration =
* ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
* configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
* configuration.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/activiti?characterEncoding=utf8&useSSL=false&nullCatalogMeansCurrent=true");
* configuration.setJdbcUsername("root");
* configuration.setJdbcPassword("MySQLadmin1!"); //设置建表策略
* configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
* //得到引擎实例 return configuration.buildProcessEngine(); }
*/
@Bean
public RepositoryService repositoryService(ProcessEngine processEngine) {
return processEngine.getRepositoryService();
}
@Bean
public RuntimeService runtimeService(ProcessEngine processEngine) {
return processEngine.getRuntimeService();
}
@Bean
public TaskService taskService(ProcessEngine processEngine) {
return processEngine.getTaskService();
}
@Bean
public HistoryService historyService(ProcessEngine processEngine) {
return processEngine.getHistoryService();
}
@Bean
public ManagementService managementService(ProcessEngine processEngine) {
return processEngine.getManagementService();
}
@Bean
public IdentityService identityService(ProcessEngine processEngine) {
return processEngine.getIdentityService();
}
}
用来测试的DeployActiviti.java
package com.avanty.activiti;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class DeployActiviti implements ApplicationRunner {
/**
* 流程部署相关表 `ACT_RE_DEPLOYMENT` `ACT_RE_MODEL` `ACT_GE_BYTEARRAY`
* `ACT_GE_PROPERTY` `ACT_RE_PROCDEF`
*/
/**
* 流程运行相关表 `ACT_RU_EXECUTION` `ACT_RU_IDENTITYLINK` `ACT_RU_JOB` `ACT_RU_TASK`
* `ACT_RU_VARIABLE`
*/
/**
* 流程历史相关表 `ACT_HI_IDENTITYLINK` `ACT_HI_VARINST` `ACT_HI_ACTINST`
* `ACT_HI_TASKINST`
*/
/**
* TaskQuery taskQuery = taskService.createTaskQuery();
* taskQuery.taskCandidateGroupIn(roleList);
* taskQuery.taskCandidateOrAssigned(usrnames);
* taskQuery.includeProcessVariables(); taskList = taskQuery.list();
*/
@Autowired
ProcessEngine processEngine;
@Autowired
RuntimeService runtimeService;
@Autowired
TaskService taskService;
@Override
public void run(ApplicationArguments args) {
finish();
}
private void deployProcess() {
RepositoryService service = processEngine.getRepositoryService();
InputStream bpmn = this.getClass().getResourceAsStream("/activiti/Process.bpmn");
Deployment deployment = service.createDeployment().name("任务流程").addInputStream("Process.bpmn", bpmn).deploy();
System.out.println(deployment.getName());
System.out.println(deployment.getId());
}
/**
* 发起任务
*/
private void step1() {
String processInstanceByKey = "Process";
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processInstanceByKey);
System.out.println(processInstance.getId());
}
/**
* 查看发起任务
*/
private void findStep1() {
TaskQuery taskQuery = taskService.createTaskQuery();
List<String> roleCodeList = new ArrayList<>();
roleCodeList.add("user1");
taskQuery.taskCandidateGroupIn(roleCodeList);
taskQuery.includeProcessVariables();
List<Task> tasks = taskQuery.list();
for (Task task : tasks) {
System.out.println(task.getId());
System.out.println(task.getProcessInstanceId());
System.out.println(task.getName());
System.out.println(task.getAssignee());
}
}
/**
* 分发任务
*/
private void step2() {
TaskQuery taskQuery = taskService.createTaskQuery();
List<String> roleCodeList = new ArrayList<>();
roleCodeList.add("user1");
taskQuery.taskCandidateGroupIn(roleCodeList);
taskQuery.includeProcessVariables();
Task task = taskQuery.singleResult();
Map<String, Object> variables = new HashMap<>();
List<String> users = new ArrayList<>();
users.add("Michael");
users.add("Zhan");
variables.put("users", users);
taskService.complete(task.getId(), variables);
System.out.println("finish");
}
/**
* 查看分发任务
*/
private void findStep2() {
String assignee = "Zhan";
// String assignee = "Michael";
List<Task> tasks1 = taskService.createTaskQuery().taskCandidateUser(assignee).list();
for (Task task1 : tasks1) {
System.out.println(task1.getId());
System.out.println(task1.getName());
}
}
/**
* 子流程-执行任务
*/
private void step3() {
TaskQuery taskQuery = taskService.createTaskQuery();
taskQuery.taskCandidateOrAssigned("Michael");
taskQuery.includeProcessVariables();
Task task = taskQuery.singleResult();
System.out.println(task.getId());
Map<String, Object> variables = new HashMap<>();
List<String> users = new ArrayList<>();
users.add("admin");
users.add("root");
variables.put("users", users);
variables.put("isPass", 1);
taskService.complete(task.getId(), variables);
System.out.println("finish");
}
/**
* 子流程-查看执行任务
*/
private void findStep3() {
String assignee = "admin";
// String assignee = "root";
List<Task> tasks = taskService.createTaskQuery().taskCandidateOrAssigned(assignee).list();
for (Task task : tasks) {
System.out.println(task.getId());
System.out.println(task.getProcessInstanceId());
System.out.println(task.getName());
}
}
/**
* 子流程-验收任务
*/
private void step4() {
String assignee = "admin";
Task task = taskService.createTaskQuery().taskCandidateOrAssigned(assignee).singleResult();
System.out.println(task.getId());
Map<String, Object> variables = new HashMap<>();
List<String> users = new ArrayList<>();
if (assignee.equals("root")) {
users.add("administrator");
users.add("su");
}else if(assignee.equals("admin")) {
users.add("alex");
users.add("sudo");
}
variables.put("users", users);
taskService.complete(task.getId(), variables);
System.out.println("finish");
}
/**
* 子流程-查看验收任务
*/
private void findStep4() {
// String assignee = "administrator";
// String assignee = "su";
// String assignee = "alex";
String assignee = "sudo";
List<Task> tasks = taskService.createTaskQuery().taskCandidateOrAssigned(assignee).list();
for (Task task : tasks) {
System.out.println(task.getId());
System.out.println(task.getProcessInstanceId());
System.out.println(task.getName());
}
}
/**
* 子流程-完成
*/
private void finish() {
String assignee = "alex";
Task task = taskService.createTaskQuery().taskCandidateOrAssigned(assignee).singleResult();
Map<String, Object> variables = new HashMap<>();
variables.put("isPass", 1);
taskService.complete(task.getId(), variables);
System.out.println("finish");
}
}
最后,附上项目地址 Activiti-Test
项目是用Eclipse写的,不用IDEA的原因是Eclipse的Activiti插件比IDEA的好,有自动对齐功能,强迫症的福音呐!