feedapi的java时间优化(在不同api调用的列表中)

wgeznvg7  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(259)

Jmeter 板提要页有一个restapi。包含分页的不同活动。不同的api从不同的数据库集合以及一些http第三方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;
    }

我尝试了不同的方法,使用可满足的未来,但没有多大的效果!

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

//主模型类

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
}

但时间并没有减少多少。我需要一个解决方案来执行此操作速度快,响应时间少。请帮帮我

oalqel3c

oalqel3c1#

如果您想做多线程处理,那么只需将其转换为 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);
    }
}

相关问题