文章目录
- 一、数据对象DataObject
- 二、租户 Tenant
- 三、接收任务 ReceiveTask
- 案例
一、数据对象DataObject
DataObject可以⽤来定义⼀些流程的全局属性。
- 绘制流程图,并配置数据对象(不需要选择任意节点)
2. 编码与测试
/**
* 部署流程
*/
@Test
void deploy() {
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("process/请假流程-dataObj.bpmn20.xml")
.name("DataObj").deploy();
System.out.println("deployment.getId() = " + deployment.getId());
System.out.println("deployment.getName() = " + deployment.getName());
}
/**
* 启动流程
*/
@Test
void startFlow() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(PROC_KEY);
System.out.println("processInstance.getId() = " + processInstance.getId());
}
/**
* 查询数据对象
* Test2_DataObj : key: creater,name: 流程创建人,value: Huathy
*/
@Test
void getDataObj() {
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey(PROC_KEY).latestVersion().singleResult();
List<Execution> list = runtimeService.createExecutionQuery().processDefinitionId(pd.getId()).list();
list.forEach(execution -> {
DataObject obj = runtimeService.getDataObject(execution.getId(), "流程创建人");
String msg = String.format("key: %s,name: %s,value: %s", obj.getDataObjectDefinitionKey(), obj.getName(), obj.getValue());
logger.info(msg);
});
}
查看ACT_RU_VARIABLE
表,数据与ACT_RU_EXECUTION
表中与EXECUTION_ID_
相关联。
二、租户 Tenant
租户。多租户:单独的实例可以为多个组织服务。Flowable使用TenantID来区分流程归属于哪个租户。
部署与启动流程示例:部署的时候带上租户ID,后续的启动查询都要带上租户ID。
/**
* 部署流程
*/
@Test
void deploy() {
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("process/请假流程-dataObj.bpmn20.xml")
.name("DataObj").tenantId("A").deploy();
System.out.println("deployment.getId() = " + deployment.getId());
System.out.println("deployment.getName() = " + deployment.getName());
}
/**
* 启动流程
* processInstance.getId() = 0e33c8a0-a7b4-11ed-9f55-00155d067d63
* org.flowable.common.engine.api.FlowableObjectNotFoundException: Process definition with key 'qjlc-dataObj' and tenantId 'B' was not found
*/
@Test
void startFlow() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKeyAndTenantId(PROC_KEY,"A");
System.out.println("processInstance.getId() = " + processInstance.getId());
ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKeyAndTenantId(PROC_KEY,"B");
System.out.println("processInstance2.getId() = " + processInstance2.getId());
}
三、接收任务 ReceiveTask
接收任务(ReceiveTask),是bpm2.0规范的一种任务,是一种等待任务,当该任务到达的时候,它不做任何逻辑,而是被动地等待用户Trigger。 当流程流转到接收任务,流程状态会保存到数据库(ACT_RU_EXECUTION表会维护该任务)。
使用场景:不明确的阻塞。例如一个复杂的计算需要等待很多条件,这些条件需要人工判断,则可以手动触发。
案例
- 绘制流程图(信封✉图标)
- 部署流程与启动流程(启动流程后,ACT_RU_TASK表并没有数据,而数据存放在ACT_RU_EXECUTION表)
/** 部署流程 */
@Test
void deploy() {
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("process/签收任务.bpmn20.xml")
.name("ReceiveTask").deploy();
logger.info("deployment.getId() = " + deployment.getId());
logger.info("deployment.getName() = " + deployment.getName());
}
/** 启动流程 */
@Test
void startFlow() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(PROC_KEY);
logger.info("processInstance.getId() = " + processInstance.getId());
}
- 获取执行实例
@Test
void getExecution() {
List<Execution> list = runtimeService.createExecutionQuery()
.processDefinitionKey(PROC_KEY).list();
list.forEach(execution -> {
String msg = String.format("exeId: %s,exeName: %s, actID: %s", execution.getId(), execution.getName(),execution.getActivityId());
logger.info(msg);
});
}
- 触发流程(由于trigger方法需要传入
执行实例ID
,所以可以根据ReceiveTask的节点名称,查询当前待触发的流程实例ID。ACT_RU_EXECUTION表的数据在trigger后会删除记录,历史记录保存到ACT_HI_ACTINST表)
@Test
void triggerAndQuery() {
List<Execution> list = runtimeService.createExecutionQuery()
.processDefinitionKey(PROC_KEY).activityId("cksj").list();
list.forEach(execution -> {
System.out.println("execution.getActivityId() = " + execution.getActivityId());
runtimeService.trigger(execution.getId());
});
}