7

I have a controller action where I need to call 3rd party web services.

My problem is that I am not calling one web service. I need to chain between 4 to 5 web services. Each web service I call returns a JSON object which I need to process and based on some logic I decide to either call another web service (from the 4 web services) or return a response to the caller. Here's what I am trying to do:

 public static Promise<Result> accounts(){
    return WS.url("url1").get().map(response1 -> {
        JsonNode mynode = response1.asJson();

        if (mynode.get("status").asInt()==200){
            Promise<JsonNode> jsonPromise = WS.url("url2").get().map(response2->{
                    return response2.asJson();
            });
        }

        return ok(mynode);
        }); 
}

Now, from the documentation, I think that what I need is to chain promises where each web service call is a promise. But I am not sure how to do that?

Thanks

2 Answers 2

7

They're calling it Reactive Composition and it's done like this ->

public static Promise<Result> reactiveCombo() {
    Promise<WSResponse> promise1 = WS.url("url1").get();
    Promise<WSResponse> promise2 = WS.url("url2").get();
    Promise<WSResponse> promise3 = WS.url("url3").get();

    return promise1.flatMap(response1 -> {
        final JsonNode json1 = response1.asJson();
        if (!json1.has("someField")) {
            return Promise.promise(() -> badRequest());
        }
        return promise2.flatMap(response2 -> {
            final JsonNode json2 = response2.asJson();
            if (json1.get("someField").asText().equals(json2.get("someField").asText())) {
                return Promise.promise(() -> badRequest());
            }
            return promise3.map(response3 -> {
                final JsonNode json3 = response3.asJson();
                if (json3.get("boolField").asBoolean()) {
                    return badRequest();
                }
                return ok();
            });
        });
    });
}

For large # of calls you can use Promise.sequence() and get 'creative':

private static Promise<JsonNode> getPromise(String url, Predicate<JsonNode> predicate) {
    return WS.url(url).get().map(response -> {
        JsonNode json = response.asJson();           
        if (predicate.negate().test(json)) {
            throw new Exception("BUMMER!");
        }
        return json;
    });
}

public static Promise<Result> reactiveCombo(List<String> urls) {
    List<Promise<JsonNode>> promises = new ArrayList<Promise<JsonNode>>(urls.size());
    Predicate<String> predURL = p -> p.contains("goodApi");
    Predicate<JsonNode> pred1 = p -> p.has("boolField") && p.get("boolField").asBoolean();
    Predicate<JsonNode> pred2 = p -> p.has("someField");

    urls.forEach(url -> {
        Promise<JsonNode> promise = predURL.test(url) ? getPromise(url, pred1) : getPromise(url, pred2);           
        promises.add(promise);
    });

    return Promise.sequence(promises).map(results -> ok()).recover(t -> badRequest());
}

Additional details + docudrama:

Go Reactive with Java 8 & Play Framework (old but still educative)

JavaWS

4
  • OK this works like a charm. However, the code looks ugly. I am talking to a DB server to perform some CRUD operations. It is clear to me that the DB REST APIs could be improved a lot to reduce the number of calls and also to provide safe transaction operations. But it is not something I can do. For example in one scenario I end up calling 10 REST APIs. SO any suggestions how I can improve the code
    – Ubaidah
    Commented Apr 29, 2015 at 21:07
  • 1
    Well, you could, for example put your URLs in an array, iterate over it and for each URL you create a Promise<T> that you add to a List<Promise<T>> promisesList. Then you can use Promise.sequence(primisesList).map((List<T> resultList) -> {...});. That would reduce the nesting quite a bit.
    – sebster
    Commented Apr 30, 2015 at 7:37
  • 1
    This might help. I hope whoever ends up maintaining this code in the future does not know where I live :)
    – Ubaidah
    Commented Apr 30, 2015 at 16:35
  • 1
    Heh, come on... It's not THAT bad.
    – sebster
    Commented Apr 30, 2015 at 17:40
4

You can use recursion to flatten this structure for any number of Promises. First create a class:

public static class ChainedWebService {
    public final Optional<ChainedWebService> next;

    public final WSRequestHolder wsResponsePromise;
    private final F.Predicate<JsonNode> predicate;

    public ChainedWebService(String url, Optional<ChainedWebService> next, F.Predicate<JsonNode> predicate) {
        this.next = next;
        this.wsResponsePromise = WS.url(url);
        this.predicate = predicate;
    }

    public F.Promise<Result> processChain() {
        return wsResponsePromise.get().flatMap(new F.Function<WSResponse, F.Promise<Result>>() {
            @Override
            public F.Promise<Result> apply(WSResponse wsResponse) throws Throwable {
                if (!predicate.test(wsResponse.asJson())) {
                    return F.Promise.pure(badRequest());
                }

                if (!next.isPresent()) {
                    return F.Promise.pure(ok());
                }

                return next.get().processChain();
            }
        });
    }
}

And then use it:

public static F.Promise<Result> reactiveCombo() {
    ChainedWebService chainedWebService3 = new ChainedWebService(
            "url3",
            Optional.<ChainedWebService>empty(),
            jsonNode -> jsonNode.get("boolField").asBoolean()
    );

    ChainedWebService chainedWebService2 = new ChainedWebService(
            "url2",
            Optional.of(chainedWebService3),
            jsonNode -> jsonNode.get("someField").asText().equals(jsonNode.get("someField").asText()));

    ChainedWebService chainedWebService1 = new ChainedWebService(
            "url1",
            Optional.of(chainedWebService2),
            jsonNode -> jsonNode.has("someField")
    );

    return chainedWebService1.processChain();
}

You can implement some basic builder to make the building process more reasonable.

Hope thats help!

2
  • Interesting approach, but you have Promise.sequence() exactly for this. Plus, you're really risking to fill-up the stack with this solution.
    – sebster
    Commented Jun 4, 2015 at 15:29
  • 1
    @sebster using Promise.sequence() will mean executing all the requests, which is not necessary in this case.
    – Idan Nahum
    Commented Oct 30, 2015 at 8:32

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.