扩展组件
现在企业中使用springcloud居多,如果在审批流程中需要调用微服务去和业务系统就行交互就显得很重要,但是如何实现它呢,接下来由我带着大家慢慢实现微服务回调组件
代码如下:
@Slf4j
public class SpringCloudCallTaskActivityBehavior extends AbstractCustomBpmnActivityBehavior {
//nacos服务id
protected Expression serviceId;
//url
protected Expression url;
//请求方法名
protected Expression method;
//参数
protected Expression params;
//是否保存请求结果
protected Expression saveResponseParameters;
//保存请求结果的变量名
protected Expression responseVariableName;
@Override
public void execute(DelegateExecution execution) {
SpringCloudBusinessCallModel callModel = this.createSpringCloudBusinessCallModel(execution, serviceId, url, method, params);
DynamicFeignClientFactory<DynamicClient> dynamicFeignClientFactory = SpringBpmnUtil.getBean(DynamicFeignClientFactory.class);
DynamicClient dynamicClient = dynamicFeignClientFactory.getFeignClient(DynamicClient.class, callModel.getServiceId(), callModel.getUrl());
Map<String, Object> params = FastJsonUtils.fastJsonBean2Map(callModel);
JsonNode jsonNode = null;
try {
JSONObject responseValueJson = dynamicClient.postExecute(params);
ObjectMapper mapper = new ObjectMapper();
jsonNode = mapper.readTree(responseValueJson.toJSONString());
} catch (Exception e) {
log.error("调用出差或转化出错", e);
}
String saveResponseParametersStr = this.getStrExpression(saveResponseParameters, execution);
if (StringUtils.isNotBlank(saveResponseParametersStr) && "true".equalsIgnoreCase(saveResponseParametersStr)) {
execution.setVariable(this.getStrExpression(responseVariableName, execution), jsonNode);
}
this.leave(execution);
}
}
注:如果springcloud动态调用服务不清楚,可以参考我们以前的博客
springcloud动态调用服务
代码如下:
public class CustomActivityBehaviorFactory extends DefaultActivityBehaviorFactory {
//创建自定义SpringCloudCallTaskActivityBehavior
public SpringCloudCallTaskActivityBehavior createSpringCloudCallTaskActivityBehavior(ServiceTask serviceTask) {
List<FieldDeclaration> fieldDeclarations = super.createFieldDeclarations(serviceTask.getFieldExtensions());
return (SpringCloudCallTaskActivityBehavior) ClassDelegate.defaultInstantiateDelegate(SpringCloudCallTaskActivityBehavior.class, fieldDeclarations);
}
代码如下:
public class CustomServiceTaskParseHandler extends ServiceTaskParseHandler {
@Override
protected void executeParse(BpmnParse bpmnParse, ServiceTask serviceTask) {
if (StringUtils.isNotEmpty(serviceTask.getType())) {
if ("sc".equalsIgnoreCase(serviceTask.getType())){
CustomActivityBehaviorFactory activityBehaviorFactory = (CustomActivityBehaviorFactory) bpmnParse.getActivityBehaviorFactory();
serviceTask.setBehavior(activityBehaviorFactory.createSpringCloudCallTaskActivityBehavior(serviceTask));
}
super.executeParse(bpmnParse, serviceTask);
}
}
代码如下:
public class CustomServiceTaskValidator extends ServiceTaskValidator {
//微服务任务
public static final String SC_TASK = "sc";
//校验服务任务
@Override
protected void verifyType(Process process, ServiceTask serviceTask, List<ValidationError> errors) {
if (StringUtils.isNotEmpty(serviceTask.getType())) {
switch (serviceTask.getType()) {
case SC_TASK:
this.validateFieldDeclarationsForSpringCloud(process, serviceTask, serviceTask.getFieldExtensions(), errors);
break;
default:
super.verifyType(process, serviceTask, errors);
break;
}
}
}
//校验springcloud类型的服务任务
private void validateFieldDeclarationsForSpringCloud(Process process, TaskWithFieldExtensions task, List<FieldExtension> fieldExtensions, List<ValidationError> errors) {
boolean serviceIdDefined = false;
boolean urlDefined = false;
for (FieldExtension fieldExtension : fieldExtensions) {
if (fieldExtension.getFieldName().equals("serviceId")) {
serviceIdDefined = true;
}
if (fieldExtension.getFieldName().equals("url")) {
urlDefined = true;
}
}
if (!serviceIdDefined) {
addError(errors, "flowable-sc-no-serviceId", process, task, "ScCall节点没有配置serviceId");
}
if (!urlDefined) {
addError(errors, "flowable-sc-no-url", process, task, "ScCall节点没有配置url");
}
}
代码如下:
public class CustomProcessValidatorFactory extends ProcessValidatorFactory {
@Override
public ProcessValidator createDefaultProcessValidator() {
ProcessValidatorImpl processValidator = new ProcessValidatorImpl();
ValidatorSet validatorSet = new ValidatorSetFactory().createFlowableExecutableProcessValidatorSet();
validatorSet.removeValidator(ServiceTaskValidator.class);
validatorSet.addValidator(new CustomServiceTaskValidator());
processValidator.addValidatorSet(validatorSet);
return processValidator;
}
}
代码如下:
.....基本配置省略
//自定义组件
List<BpmnParseHandler> customServiceTaskParseHandlers = new ArrayList<>();
customServiceTaskParseHandlers.add(customServiceTaskParseHandler());
configuration.setCustomDefaultBpmnParseHandlers(customServiceTaskParseHandlers);
configuration.setActivityBehaviorFactory(customActivityBehaviorFactory());
configuration.setProcessValidator(customProcessValidatorFactory().createDefaultProcessValidator());
}
@Bean
public CustomServiceTaskParseHandler customServiceTaskParseHandler() {
return new CustomServiceTaskParseHandler();
}
@Bean
public CustomActivityBehaviorFactory customActivityBehaviorFactory() {
return new CustomActivityBehaviorFactory();
}
@Bean
public CustomProcessValidatorFactory customProcessValidatorFactory() {
return new CustomProcessValidatorFactory();
}
以上就是今天要讲的内容,这样子我们就可以随意扩展自己的自定义组件了,记得我们所有的扩展都是为企业服务的,不是为了扩展而扩展,实际能落地才是具备价值的。在这里也感谢贺波同学提供的支持,他写的一本activiti可读性很高,代码质量也非常不错,推荐给大家。