上周项目经理交给新的任务,在现在的工作基础上整合工作流引擎,技术选型选的是activiti6.0,于是我在网上就找各种资料整合,大部分的是先在pom.xml文件内添加依赖:
org.activiti
activiti-spring-boot-starter-basic
6.0.0
org.activiti
activiti-spring
6.0.0
org.activiti
activiti-engine
6.0.0
de.odysseus.juel
juel-api
2.2.7
org.activiti
activiti-process-validation
6.0.0
org.activiti
activiti-form-api
6.0.0
org.activiti
activiti-dmn-api
6.0.0
org.apache.commons
commons-lang3
3.1
org.activiti
activiti-image-generator
6.0.0
org.activiti
activiti-bpmn-model
6.0.0
org.activiti
activiti-bpmn-converter
6.0.0
com.h2database
h2
runtime
de.odysseus.juel
juel-impl
2.2.7
再在application.yml文件中增加如下配置:
spring:
activiti:
database-schema-update: true
history-level: full
process-definition-location-prefix: classpath:/processes/
datasource:
url: jdbc:mysql://127.0.0.1:3306/demo_workflow?useUnicode=true&characterEncoding=UTF-8
username: root
password: root
driverClassName: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
然后再用activiti.cfg.xml获取processEngine,配置如下:
然后在启动类里添加这个注解
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
还需要在resources下新建目录processes文件夹,里边需要新建流程文件,流程文件为.bomn文件,然后将它复制一份改名为xml,注意xml文件的结尾需为bpmn.xml或者bpmn20.xml不然会找不到相关流程配置文件。
下面贴上简单的业务流程图:
接下来需要在项目里的controller里写一些流程代码来部署,启动,流转流程。
下边贴上部分代码:
@RestController
@RequestMapping(value = "/activitiService")
public class DemoController {
private static final Logger logger = LoggerFactory.getLogger(DemoController.class);
/**
* 流程定义和部署相关的存储服务
*/
@Autowired
private RepositoryService repositoryService;
/**
* 流程运行时相关的服务
*/
@Autowired
private RuntimeService runtimeService;
/**
* 节点任务相关操作接口
*/
@Autowired
private TaskService taskService;
/**
* 历史记录相关服务接口
*/
@Autowired
private HistoryService historyService;
@RequestMapping(value = "/start")
public String start() {
Deployment deployment = repositoryService.createDeployment()//创建一个部署对象
.name("离司流程")
.addClasspathResource("processes/salemanResignDemo.bpmn")
.addClasspathResource("diagrams/salemanResignDemo.png")
.deploy();
logger.info("部署ID:" + deployment.getId());
logger.info("部署名称:" + deployment.getName());
// //查询流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
//在流程模板中定义的流程ID
String instanceKey = processDefinition.getId();
logger.info("开启离司流程...");
//设置流程参数,开启流程
Map map = new HashMap();
map.put("salemanNo", "110234");
map.put("resignReason", "个人原因");
//使用流程定义的key启动流程实例,key对应salemanResignDemo.bpmn文件中id的属性值,使用key值启动,默认是按照最新版本的流程定义启动
ProcessInstance instance = runtimeService.startProcessInstanceById(instanceKey, map);
logger.info("启动流程实例成功:{}", instance);
logger.info("流程实例ID:{}", instance.getId());
logger.info("流程定义ID:{}", instance.getProcessDefinitionId());
//验证是否启动成功
//通过查询正在运行的流程实例来判断
ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
//根据流程实例ID来查询
List runningList = processInstanceQuery.processInstanceId(instance.getProcessInstanceId()).list();
logger.info("根据流程ID查询条数:{}", runningList.size());
// 返回流程ID
return instance.getId();
}
@RequestMapping(value = "/applyResign")
public String applyResign(@RequestParam String taskId, String salemanNo, String resignReason) {
/*
* 获取请求参数
*/
// String taskId;
List list = taskService.createTaskQuery().taskAssignee("xiaoli").list();
/*
* 查询任务
*/
if (list != null && list.size() > 0) {
for (Task task : list) {
logger.info("任务ID:" + task.getId());
logger.info("任务名称:" + task.getName());
logger.info("任务的创建时间:" + task.getCreateTime());
logger.info("任务的办理人:" + task.getAssignee());
logger.info("流程实例ID:" + task.getProcessInstanceId());
logger.info("执行对象ID:" + task.getExecutionId());
logger.info("流程定义ID:" + task.getProcessDefinitionId());
taskId = task.getId();
}
} else {
logger.info("任务ID:{}查询到任务为空!");
return "fail";
}
/*
* 参数传递并提交申请
*/
Map map = new HashMap();
map.put("salemanNo", salemanNo);
map.put("resignReason", resignReason);
taskService.complete(taskId, map);
logger.info("执行【员工离司申请】环节,流程推动到【主管审核】环节");
/*
* 返回同意
*/
return "success";
}
关于数据库表的说明,我在网上看到一张图,我就直接拿来用吧,事先声明这不是原创的图片:
可以看看,哪里有问题欢迎批评指正,如有遗漏,及时联系我。