Flowable服务组件-扩展组件

Flowable服务组件-扩展组件

扩展组件


文章目录

  • Flowable服务组件-扩展组件
  • 前言
    • Flowable给我们提供了非常丰富的组件,但是在实际场景中,我们有需要企业个性化的组件,如何扩展自己的组件至关重要
  • 一、扩展微服务回调组件
  • 二、程序步骤
    • 1.定义我们的扩展组件 SpringCloudCallTaskActivityBehavior
    • 2.定义自定义ActivityBehaviorFactory
    • 3.定义自定义ServiceTaskParseHandler
    • 3.除了定义组件和handler我们还需要自定义校验
      • 3.1自定义校验ServiceTaskValidator
      • 3.1自定义校验工厂 ProcessValidatorFactory
    • 4.还没有完,我们要把我们定义的这些组件交给spring容器然后给引擎注入进去
  • 总结


前言

Flowable给我们提供了非常丰富的组件,但是在实际场景中,我们有需要企业个性化的组件,如何扩展自己的组件至关重要

Flowable服务组件-扩展组件_第1张图片

一、扩展微服务回调组件

现在企业中使用springcloud居多,如果在审批流程中需要调用微服务去和业务系统就行交互就显得很重要,但是如何实现它呢,接下来由我带着大家慢慢实现微服务回调组件

二、程序步骤

1.定义我们的扩展组件 SpringCloudCallTaskActivityBehavior

代码如下:

@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动态调用服务

2.定义自定义ActivityBehaviorFactory

代码如下:

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);
    }

3.定义自定义ServiceTaskParseHandler

代码如下:

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);
    }
}

3.除了定义组件和handler我们还需要自定义校验

3.1自定义校验ServiceTaskValidator

代码如下:

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");
        }
    }

3.1自定义校验工厂 ProcessValidatorFactory

代码如下:

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;
    }
}

4.还没有完,我们要把我们定义的这些组件交给spring容器然后给引擎注入进去

代码如下:

.....基本配置省略
   //自定义组件
        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可读性很高,代码质量也非常不错,推荐给大家。
Flowable服务组件-扩展组件_第2张图片

你可能感兴趣的:(Flowable组件库,java,spring,cloud,微服务,flowable)