跨系统调用Controller

本文涉及的产品
云原生网关 MSE Higress,422元/月
注册配置 MSE Nacos/ZooKeeper,118元/月
Serverless 应用引擎免费试用套餐包,4320000 CU,有效期3个月
简介: 跨系统调用Controller

 请求端方法:

Controller:

@Override
    public ResultData saveData(long userId, TemplateManage templateManage) {
        //改变状态入湖中
        cloudFlowUtil.updateLakeStatusIng(templateManage.getFormId());
        ltCloudUtil.updateLakeStatusIng(templateManage.getDataSourceId());
        TemplateManage templateManageNew = new TemplateManage();
        templateManageNew.setId(SnowflakeIdGenerator.getId());
        templateManageNew.setName(templateManage.getName());
        templateManageNew.setDesign(templateManage.getDesign());
        templateManageNew.setParentId(templateManage.getParentId());
        templateManageNew.setParentCode(templateManage.getParentCode());
        templateManageNew.setGroupId(templateManage.getGroupId());
        templateManageNew.setDataSourceId(templateManage.getDataSourceId());
        templateManageNew.setDataSourceType(templateManage.getDataSourceType());
        templateManageNew.setCreateUserName(templateManage.getCreateUserName());
        templateManageNew.setFormId(templateManage.getFormId());
        templateManageNew.setIsDeleted("0");
        templateManageNew.setSpStatus("1");
        templateManageNew.setLakeType(templateManage.getLakeType());
        templateManageNew.setCreateTime(LocalDateTime.now());
        templateManageNew.setCreateUserId(userId);
        List<DfsFormAppro> dfsFormApproList = dfsFormApproMapper.selectList(null);
        Boolean flag = false;
        //1.找到DfsFormAppro里有没有表单id templateManage.getFormId()
        DfsTaskAppro listByFormId = dfsTaskApproService.getListByFormId(templateManage.getFormId());
        //2.如果有就再次提交
        if (listByFormId != null){
            cloudFlowUtil.reExecute(listByFormId.getTaskCloudFlowOrderId());
        }else {
            //3.如果没有就初次提交
            DfsFormAppro dfsFormAppro = new DfsFormAppro();
            // 首次提交
            dfsFormAppro.setId(SnowflakeIdGenerator.getId());
            dfsFormAppro.setTaskFormId(templateManage.getFormId());
            String orderId = "";
            JSONObject appro = new JSONObject();
            if (templateManage.getLakeType().equalsIgnoreCase("1")) {
                // 直报提交到审批流
                appro = cloudFlowUtil.startAndExecute("35aef0a551384210a7b2be4df7008269");
            } else if (templateManage.getLakeType().equalsIgnoreCase("2")) {
                // 爬虫提交到审批流
                appro = cloudFlowUtil.startAndExecute("39fe7a406bff4f1a8afb94a68754172d");
            } else if (templateManage.getLakeType().equalsIgnoreCase("3")) {
                // 同步提交到审批流
                appro = cloudFlowUtil.startAndExecute("ca1f66846f9b4697992418cd2072f102");
            } else if (templateManage.getLakeType().equalsIgnoreCase("4")) {
                // 集成提交到审批流
                appro = cloudFlowUtil.startAndExecute("91a789b3f6864c09a9d4350e50ee25a8");
            } else if (templateManage.getLakeType().equalsIgnoreCase("5")) {
                // 提交到审批流
                appro = cloudFlowUtil.startAndExecute("4083c7761cf24a60a2199657950d3576");
            }
            if (appro.getInteger("code") == 20000) {
                JSONObject taskOrder = JSONObject.parseObject(appro.getString("data"));
                orderId = taskOrder.getString("id");
                dfsFormAppro.setTaskCloudFlowOrderId(orderId);
            }
            dfsFormAppro.setTemplateManageId(templateManageNew.getId());
            dfsFormAppro.setCreateTime(LocalDateTime.now());
            dfsFormAppro.setCreateUser(userId);
            // 向数据库中保存数据
            formApproService.save(dfsFormAppro);
        }
        templateManageMapper.insert(templateManageNew);
        return ResultData.success("ok");
    }

image.gif

调用的CloudFlowUtil:

package com.todod.utils;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.todod.dto.FormFillFieldReq;
import com.todod.entity.QueryEntry;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class CloudFlowUtil {
    @Value("${remote.cloud.flow.url}")
    private String BASE_CLOW_FLOW_URL;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${remote.fill.in.url}")
    private String BASE_FILL_IN_URL;
    
    /**
     * @Title: getUserList
     * @Description: 获取流程列表,用来针对不同的云平台的用户显示
     * @author: wgb
     * @date 2023-07-26
     * @return JSONObject
     **/
    public JSONObject getFlowList(){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(StpUtil.getTokenName(), StpUtil.getTokenValue());
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(BASE_CLOW_FLOW_URL + "flow/getProcessList", headers, String.class);
        JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
        return reData;
    }
    
    /**
     * @Title: startAndExecute
     * @Description: 执行"执行任务接口"(传入审核流id)
     * @param: processId 任务绑定审核流id
     * @author: wgb
     * @date 2023-07-26
     * @return JSONObject
     **/
    public JSONObject startAndExecute(String processId){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(StpUtil.getTokenName(), StpUtil.getTokenValue());
        
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("processId", processId);
        map.put("processName", null);
        map.put("args", null);
        
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(BASE_CLOW_FLOW_URL + "/flow/startAndExecute", new HttpEntity<>(map, headers), String.class);
        JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
        return reData;
    }
    /**
     * @Title: reExecute
     * @Description: 驳回或撤回后再次提交
     * @param: orderId 审核流实例id
     * @author: wgb
     * @date 2023-07-26
     * @return JSONObject
     **/
    public JSONObject reExecute(String orderId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(StpUtil.getTokenName(), StpUtil.getTokenValue());
        Map<String, Object> map = new HashMap<String, Object>();
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(BASE_CLOW_FLOW_URL + "/flow/task/reExecute?orderId="+orderId,
                new HttpEntity<>(map, headers), String.class);
        JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
        return reData;
    }
    /**
     * @Title: getTodoList
     * @Description: 获取待审批列表
     * @param query
     * @return
     */
  public JSONObject getTodoList(QueryEntry query) {
    HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(StpUtil.getTokenName(), StpUtil.getTokenValue());
        
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(BASE_CLOW_FLOW_URL + "/flow/task/todoList", new HttpEntity<>(query, headers), String.class);
        JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
        return reData;
  }
  /**
   * @Title: getDoneList
     * @Description: 获取已完成列表
   * @param query
   * @return
   */
  public JSONObject getDoneList(QueryEntry query) {
    HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(StpUtil.getTokenName(), StpUtil.getTokenValue());
        
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(BASE_CLOW_FLOW_URL + "/flow/task/doneList", new HttpEntity<>(query, headers), String.class);
        JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
        return reData;
  }
  
  /**
     * @Title: getUserList
     * @Description: 获取获取审批完成后列表
     * @param: orderIds 审批实例id列表字符串
     * @author: wgb
     * @date 2023-07-26
     * @return JSONObject
     **/
    public JSONObject getOrdercompletionList(String orderIds){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
//        headers.add(StpUtil.getTokenName(), StpUtil.getTokenValue());
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(BASE_CLOW_FLOW_URL + "/ordercompletion/getOrderByIds?orderIds=" + orderIds, headers, String.class);
        JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
        return reData;
    }
    public JSONObject updateLakeStatus(Long formId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(StpUtil.getTokenName(), StpUtil.getTokenValue());
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(
                BASE_FILL_IN_URL + "ltcloud/form/updateLakeStatus?id=" + formId, headers, String.class);
        JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
        return reData;
    }
    //入湖驳回
    public JSONObject updateLakeStatusBack(Long formId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(StpUtil.getTokenName(), StpUtil.getTokenValue());
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(
                BASE_FILL_IN_URL + "ltcloud/form/updateLakeStatusBack?id=" + formId, headers, String.class);
        JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
        return reData;
    }
    //入湖中
    public JSONObject updateLakeStatusIng(Long formId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(StpUtil.getTokenName(), StpUtil.getTokenValue());
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(
                BASE_FILL_IN_URL + "ltcloud/form/updateLakeStatusIng?id=" + formId, headers, String.class);
        JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
        return reData;
    }
}

image.gif

在yaml文件中配置好对应的路径即可:remote.cloud.flow.url = http://localhost:8083/cloud_flow/

被调用方Controller:

/**
   * @title startAndExecute
     * @description 启动并执行第一个任务节点
     * @param param
     * @param userId 当前用户id
     * @return
     */
  public Order startAndExecute(WfOrderParam param, Long userId) {
//    Process process = this.getProcessByProcessId(param.getProcessId());
    
    param.setArgs(param.getArgs()!=null?param.getArgs():new HashMap<String,Object>());
    param.getArgs().put("reason", "提交");
    
//        param.getArgs().put(TododConstants.INSTANCE_URL, process.getInstanceUrl());
//        // 设置业务ID
//        param.getArgs().put(SnakerEngine.ID, UUID.randomUUID().toString().replaceAll("-",""));
//        // 创建流程实例用户名
//        param.getArgs().put(TododConstants.ORDER_USER_NAME_KEY, "");
//        // 创建流程实例姓名
//        param.getArgs().put(TododConstants.ORDER_USER_REAL_NAME_KEY, "");
        Order order = snakerEngineFacets.startInstanceById(param.getProcessId(), userId.toString(), param.getArgs());
        
        // 获取流程实例的所有任务
        List<Task> tasks = snakerEngineFacets.getEngine().query().getActiveTasks(new QueryFilter().setOrderId(order.getId()));
        List<Task> newTasks = new ArrayList<Task>();
        if (tasks != null && tasks.size() > 0) {
            Task task = tasks.get(0);
            
         // 保存审批记录
          ApproRecords records = new ApproRecords();
          records.setId(task.getId());
          records.setOrderId(order.getId());
          records.setPerformType("0");
          records.setOperator(userId.toString());
          records.setRemark("提交");
          records.setFinishTime(LocalDateTime.now());
          recordsService.save(records);
          
            newTasks.addAll(snakerEngineFacets.getEngine().executeTask(task.getId(), userId.toString(), param.getArgs()));
        }
        return order;
  }

image.gif


目录
相关文章
|
9月前
|
设计模式 算法 测试技术
C++ 创建兼容多个IPC机制的上层接口
C++ 创建兼容多个IPC机制的上层接口
147 1
|
6月前
|
安全 Android开发 开发者
Service与Activity如何实现通信
Android为Service与Activity之间的通信提供了多种灵活的方式,开发者可以根据应用程序的需求选择合适的通信机制。对于多数简单通信需求,Intent和Binder通常就足够使用。另外,要注意线程安全和数据同步的问题,尤其是在多线程环境下操作UI或Service中的共享资源时。
211 0
|
8月前
|
编译器 测试技术 API
浅谈RPC调用远程资源
【6月更文挑战第1天】gRPC是Google开源的微服务通信框架,基于RPC,允许客户端像调用本地对象一样调用远程服务器方法。它使用Protocol Buffers编译器插件生成客户端和服务器代码,并在多个平台上提供高性能。要安装gRPC Go依赖,需执行相应go install命令。
113 7
浅谈RPC调用远程资源
|
9月前
|
存储 监控 安全
如何确保Controller的并发安全?
【2月更文挑战第6天】
170 1
|
9月前
|
传感器 安全
3500/42 GE / 本特利内华达 带内部屏障和内部终端的I/O模块
3500/42 GE / 本特利内华达 带内部屏障和内部终端的I/O模块
|
前端开发 Java 微服务
微服务之间调用的异常应该如何处理
在分布式服务的场景下,业务服务都将进行拆分,不同服务之间都会相互调用,如何做好异常处理是比较关键的,可以让业务人员在页面使用系统报错后,很清楚的看到服务报错的原因,而不是返回代码级别的异常报错,比如NullException、IllegalArgumentException、FeignExecption等异常报错,这样就会让非技术人员看到了一头雾水,从而很降低用户的体验感。
|
Dubbo 网络协议 Java
网关调用其他项目的方法(RPC实现)
网关调用其他项目的方法(RPC实现)
151 0
|
Kubernetes 负载均衡 网络协议
K8S Service底层策略初探和分析
K8S Service底层策略初探和分析
126 0
|
Kubernetes API 容器
如何从k8s集群外的机器上调用k8s的API(可调用多个k8s的apiserver)
从k8s集群外的机器上调用k8s主节点的apiserver提供的API
4364 1
|
数据库
操作系统第五章_01 IO设备的基本概念和分类 IO控制器 IO控制方式
操作系统第五章_01 IO设备的基本概念和分类 IO控制器 IO控制方式
484 0
操作系统第五章_01 IO设备的基本概念和分类 IO控制器 IO控制方式