feat(REQ-3340) - starter 更新 API 如果存在相同方法名同异步的处理办法

This commit is contained in:
wangli 2025-01-22 10:39:34 +08:00
parent cdb9fcd23f
commit 4d39265ecd
3 changed files with 34 additions and 4 deletions

View File

@ -18,6 +18,8 @@ public class WorkflowEngineStarterRpcInvokeDTO implements Serializable {
private List<String> parameters;
private String parameterTypesMd5;
public String getClassName() {
return className;
}
@ -41,4 +43,12 @@ public class WorkflowEngineStarterRpcInvokeDTO implements Serializable {
public void setParameters(List<String> parameters) {
this.parameters = parameters;
}
public String getParameterTypesMd5() {
return parameterTypesMd5;
}
public void setParameterTypesMd5(String parameterTypesMd5) {
this.parameterTypesMd5 = parameterTypesMd5;
}
}

View File

@ -18,6 +18,7 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -36,6 +37,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@ -46,6 +48,7 @@ import java.util.Objects;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static cn.axzo.workflow.common.constant.StarterConstants.STARTER_INVOKE_MODE;
import static cn.axzo.workflow.common.enums.RpcInvokeModeEnum.SYNC;
@ -102,6 +105,9 @@ public class ComplexInvokeClient implements Client {
event.setClassName(metadata.targetType().getName());
event.setMethodName(metadata.method().getName());
Class<?>[] parameterTypes = metadata.method().getParameterTypes();
event.setParameterTypesMd5(StringUtils.collectionToCommaDelimitedString(Arrays.stream(parameterTypes).map(Class::getName).collect(Collectors.toList())));
List<String> args = new ArrayList<>();
event.setParameters(args);
buildArgs(request, metadata, args);

View File

@ -20,6 +20,7 @@ import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
@ -27,10 +28,12 @@ import org.springframework.web.bind.annotation.RequestParam;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.ConnectException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import static cn.axzo.workflow.common.enums.RpcInvokeModeEnum.SYNC;
@ -48,7 +51,7 @@ public class WorkflowEngineStarterRetryEventListener implements EventHandler, In
private final String currentApplicationName;
private WorkflowCoreService workflowCoreService;
private WorkflowManageService workflowManageService;
private final Map<String, InterfaceMapping> methodCache = new HashMap<>();
private final Map<String, Map<String, InterfaceMapping>> multiMethodCache = new HashMap<>();
class InterfaceMapping {
private final Object interfaceObject;
@ -97,7 +100,10 @@ public class WorkflowEngineStarterRetryEventListener implements EventHandler, In
Method[] methods = coreService.getDeclaredMethods();
for (Method method : methods) {
methodCache.put(method.getName(), new InterfaceMapping(workflowCoreService, method));
String parameterStr = StringUtils.collectionToCommaDelimitedString(Arrays.stream(method.getParameterTypes()).map(Class::getName).collect(Collectors.toList()));
Map<String, InterfaceMapping> methodCache = multiMethodCache.getOrDefault(parameterStr, new HashMap<>());
methodCache.put(parameterStr, new InterfaceMapping(workflowCoreService, method));
multiMethodCache.put(method.getName(), methodCache);
}
}
@ -110,7 +116,10 @@ public class WorkflowEngineStarterRetryEventListener implements EventHandler, In
Method[] methods = manageService.getDeclaredMethods();
for (Method method : methods) {
methodCache.put(method.getName(), new InterfaceMapping(workflowManageService, method));
String parameterStr = StringUtils.collectionToCommaDelimitedString(Arrays.stream(method.getParameterTypes()).map(Class::getName).collect(Collectors.toList()));
Map<String, InterfaceMapping> methodCache = multiMethodCache.getOrDefault(parameterStr, new HashMap<>());
methodCache.put(parameterStr, new InterfaceMapping(workflowManageService, method));
multiMethodCache.put(method.getName(), methodCache);
}
}
@ -120,7 +129,11 @@ public class WorkflowEngineStarterRetryEventListener implements EventHandler, In
log.info("WorkflowEngineClientRetryEventListener onEvent: {}", event.toPrettyJsonString());
WorkflowEngineStarterRpcInvokeDTO dto = event.normalizedData(WorkflowEngineStarterRpcInvokeDTO.class);
InterfaceMapping mapping = methodCache.getOrDefault(dto.getMethodName(), null);
Map<String, InterfaceMapping> methodCache = multiMethodCache.getOrDefault(dto.getMethodName(), new HashMap<>());
if (CollectionUtils.isEmpty(methodCache)) {
throw new WorkflowNoMethodException("Not methodCache found: " + dto.getMethodName());
}
InterfaceMapping mapping = methodCache.getOrDefault(dto.getParameterTypesMd5(), null);
if (Objects.isNull(mapping)) {
throw new WorkflowNoMethodException("Not method found: " + dto.getMethodName());
}
@ -135,6 +148,7 @@ public class WorkflowEngineStarterRetryEventListener implements EventHandler, In
if (log.isDebugEnabled()) {
log.debug("Event Invoke Result: {}", JSON.toJSONString(invoke));
}
log.info("WorkflowEngineClientRetryEventListener onEvent end!");
} catch (Throwable e) {
// 能抛出异常目前只有两种情况, 一个是网络异常, 另一个是对端服务内部异常
Throwable cause = getRealCause(e);