添加依赖
<!--spring boot 快速启动依赖-->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>6.0.0</version>
</dependency>
<!--自动部署布局依赖-->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-layout</artifactId>
<version>6.0.0.RC1</version>
</dependency>
修改spring boot 配置文件
spring:
activiti:
check-process-definitions: false # activti是否自动部署
db-identity-used: false #是否使用activti自带的用户体系
database-schema-update: true #是否每次都更新数据库
process-definition-location-prefix: classpath:/processes/
history-level: full #full最高级别
注入服务
/**
* 任务管理
*/
@Autowired
private TaskService taskService;
/**
* 历史管理(执行完的数据)
*/
@Autowired
private HistoryService historyService;
/**
* 管理流程定义
*/
@Autowired
private RepositoryService repositoryService;
/**
* 组织机构管理
*/
@Autowired
private IdentityService identityService;
@Autowired
private RuntimeService runtimeService;
启动任务流程
Map<String, Object> vars = new HashMap<>();
ProcessInstance processInstance = runtimeService
.startProcessInstanceByKey(processDefinitionKey,vars);
System.out.println("流程实例ID:" + processInstance.getProcessInstanceId());
System.out.println("流程定义ID:" + processInstance.getProcessDefinitionId());
//获得流程模型
Task task = taskService.createTaskQuery()
.processInstanceId(processInstance.getProcessInstanceId())
.singleResult();
//taskService.addComment(task.getId(),task.getProcessInstanceId(),"");
taskService.complete(task.getId(), vars);
//获取下一步task
List<TaskModel> userTasks = getNextNode(processInstance.getProcessInstanceId());
public List<TaskModel> getNextNode(String processInstanceId) throws Exception {
String processDefinitionId = historyService.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult().getProcessDefinitionId();
List<Task> tasks = taskService.createTaskQuery()
.processInstanceId(processInstanceId)
.list();
List<TaskModel> userTasks = new ArrayList<>();
BpmnModel model = repositoryService.getBpmnModel(processDefinitionId);
if(model != null&&tasks!=null&&tasks.size()>0) {
//获得流程模型的所有节点
Collection<FlowElement> flowElements = model.getMainProcess().getFlowElements();
for(FlowElement e : flowElements) {
for(Task t : tasks){
if(e.getId().equals(t.getTaskDefinitionKey())){
if( e instanceof UserTask){
userTasks.add(new TaskModel((UserTask)e,t));
}else{
List<SequenceFlow> currentUSer = ((UserTask)e).getOutgoingFlows();
for(SequenceFlow s : currentUSer){
nextNode(flowElements,s,userTasks,t);
}
}
}
}
}
}
return userTasks;
}
private void nextNode(Collection<FlowElement> flowElements,SequenceFlow currentSequenceFlow,List<TaskModel> userTasks,Task task)throws Exception{
//获得当前线在
String SequenceFlowId = currentSequenceFlow.getTargetRef();
for(FlowElement e : flowElements){
if(e.getId().equals(SequenceFlowId)){
//判断类型
//如果是排他网管
if(e instanceof ExclusiveGateway){
//默认流程线
String defaultFlowString = ((ExclusiveGateway) e).getDefaultFlow();
SequenceFlow defaultFlow = null;
//获取该节点下一步线集合
List<SequenceFlow> egSequenceFlow = ((ExclusiveGateway) e).getOutgoingFlows();
//标识
boolean boo = true;
for(int i=0;i<egSequenceFlow.size();i++){
//如果是默认路线
if(egSequenceFlow.get(i).getId().equals(defaultFlowString)){
defaultFlow = egSequenceFlow.get(i);
}
if(!StringUtils.isEmpty(egSequenceFlow.get(i).getConditionExpression())){
//递归获取排他网关路线
nextNode(flowElements,egSequenceFlow.get(i),userTasks,task);
}else{
continue;
}
//如果最后一个走完没有el为true的,则查看是否有默认流程,如果没有抛出异常
if(i==egSequenceFlow.size()-1&&boo){
if(StringUtils.isEmpty(defaultFlowString)){
throw new Exception("流程异常");
}else{
//如果有默认流程 递归
nextNode(flowElements,defaultFlow,userTasks,task);
}
}
}
//如果是user用户审批节点
}else if(e instanceof UserTask){
userTasks.add(new TaskModel((UserTask) e,task));
}
}
}
}
如需指定下一步任务人员可以使用