Flowable监听器动态调用Springcloud接口

由于今年接到一个大的项目,食品安全全链路追溯项目,流程就很少发博客了,但是我还一直坚持到一线,不停的完善流程的项目。

在很多场景中,我们都需要在监听器中去动态调用业务接口,如果企业内部使用的微服务,nacos,我们又当怎么去动态调用接口呢

目标:只需要配置,不需要硬编码来调用接口

  1. 动态配置调用

  • 第一 、编写动态client工厂类 DynamicFeignClientFactory

@Component
public class DynamicFeignClientFactory<T> {

    private FeignClientBuilder feignClientBuilder;

    public DynamicFeignClientFactory(ApplicationContext appContext) {
        this.feignClientBuilder = new FeignClientBuilder(appContext);
    }

    public T getFeignClient(final Class<T> type, String serviceId,String path) {
        return this.feignClientBuilder.forType(type, serviceId).path(path).build();
    }

}
  • 第二 、编写动态client类 SpringCloudDynamicApi 和 DynamicClient

public interface SpringCloudDynamicApi {
    @PostMapping(value = "", produces = "application/json")
    JSONObject postExecute(@RequestBody Map<String,String> params);

    @GetMapping(value = "", produces = "application/json")
    JSONObject getExecute(@RequestBody  Map<String,String> params);
}
@FeignClient(name = "dynamicClient",contextId = "dynamicClient")
public interface DynamicClient extends SpringCloudDynamicApi {

}
  • 第三 、编写动态的监听 TaskBusinessCallListener

@Component(value = "taskBusinessCallListener")
public class TaskBusinessCallListener implements TaskListener {
    /**
     * nacos的服务id
     */
    private FixedValue serviceId;
    /**
     * 访问的url路径
     */
    private FixedValue url;
    /**
     * 参数 多个的话用分号隔开 实例 userCode:00004737;status:1
     */
    private FixedValue params;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private DynamicFeignClientFactory<DynamicClient> dynamicFeignClientFactory;

    @Override
    public void notify(DelegateTask delegateTask) {
        String processInstanceId = delegateTask.getProcessInstanceId();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        String businessKey = processInstance.getBusinessKey();
        String serviceIdStr = null, urlStr = null, paramsStr = null;
        if (serviceId != null) {
            serviceIdStr = serviceId.getExpressionText();
        }
        if (url != null) {
            urlStr = url.getExpressionText();
        }
        if (params != null) {
            paramsStr = params.getExpressionText();
        }
        DynamicClient dynamicClient = dynamicFeignClientFactory.getFeignClient(DynamicClient.class, serviceIdStr, urlStr);
        Map<String, String> params = new HashMap<>();
        params.put("processInstanceId", processInstanceId);
        params.put("businessKey", businessKey);
        params.put("params", paramsStr);
        dynamicClient.postExecute(params);
        }
  • 第四、测试


@SpringBootTest(classes = HopeFlowServerApplication.class)
public class TestClient {

    @Autowired
    private DynamicFeignClientFactory<DynamicClient> feignClientFactory;

    @Test
    public void testDynamic() {
        DynamicClient dynamicClient = feignClientFactory.getFeignClient(DynamicClient.class, "flow-center", "/api/flow/base/getApps");
        Map<String,String> params = new HashMap<>();
        JSONObject object = dynamicClient.postExecute(params);
        System.out.println(object);
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小学生05101

flowable

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值