maven:
<!--activiti--> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter-basic</artifactId> <version>6.0.0</version> </dependency>
启动报错:
一:Description:
Field restTemplate in com.vvar.cloudflow.base.utils.SimpleWebServiceUtil required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
The injection point has the following annotations:
解决
@Primary
@Bean
public TaskExecutor primaryTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
return executor;
}
二:nested exception is java.io.FileNotFoundException: class path resource [org/springframework/security/config/annotation/authentication/configurers/GlobalAuthenticationConfigurerAdapter.class] cannot be opened because it does not exist
解决集成 去掉org.activiti.spring.boot.SecurityAutoConfiguration.class
@SpringBootApplication(exclude={ org.activiti.spring.boot.SecurityAutoConfiguration.class })
#activiti配置 spring.activiti.database-schema-update=true spring.activiti.history-level=full spring.activiti.check-process-definitions=false
流程图:
在activiti6官网中下载,将activiti.app.war在tomcat 的webapps中,启动tomcat。输入网址:http://localhost:8080/activiti-app
账号 admin 密码test
进入Kickstart App,右上角新建(Create Process)
将流程图下载下来放进项目中processes文件夹下(在rescources目录下创建processes文件,如果processes没有流程图会报未找到文件的错误,activiti启动会扫描processes文件夹)
activiti设置用户和用户组:
https://blog.csdn.net/qq_29914837/article/details/92104148
部署流程:
public void deploy1( ){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); Deployment deployment = processEngine.getRepositoryService()//获取流程定义和部署对象相关的Service .createDeployment()//创建部署对象 .name("修改合同审核流程")//声明流程的名称 .addClasspathResource("processes/***.bpmn20.xml")//加载资源文件,一次只能加载一个文件 .deploy();//完成部署 System.out.println("部署ID:"+deployment.getId());//1 System.out.println("部署时间:"+deployment.getDeploymentTime()); }
提交申请:
public void startProcessInstance(){ String processDefinitionKey = "***";//***.bpmn20.xml中的id(流程图id) ProcessInstance processInstance = processEngine.getRuntimeService()// 与正在执行的流程实例和执行对象相关的Service .startProcessInstanceByKey(processDefinitionKey);// 使用流程定义的key启动流程实例,key对应leave.bpmn文件中id的属性 Task vacationApply = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); // for (String group : groups) { // taskService.addCandidateGroup(vacationApply.getId(), group);//设置处理组 // } // taskService.addCandidateUser(vacationApply.getId(), "userId");//设置处理人 System.out.println("流程实例ID:" + processInstance.getId());// 流程实例ID: System.out.println("流程定义ID:" + processInstance.getProcessDefinitionId());// 流 System.out.println("任务ID:" + vacationApply.getId()); }
处理任务:
public void completeMyPersonalTask(String taskId) throws ParseException { String taskIdFin = null; String execution = null; // List<Task> taskList = //taskService.createTaskQuery().taskAssignee("userId").list();//根据处理人查询任务 List<Task> tasks = taskService.createTaskQuery().taskCandidateOrAssigned("userId").list();//根据处理人或者处理组查询任务 if (CollectionUtils.isEmpty(tasks)) return ServerResponse.createByErrorMessage("你未有权限审批合同"); for (Task task : tasks) { if (task.getId().equals(taskId)) execution = task.getExecutionId();//执行对象id } Map<String, Object> args = new HashMap<>(); args.put("submitType", "y"); processEngine.getTaskService()// 与正在执行的任务管理相关的Service .complete(taskId,args); List<Task> taskList = taskService.createTaskQuery().taskCandidateOrAssigned("userId").list(); for (Task task : taskList) { if (task.getExecutionId().equals(execution)) taskIdFin = task.getId();//获取下个任务id } System.out.println("完成任务:任务ID:" + taskId); }
任务出结束后自动配置处理人或者处理组有多种方式,我采用了监听方式。其他网上有很多。
创建监听类,实现 TaskListener的notify方法
public class AdminTaskListenerImpl implements TaskListener { @Override public void notify(DelegateTask delegateTask) { delegateTask.addCandidateGroup("groupId");//设置处理组 delegateTaskaddCandidateGroups(Collection<String> candidateGroups)//设置多个处理组 delegateTask.addCandidateUser("userId"); //设置处理人 delegateTask.addCandidateUsers(Collection<String> candidateUsers); //设置多个处理人 } }
监听需要在流程图中进行配置。
如果需要使用@Autowired或者spring 注入时报空指针,处理办法如下:
@Component("AdminTaskListenerImpl") public class AdminTaskListenerImpl implements TaskListener { @Autowired private PositionRepository positionRepository; @Override public void notify(DelegateTask delegateTask) { List<Position> positions = positionRepository.findAllByApprovalProcess(ApprovalProcess.EMPLOYEE); List<String> positionIds = positions.stream().map(e -> e.getId().toString()).collect(Collectors.toList()); delegateTask.addCandidateGroups(positionIds); } }
在流程图监听配置修改成:create 设置成complete