RuntimeService 运行服务类
支持启动的方式
流程定义: 从这里获取资源文件.
执行实例: 流程实例中执行的每个环节.流程实例: 一个流程实例包括所有运行的节点,一个流程中流程实例只有一个.
启动一个实例:
public void startProcessInstanceByKey() {String processDefinitionKey = "leave";ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);System.out.println(processInstance.getId() + "," + processInstance.getActivityId());}
查询该受理人的任务:
public void queryMyTask() {String processDefinitionKey = "leave";String assignee = "王五";List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).processDefinitionKey(processDefinitionKey).list();}
完成任务:
public void completeMyTask() {String taskId = "54502";taskService.complete(taskId);}
查找运行实例的状态:
方式1:
public void queryProcessInstanceState() {ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId("59501")//.deploymentId().singleResult();if (processInstance != null) {System.out.println("当前流程实例正在运行");} else {System.out.println("当前流程实例已经结束");}}
方式2:
public void queryProcessInstanceState2() {String processInstanceId = "59501";HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();if (historicProcessInstance.getEndTime() == null) {System.out.println("当前流程实例正在运行");} else {System.out.println("当前流程实例已经结束");}}
流程实例执行完成,相应的execution表中数据将被删除.
查询执行实例
public void createExecutionQuery() {List<Execution> executions = runtimeService.createExecutionQuery().list();for (Execution execution : executions) {System.out.println(execution.getId() + "," + execution.getActivityId());}}
查询历史流程实例:
public void createHistoricProcessInstanceQuery() {String processInstanceId = "59501";HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();System.out.println("流程定义ID:" + hpi.getProcessDefinitionId());System.out.println("流程实例ID:" + hpi.getId());System.out.println(hpi.getStartTime());System.out.println(hpi.getStartActivityId());System.out.println(hpi.getEndTime());}
查询历史活动实例:
public void createHistoricActivityInstanceQuery() {List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery().list();for (HistoricActivityInstance hai : list) {System.out.println(hai.getId());}}
查询历史任务实例:
//select distinct RES.* from ACT_HI_TASKINST RES order by RES.ID_ ascpublic void createHistoricTaskInstanceQuery() {List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().list();}
设置流程发起人:
方式1 :
public void setAuthenticatedUserId1() {String authenticatedUserId = "朱元璋";identityService.setAuthenticatedUserId(authenticatedUserId);String processDefinitionKey = "leave";ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);System.out.println(processInstance.getId() + "," + processInstance.getActivityId());}
方式2 :
public void setAuthenticatedUserId2() {String authenticatedUserId = "朱重八";Authentication.setAuthenticatedUserId(authenticatedUserId);String processDefinitionKey = "leave";ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);System.out.println(processInstance.getId() + "," + processInstance.getActivityId());}
initiator结合流程实例启动人使用:
启动一个流程实例后会发现act_ru_variable会多出一条参数数据.
dataobject的使用:
部署相关的流程,启动流程后variable表中添加3条数据
log4j.properties中配置(打印variable的日志信息):
获取DataObject参数:
方式1:
/*** select * from ACT_RU_VARIABLE where EXECUTION_ID_ = ? and NAME_= ? and TASK_ID_ is null* - ==> Parameters: 77001(String), day(String)*/@Testpublic void getDataObject() {String executionId = "77001";String dataObject = "天数";DataObject dataObject1 = runtimeService.getDataObject(executionId, dataObject);if (dataObject1 != null) {System.out.println(dataObject1.getDataObjectDefinitionKey());System.out.println(dataObject1.getDescription());System.out.println(dataObject1.getExecutionId());System.out.println(dataObject1.getName());System.out.println(dataObject1.getValue());System.out.println(dataObject1.getType());}}
方式2:
/*** select * from ACT_RU_VARIABLE where EXECUTION_ID_ = ? and TASK_ID_ is null*/@Testpublic void getDataObject2() {String executionId = "77001";Map<String, DataObject> dataObject1 = runtimeService.getDataObjects(executionId);Set<Map.Entry<String, DataObject>> entries = dataObject1.entrySet();for (Map.Entry<String, DataObject> dataObjectEntry : entries) {DataObject dataObject = dataObjectEntry.getValue();if (dataObject != null) {System.out.println(dataObject.getDataObjectDefinitionKey());System.out.println(dataObject.getDescription());System.out.println(dataObject.getExecutionId());System.out.println(dataObject.getName());System.out.println(dataObject.getValue());System.out.println(dataObject.getType());}}}
流程实例的删除以及级联删除:
@Testpublic void deleteProcessInstance() {String processInstanceId = "72001";String deleteReason = "我想删除";runtimeService.deleteProcessInstance(processInstanceId, deleteReason);}//级联删除@Testpublic void deleteProcessInstanceCascade() {String processInstanceId = "69501";String deleteReason = "我想删除";//processEngineConfiguration方式// ProcessEngineConfigurationImpl processEngineConfiguration = //(ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration(); //processEngineConfiguration.getExecutionEntityManager()//.deleteProcessInstance(processInstanceId,deleteReason,true);//DeleteProcessInstanceCaCadeCmd方式删除managementService.executeCommand(new DeleteProcessInstanceCaCadeCmd(processInstanceId, deleteReason));}
获取流程实例运行节点:
public void getActiveActivityIds() {String executionId = "77005";List<String> activeActivityIds = runtimeService.getActiveActivityIds(executionId);
}
租户方式部署以及启动:
//根据流程定义的id启动
public void startProcessInstanceById() {String processDefinitionId = "dataobject:1:74504";ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId);
}
//根据流程定义的key和租户id
public void startProcessInstanceByKeyAndTenantId() {ProcessInstance processInstance = runtimeService.startProcessInstanceByKeyAndTenantId("leave", "001");
}
自定义流程实例助手类:
首先,重写里面的方法
//重写MyProcessInstanceHelper集成ProcessInstanceHelper ,并重写里面的方法
public class MyProcessInstanceHelper extends ProcessInstanceHelper {@Overridepublic ProcessInstance createProcessInstance(ProcessDefinition processDefinition, String businessKey, String processInstanceName, Map<String, Object> variables, Map<String, Object> transientVariables) {return super.createProcessInstance(processDefinition, businessKey, processInstanceName, variables, transientVariables);}@Overridepublic ProcessInstance createProcessInstance(ProcessDefinition processDefinition, String businessKey, String processInstanceName, String overrideDefinitionTenantId, Map<String, Object> variables, Map<String, Object> transientVariables, String callbackId, String callbackType, boolean startProcessInstance) {System.out.println("MyProcessInstanceHelper:createProcessInstance");return super.createProcessInstance(processDefinition, businessKey, processInstanceName, overrideDefinitionTenantId, variables, transientVariables, callbackId, callbackType, startProcessInstance);}
}
然后,通过配置文件注入进来
<property name="processInstanceHelper" ><bean class="com.heima.bpmn.MyProcessInstanceHelper"></bean>
</property>
流程定义的挂起与激活:
判断流程定义是否被挂起
public void isProcessDefinitionSuspended() {String processDefinitionId = "leave:2:67004";boolean processDefinitionSuspended = repositoryService.isProcessDefinitionSuspended(processDefinitionId);System.out.println(processDefinitionSuspended);
}
挂起一个流程实例
public void suspendProcessDefinitionById() {String processDefinitionId = "leave:2:67004";repositoryService.suspendProcessDefinitionById(processDefinitionId,true,null);
}
释放流程实例
public void activateProcessDefinitionById() {String processDefinitionId = "leave:2:67004";repositoryService.activateProcessDefinitionById(processDefinitionId);
'}