Feed API 的时间优化在不同 API 调用的列表中

问题描述

仪表板提要页面有一个 REST API。包含带有分页的不同活动。不同的 API 从不同的数据库集合以及一些 Http 3rd 方 API 中获取数据。

public List<Map<String,Object>> getData(params...) {
List<Map<String,Object>> uhfList = null;
        Map<String,Object> uhf = null;
           for (MasterModel masterModel : pageActivities) {    //Taking Time n (which I need to reduce)
                uhf = new HashMap<String,Object>();
                uhf.put("Key",getItemsByMethodName(params..));
                uhfList.add(uhf);
            }
return uhfList;
}



  private List<? extends Object> getItemsByMethodName(params...) {
        java.lang.reflect.Method method = null;
        List<? extends Object> data = null;
        try {
            method = uhfRelativeService.getClass().getMethod(params...);
            data = (List<? extends Object>) method.invoke(params...);
        } catch (Exception e) {
            LOG.error("Error Occure in get Items By Method Name :: ",e.getMessage());
        }
        return data;
    }

我使用 Complatable Future 尝试了不同的方法,但效果不佳!

private CompletableFuture<List<? extends Object>> getItemsByMethodName(UserDetail userIdentity,UHFMaster uhfMaster) {
    java.lang.reflect.Method method = null;
    CompletableFuture<List<? extends Object>> data = null;
    try {
        method = uhfRelativeService.getClass().getMethod(uhfMaster.getMethodName().trim(),params...);
        data = (CompletableFuture<List<? extends Object>>) method.invoke(uhfRelativeService,userIdentity);
    } catch (Exception e) {
        LOG.error("Error  :: ",e.getMessage());
    }
    return data;
}

//MasterModel 类

public class MasterModel {
    @Id
    private ObjectId id;
    private String param;
    private String param1;
    private String param2;
    private Integer param3;
    private Integer param4;
    private Integer param5;
    private Integer param6;
    private Integer param7;
    private Integer param8;
    private Integer param9;
    private String param10;
    private String param11;

//getter & setter
}

但是时间并没有减少多少。我需要一个解决方案来以更少的响应时间快速执行此操作。请帮我解决这个问题

解决方法

如果您想进行多线程处理,那么仅强制转换为 CompletetableFuture 将无济于事。要在单独的线程中实际异步运行进程,您可以执行以下操作:

public List<Map<String,Object>> getData(params...) {    
    UHFMaster master = null;             // assume present
    List<UserDetail> userDetails = null; // assume list present

    // just an example
    // asynchronous threads
    List<CompletableFuture<List<? extends Object>>> futures = 
        userDetails.stream().map(u -> getItemsByMethodName(u,master));
    
    // single future which completes when all the threads/futures in list complete
    CompletableFuture<List<? extends Object>> singleFuture = 
        CompletableFuture.allOf(futures);

    // .get() on future will block - meaning waiting for the completion
    List<Map<String,Object>> listToReturn = 
        singleFuture.get().map(i -> new HashMap() {{ put("Key",i); }});

    return listToReturn;
}

private CompletableFuture<List<? extends Object>> getItemsByMethodName(UserDetail userIdentity,UHFMaster uhfMaster) {
    try {
        java.lang.reflect.Method method = uhfRelativeService.getClass().getMethod(uhfMaster.getMethodName().trim(),params...);

        return CompletableFuture
            .suppyAsync(() -> method.invoke(uhfRelativeService,userIdentity));
    } catch (Exception e) {
        LOG.error("Error  :: ",e.getMessage());
        return CompletableFuture.completedFuture(null);
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...