mardi 21 avril 2015

Akka:Error In merging two list of Futures

I have 2 list of futures; "List<Future<Object1>>"and "List<Future<Object2>>". I want to merge them to create a new "List<Future<Object1>>", so that modified object1 will have a object2 within it.

Please see the stripped down version of my code

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

However when I am returning the final response, intermittently Object1 is not getting updated with Object2. All the futures are populated by making Rest calls to different services. On analysis, I can see in those scenarios , its not enetering the second flatmap itself. I think the problem is first future flatmap is executed before second list of futurepopulation. Please help

Aucun commentaire:

Enregistrer un commentaire