• 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();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 第二 、编写动态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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @FeignClient(name = "dynamicClient",contextId = "dynamicClient")
    public interface DynamicClient extends SpringCloudDynamicApi {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 第三 、编写动态的监听 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);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 第四、测试

    
    @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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    OFD转PDF ~java实现
    windows terminal鼠标右键打开
    C#使用TcpListener进行聊天通信
    TCP Illinois 与 TCP Highspeed
    时间本地化开发规范
    vue.runtime.esm.js?c320:4625 [Vue warn]: Failed to resolve directive: lazy
    上机实验四 图的最小生成树算法设计 西安石油大学数据结构
    一个 15 年 SAP ABAP 开发人员分享的 SAPGUI 一些个性化设置和实用小技巧
    分闸合闸电源监视继电器JZZS-1120/DC220V
    大众点评字体反爬解析
  • 原文地址:https://blog.csdn.net/liuwenjun05101/article/details/127445589