【代码评审修改】 异步 http 触发器代码评审修改
This commit is contained in:
parent
d0fbb7677c
commit
5e31062d6b
|
@ -19,13 +19,6 @@ public interface BpmProcessInstanceApi {
|
||||||
*/
|
*/
|
||||||
String createProcessInstance(Long userId, @Valid BpmProcessInstanceCreateReqDTO reqDTO);
|
String createProcessInstance(Long userId, @Valid BpmProcessInstanceCreateReqDTO reqDTO);
|
||||||
|
|
||||||
// TODO @jason:新增 BpmProcessTaskApi 接口,这个要不改成 triggerTask,保持通用性(和 flowable 保持一致)
|
|
||||||
/**
|
|
||||||
* 异步 HTTP 请求触发器回调, 为了唤醒流程继续执行
|
|
||||||
*
|
|
||||||
* @param processInstanceId 流程实例编号
|
|
||||||
* @param callbackId 回调编号, 对应 ReceiveTask Id TODO @jason:改成 taskDefineKey
|
|
||||||
*/
|
|
||||||
void asyncHttpTriggerCallback(String processInstanceId, String callbackId);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package cn.iocoder.yudao.module.bpm.api.task;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 流程任务 Api 接口
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
public interface BpmProcessTaskApi {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 触发流程任务的执行
|
||||||
|
*
|
||||||
|
* @param processInstanceId 流程实例编号
|
||||||
|
* @param taskDefineKey 任务 Key
|
||||||
|
*/
|
||||||
|
void triggerTask(@NotEmpty(message = "流程实例的编号不能为空") String processInstanceId,
|
||||||
|
@NotEmpty(message = "任务 Key 不能为空") String taskDefineKey);
|
||||||
|
}
|
|
@ -19,7 +19,7 @@ public enum BpmTriggerTypeEnum implements ArrayValuable<Integer> {
|
||||||
HTTP_REQUEST(1, "发起 HTTP 请求"),
|
HTTP_REQUEST(1, "发起 HTTP 请求"),
|
||||||
FORM_UPDATE(2, "更新流程表单数据"),
|
FORM_UPDATE(2, "更新流程表单数据"),
|
||||||
FORM_DELETE(3, "删除流程表单数据"),
|
FORM_DELETE(3, "删除流程表单数据"),
|
||||||
ASYNC_HTTP_REQUEST(4, "发起异步 HTTP 请求"); // TODO @jason:HTTP_REQUEST_ASYNC
|
HTTP_REQUEST_ASYNC(4, "发起异步 HTTP 请求");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 触发器执行动作类型
|
* 触发器执行动作类型
|
||||||
|
|
|
@ -2,12 +2,10 @@ package cn.iocoder.yudao.module.bpm.api.task;
|
||||||
|
|
||||||
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
|
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
|
||||||
import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService;
|
import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService;
|
||||||
import cn.iocoder.yudao.module.bpm.service.task.BpmTaskService;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.validation.annotation.Validated;
|
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flowable 流程实例 Api 实现类
|
* Flowable 流程实例 Api 实现类
|
||||||
|
@ -22,17 +20,8 @@ public class BpmProcessInstanceApiImpl implements BpmProcessInstanceApi {
|
||||||
@Resource
|
@Resource
|
||||||
private BpmProcessInstanceService processInstanceService;
|
private BpmProcessInstanceService processInstanceService;
|
||||||
|
|
||||||
@Resource
|
|
||||||
private BpmTaskService bpmTaskService;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String createProcessInstance(Long userId, @Valid BpmProcessInstanceCreateReqDTO reqDTO) {
|
public String createProcessInstance(Long userId, @Valid BpmProcessInstanceCreateReqDTO reqDTO) {
|
||||||
return processInstanceService.createProcessInstance(userId, reqDTO);
|
return processInstanceService.createProcessInstance(userId, reqDTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void asyncHttpTriggerCallback(String processInstanceId, String callbackId) {
|
|
||||||
bpmTaskService.triggerReceiveTask(processInstanceId, callbackId);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package cn.iocoder.yudao.module.bpm.api.task;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.task.BpmTaskService;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 流程任务 Api 实现类
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Validated
|
||||||
|
public class BpmProcessTaskApiImpl implements BpmProcessTaskApi {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private BpmTaskService bpmTaskService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void triggerTask(String processInstanceId, String taskDefineKey) {
|
||||||
|
bpmTaskService.triggerTask(processInstanceId, taskDefineKey);
|
||||||
|
}
|
||||||
|
}
|
|
@ -389,13 +389,11 @@ public class BpmSimpleModelNodeVO {
|
||||||
@Schema(description = "请求返回处理设置", example = "[]")
|
@Schema(description = "请求返回处理设置", example = "[]")
|
||||||
private List<KeyValue<String, String>> response;
|
private List<KeyValue<String, String>> response;
|
||||||
|
|
||||||
// TODO @jason:改成 callbackTaskDefineKey
|
|
||||||
/**
|
/**
|
||||||
* 异步 Http 请求,需要指定回调 ID,用于回调执行
|
* 异步 Http 请求,需要指定回调任务 Key,用于回调执行
|
||||||
*/
|
*/
|
||||||
@Schema(description = "回调 ID", example = "xxx", hidden = true)
|
@Schema(description = "回调任务 Key", example = "xxx", hidden = true)
|
||||||
private String callbackId;
|
private String callbackTaskDefineKey;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Schema(description = "流程表单触发器设置", example = "{}")
|
@Schema(description = "流程表单触发器设置", example = "{}")
|
||||||
|
|
|
@ -22,7 +22,6 @@ import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.annotation.security.PermitAll;
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import org.flowable.engine.history.HistoricProcessInstance;
|
import org.flowable.engine.history.HistoricProcessInstance;
|
||||||
import org.flowable.engine.repository.ProcessDefinition;
|
import org.flowable.engine.repository.ProcessDefinition;
|
||||||
|
@ -179,14 +178,4 @@ public class BpmProcessInstanceController {
|
||||||
public CommonResult<BpmProcessInstanceBpmnModelViewRespVO> getProcessInstanceBpmnModelView(@RequestParam(value = "id") String id) {
|
public CommonResult<BpmProcessInstanceBpmnModelViewRespVO> getProcessInstanceBpmnModelView(@RequestParam(value = "id") String id) {
|
||||||
return success(processInstanceService.getProcessInstanceBpmnModelView(id));
|
return success(processInstanceService.getProcessInstanceBpmnModelView(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO @jason:要不去掉这个接口,单体通过 asyncHttpTriggerCallback?
|
|
||||||
@PostMapping("/http-trigger/callback")
|
|
||||||
@Operation(summary = "异步 HTTP 请求触发器回调")
|
|
||||||
@PermitAll // 允许外部调用,不需要登录。 TODO @芋艿 需要加一下验证签名吗?
|
|
||||||
public CommonResult<Boolean> httpTriggerCallback(@Valid @RequestBody BpmHttpTriggerCallbackReqVO reqVO) {
|
|
||||||
taskService.triggerReceiveTask(reqVO.getId(), reqVO.getCallbackId());
|
|
||||||
return success(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
package cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
// TODO @jason:要不去掉这个接口,单体通过 asyncHttpTriggerCallback?
|
|
||||||
@Schema(description = "管理后台 - Bpm 异步 Http 触发器请求回调 Request VO")
|
|
||||||
@Data
|
|
||||||
public class BpmHttpTriggerCallbackReqVO {
|
|
||||||
|
|
||||||
@Schema(description = "流程实例的编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "dca1cdcc-b8fe-11ef-99b5-00ff4722db8b")
|
|
||||||
@NotEmpty(message = "流程实例的编号不能为空")
|
|
||||||
private String id;
|
|
||||||
|
|
||||||
@Schema(description = "回调编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "dca1cdcc-b8fe-11ef-99b5-01ff4722db8b")
|
|
||||||
@NotEmpty(message = "回调编号不能为空")
|
|
||||||
private String callbackId;
|
|
||||||
|
|
||||||
}
|
|
|
@ -109,7 +109,7 @@ public class BpmTaskEventListener extends AbstractFlowableEngineEventListener {
|
||||||
// 2.2 延迟器超时处理
|
// 2.2 延迟器超时处理
|
||||||
} else if (ObjectUtil.equal(bpmTimerBoundaryEventType, BpmBoundaryEventTypeEnum.DELAY_TIMER_TIMEOUT)) {
|
} else if (ObjectUtil.equal(bpmTimerBoundaryEventType, BpmBoundaryEventTypeEnum.DELAY_TIMER_TIMEOUT)) {
|
||||||
String taskKey = boundaryEvent.getAttachedToRefId();
|
String taskKey = boundaryEvent.getAttachedToRefId();
|
||||||
taskService.triggerReceiveTask(event.getProcessInstanceId(), taskKey);
|
taskService.triggerTask(event.getProcessInstanceId(), taskKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.module.bpm.enums.definition.BpmTriggerTypeEnum.ASYNC_HTTP_REQUEST;
|
import static cn.iocoder.yudao.module.bpm.enums.definition.BpmTriggerTypeEnum.HTTP_REQUEST_ASYNC;
|
||||||
import static cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnModelConstants.*;
|
import static cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnModelConstants.*;
|
||||||
import static cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils.*;
|
import static cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils.*;
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
|
@ -754,6 +754,17 @@ public class SimpleModelUtils {
|
||||||
public List<? extends FlowElement> convertList(BpmSimpleModelNodeVO node) {
|
public List<? extends FlowElement> convertList(BpmSimpleModelNodeVO node) {
|
||||||
Assert.notNull(node.getTriggerSetting(), "触发器节点设置不能为空");
|
Assert.notNull(node.getTriggerSetting(), "触发器节点设置不能为空");
|
||||||
List<FlowElement> flowElements = new ArrayList<>(2);
|
List<FlowElement> flowElements = new ArrayList<>(2);
|
||||||
|
// 异步 HTTP 请求。需要附加一个 ReceiveTask、发起请求后、等待回调执行
|
||||||
|
if (HTTP_REQUEST_ASYNC.getType().equals(node.getTriggerSetting().getType())) {
|
||||||
|
Assert.notNull(node.getTriggerSetting().getHttpRequestSetting(), "触发器 HTTP 请求设置不能为空");
|
||||||
|
String attachNodeId = "Activity_" + IdUtil.fastUUID();
|
||||||
|
ReceiveTask receiveTask = new ReceiveTask();
|
||||||
|
receiveTask.setId(attachNodeId);
|
||||||
|
receiveTask.setName("异步 HTTP 请求");
|
||||||
|
node.setAttachNodeId(attachNodeId);
|
||||||
|
node.getTriggerSetting().getHttpRequestSetting().setCallbackTaskDefineKey(attachNodeId); // 设置 callbackTaskDefineKey
|
||||||
|
flowElements.add(receiveTask);
|
||||||
|
}
|
||||||
// 触发器使用 ServiceTask 来实现
|
// 触发器使用 ServiceTask 来实现
|
||||||
ServiceTask serviceTask = new ServiceTask();
|
ServiceTask serviceTask = new ServiceTask();
|
||||||
serviceTask.setId(node.getId());
|
serviceTask.setId(node.getId());
|
||||||
|
@ -761,28 +772,13 @@ public class SimpleModelUtils {
|
||||||
serviceTask.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
|
serviceTask.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
|
||||||
serviceTask.setImplementation("${" + BpmTriggerTaskDelegate.BEAN_NAME + "}");
|
serviceTask.setImplementation("${" + BpmTriggerTaskDelegate.BEAN_NAME + "}");
|
||||||
addExtensionElement(serviceTask, TRIGGER_TYPE, node.getTriggerSetting().getType());
|
addExtensionElement(serviceTask, TRIGGER_TYPE, node.getTriggerSetting().getType());
|
||||||
flowElements.add(serviceTask);
|
|
||||||
|
|
||||||
// 异步 HTTP 请求。需要附加一个 ReceiveTask、发起请求后、等待回调执行
|
|
||||||
// TODO @jason:这里能挪到最后处理么?这样,代码的整体性更好;
|
|
||||||
if (ASYNC_HTTP_REQUEST.getType().equals(node.getTriggerSetting().getType())) {
|
|
||||||
Assert.notNull(node.getTriggerSetting().getHttpRequestSetting(), "触发器 HTTP 请求设置不能为空");
|
|
||||||
String attachNodeId = "Activity_" + IdUtil.fastUUID();
|
|
||||||
ReceiveTask receiveTask = new ReceiveTask();
|
|
||||||
receiveTask.setId(attachNodeId);
|
|
||||||
receiveTask.setName("异步 HTTP 请求");
|
|
||||||
node.setAttachNodeId(attachNodeId);
|
|
||||||
node.getTriggerSetting().getHttpRequestSetting().setCallbackId(attachNodeId); // 设置 receiveId
|
|
||||||
flowElements.add(receiveTask);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO @jason:是不是挪到 flowElements.add(serviceTask); 之前哈,因为它在处理 serviceTask
|
|
||||||
if (node.getTriggerSetting().getHttpRequestSetting() != null) {
|
if (node.getTriggerSetting().getHttpRequestSetting() != null) {
|
||||||
addExtensionElementJson(serviceTask, TRIGGER_PARAM, node.getTriggerSetting().getHttpRequestSetting());
|
addExtensionElementJson(serviceTask, TRIGGER_PARAM, node.getTriggerSetting().getHttpRequestSetting());
|
||||||
}
|
}
|
||||||
if (node.getTriggerSetting().getFormSettings() != null) {
|
if (node.getTriggerSetting().getFormSettings() != null) {
|
||||||
addExtensionElementJson(serviceTask, TRIGGER_PARAM, node.getTriggerSetting().getFormSettings());
|
addExtensionElementJson(serviceTask, TRIGGER_PARAM, node.getTriggerSetting().getFormSettings());
|
||||||
}
|
}
|
||||||
|
flowElements.add(serviceTask);
|
||||||
return flowElements;
|
return flowElements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -276,13 +276,15 @@ public interface BpmTaskService {
|
||||||
*/
|
*/
|
||||||
void processTaskTimeout(String processInstanceId, String taskDefineKey, Integer handlerType);
|
void processTaskTimeout(String processInstanceId, String taskDefineKey, Integer handlerType);
|
||||||
|
|
||||||
// TODO @jason:改成 triggerTask。然后,“触发 ReceiveTask,让流程继续执行”,改成一些调用场景
|
|
||||||
/**
|
/**
|
||||||
* 触发 ReceiveTask,让流程继续执行
|
* 触发流程任务 (ReceiveTask) 的执行
|
||||||
|
* <p>
|
||||||
|
* 1. Simple 模型异步 HTTP 请求触发器节点的回调,触发流程继续执行
|
||||||
|
* 2. Simple 模型延迟器节点,到时触发流程继续执行
|
||||||
*
|
*
|
||||||
* @param processInstanceId 流程示例编号
|
* @param processInstanceId 流程示例编号
|
||||||
* @param taskDefineKey 任务 Key
|
* @param taskDefineKey 任务 Key
|
||||||
*/
|
*/
|
||||||
void triggerReceiveTask(String processInstanceId, String taskDefineKey);
|
void triggerTask(String processInstanceId, String taskDefineKey);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1323,7 +1323,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void triggerReceiveTask(String processInstanceId, String taskDefineKey) {
|
public void triggerTask(String processInstanceId, String taskDefineKey) {
|
||||||
Execution execution = runtimeService.createExecutionQuery()
|
Execution execution = runtimeService.createExecutionQuery()
|
||||||
.processInstanceId(processInstanceId)
|
.processInstanceId(processInstanceId)
|
||||||
.activityId(taskDefineKey)
|
.activityId(taskDefineKey)
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class BpmAsyncHttpRequestTrigger extends BpmAbstractHttpRequestTrigger {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BpmTriggerTypeEnum getType() {
|
public BpmTriggerTypeEnum getType() {
|
||||||
return BpmTriggerTypeEnum.ASYNC_HTTP_REQUEST;
|
return BpmTriggerTypeEnum.HTTP_REQUEST_ASYNC;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -43,7 +43,7 @@ public class BpmAsyncHttpRequestTrigger extends BpmAbstractHttpRequestTrigger {
|
||||||
// 2.2 设置请求体
|
// 2.2 设置请求体
|
||||||
MultiValueMap<String, String> body = buildHttpBody(processInstance, setting.getBody());
|
MultiValueMap<String, String> body = buildHttpBody(processInstance, setting.getBody());
|
||||||
// TODO @芋艿:【异步】在看看
|
// TODO @芋艿:【异步】在看看
|
||||||
body.add("callbackId", setting.getCallbackId()); // 异步请求 callbackId 需要传给被调用方,用于回调执行
|
body.add("callbackId", setting.getCallbackTaskDefineKey()); // 异步请求 callbackId 需要传给被调用方,用于回调执行
|
||||||
|
|
||||||
// 3. 发起请求
|
// 3. 发起请求
|
||||||
sendHttpRequest(setting.getUrl(), headers, body);
|
sendHttpRequest(setting.getUrl(), headers, body);
|
||||||
|
|
Loading…
Reference in New Issue