0

I have a controller method which sends two web service requests at the same time, I immediately return a promise for both of them. Now what I want to do is combine the results of the two web service calls into a single result returned to the user. The code I have so far is:

 public static Promise<Result> search(String searchTerms) {
    final Promise<List<SearchResult>> result1 = webserviceOne(searchTerms);
    final Promise<List<SearchResult>> result2 = webserviceTwo(searchTerms);

    return result1.flatMap(
            new Function<Promise<List<SearchResult>>, Promise<Result>>() {
                public Promise<Result> apply(Promise<List<SearchResult>> res1) {
                    return result2.flatMap(
                        new Function<Promise<List<SearchResult>>, Result>() {
                            public Result apply(Promise<List<SearchResult>> res2) {
                                //TODO: Here I want to combine the two lists of results and return a JSON response
                            }
                        }
                    );
                }
            }
    );
}

How do I do this? I'm finding it really hard to find decent documentation for this sort of thing.

2 Answers 2

1

Something like this should do it:

 public static Promise<Result> search(String searchTerms) {
    final Promise<List<SearchResult>> result1 = webserviceOne(searchTerms);
    final Promise<List<SearchResult>> result2 = webserviceTwo(searchTerms);

    return result1.flatMap(
            new Function<Promise<List<SearchResult>>, Promise<Result>>() {
                public Promise<Result> apply(List<SearchResult> res1) {
                    return result2.flatMap(
                        new Function<Promise<List<SearchResult>>, Result>() {
                            public Result apply(List<SearchResult> res2) {
                                List<SearchResult> newList = new ArrayList<SearchResult>(res1);
                                newList.addAll(res2);
                                return ok(toJson(newList));
                            }
                        }
                    );
                }
            }
    );
}
0
@Override
public Zone buildZone(final GeoPoint center, final int distance) {
    Promise<List<Street>> streetPromise = Promise.promise(
            new Function0<List<Street>>() {
                public List<Street> apply() {
                    return streetRepository.findByLocation(center.getGeom(), distance);
                }
            }
    );

    Promise<List<Place>> placePromise = Promise.promise(
            new Function0<List<Place>>() {
                public List<Place> apply() {
                    return placeService.findByLocation(center, distance);
                }
            }
    );

    Promise<Zone> result = Promise.sequence(streetPromise, placePromise).map(
            new Function<List<List<? extends Object>>, Zone>() {
                @Override
                public Zone apply(List<List<? extends Object>> lists) throws Throwable {
                    return new Zone((List<Street>) lists.get(0), (List<Place>) lists.get(1));
                }
            }
    );

    return result.get(10, TimeUnit.SECONDS);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.