Activiti如何与业务代码整合
1、请假流程,activiti 25张表存储流程走到哪一步,请假流程还包括请假当事人,请假内容,请假开始时间,请假结束时间,事由,状态类型由一个请假表存储。两个表关联就是通过businesskey业务主键。需要将请假单的id存入activiti表。
2、act_ru_execution流程实例执行表中间有一个businesskey,存储业务主键。流程实例执行一步,就会里面插入一条数据。
存储businesskey操作如下
package com.xiaofeiyang.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RuntimeService; import org.activiti.engine.runtime.ProcessInstance; /** * @author: yangchun * @description: * @date: Created in 2020-06-01 16:57 */ public class BusinessKeyAdd { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_1","1"); System.out.println(processInstance.getBusinessKey()); } }
3、流程没有走完的,流程挂起操作,流程废弃的时候使用
package com.xiaofeiyang.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.ProcessDefinition; import java.util.Date; import java.util.List; /** * @author: yangchun * @description: * @date: Created in 2020-06-03 15:12 */ public class SuspendActiviti { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("myProcess_1") .singleResult(); if(processDefinition.isSuspended()){ repositoryService.activateProcessDefinitionById(processDefinition.getId(),true,null); }else { repositoryService.suspendProcessDefinitionById(processDefinition.getId(),true,null); } } }
4、因为某些条件导致单个流程实例挂起
package com.xiaofeiyang.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.repository.ProcessDefinition; import org.activiti.engine.runtime.ProcessInstance; /** * @author: yangchun * @description: * @date: Created in 2020-06-03 15:12 */ public class SuspendProcessInstance { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey("myProcess_1").singleResult(); if(processInstance.isSuspended()){ runtimeService.activateProcessInstanceById(processInstance.getId()); }else { runtimeService.suspendProcessInstanceById(processInstance.getId()); } } }
5、任务分配
表达式方式进行分配,UEL,统一表达式语言。JavaEE规范,${变量}
package com.xiaofeiyang.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RuntimeService; import org.activiti.engine.runtime.ProcessInstance; import java.util.HashMap; import java.util.Map; /** * @author: yangchun * @description: * @date: Created in 2020-06-01 16:57 */ public class AssigneeUEL { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); Map<String,Object> assigned = new HashMap<>(); assigned.put("assigned0","yangchun"); assigned.put("assigned1","liujiawei"); assigned.put("assigned2","xiafeng"); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("assigned","1",assigned); System.out.println(processInstance.getBusinessKey()); } }
6、Create任务创建后触发,Assignment,分配后触发,Delete,触发,All所有事件发生触发。在task里面设置tasklistener就行了
package com.xiaofeiyang.activiti; import org.activiti.engine.delegate.DelegateTask; import org.activiti.engine.delegate.TaskListener; /** * @author: yangchun * @description: * @date: Created in 2020-06-06 17:19 */ public class ActivitiTaskListener implements TaskListener { @Override public void notify(DelegateTask delegateTask) { delegateTask.setAssignee("yangchun"); } }
7、查询个人代办任务,包含业务信息
package com.xiaofeiyang.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; /** * @author: yangchun * @description: * @date: Created in 2020-06-06 17:27 */ public class UserTaskQuery { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); RuntimeService runtimeService = processEngine.getRuntimeService(); Task task = taskService.createTaskQuery().processDefinitionKey("assigned") .taskAssignee("yangchun").singleResult(); String processInstanceId = task.getProcessInstanceId(); ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult(); String businessKey = processInstance.getBusinessKey(); System.out.println(businessKey); }