Akka:错误合并两个期货清单

时间:2015-04-21 19:29:48

标签: java akka future

我有2份期货清单; "List<Future<Object1>>""List<Future<Object2>>"。我想将它们合并以创建新的"List<Future<Object1>>",以便修改后的object1中包含object2

请参阅我的代码的精简版

 public Future<Object> mergeResponse(List<Future<Object1>> listOfObject1Future, final List<Future<Object2>> listOfObject1Future2) {
    Iterable<Future<Object>> listOfObject1 = listOfObject1Future; //first list of future
    Future<Iterable<Object>> listOfObject1Future = Futures.sequence(listOfObject1, ec);

    Future<Object> mergedResponse = listOfObject1Future.flatMap(new  Mapper<Iterable<Object>, Future<Object>>() {
        public Future<Object> apply(final Iterable<Object> response1){
            return Futures.future(new Callable<Object>() {
                public Object call() throws Exception { 
                Iterator<Object> listObject1Iterator = response1.iterator();
                final List<Future<Object>> finalResponse = new ArrayList<>();
                while (listObject1Iterator.hasNext()) {
                    Object1 responseObject1 = listObject1Iterator.next();
                    finalResponse = merge(responseObject1,  listOfObject1Future); //passing the second list of futures

                }
                 return finalResponse;
                }
            }, ec);
        }
    }, ec);
    return mergedResponse;
 }


public Future<Object> merge(final Object1 responseObject1, List<Future<Object>> listOfObject2Future){

    Iterable<Future<Object>> listOfObject2 = listOfObject2Future;
    Future<Iterable<Object>> listOfObject2Future = Futures.sequence(listOfObject2, ec);

    Future<Object> response = listOfObject2Future.flatMap(new Mapper<Iterable<Object>, Future<Object>>() {
        public Future<Object> apply(final Iterable<Object> response2){
            return Futures.future(new Callable<Object>() {
                public Object call() throws Exception { 
                Iterator<Object> listObject2Iterator = response2.iterator();
                while (listObject2Iterator.hasNext()) {
                    Object2 responseObject2 = listObject2Iterator.next();

                if(responseObject1.getXXX().equals(responseObject2.getXXX())   //setting object2 in object1 based on some condition
                {
                   responseObject1.setObject2(responseObject2);
                }
                }
                return responseObject1;
                }
            }, ec);
        }
    }, ec);

    return response;
}

但是,当我返回最终回复时,间歇性地Object1未使用Object2进行更新。所有期货都通过将Rest调用到不同的服务来填充。在分析中,我可以看到在这些场景中,它没有影响第二个flatmap本身。我认为问题是第一个未来的flatmap是在未来人口的第二个列表之前执行的。请帮忙

0 个答案:

没有答案