json - Multiple WS calls in one Java Play action -
i developing play 2 (v2.2.3) java application (with java 8) task fetch info 2 web services , homecoming composed result.
from first ws phone call json array object let's job listings. every job in collection want phone call ws gives me stock cost of company offering job. after expect action homecoming new 'enhanced' json array along each job listing see stock price.
i started putting , managed list of jobs somehow part glue 2 phone call not working/being called.
public static promise<result> index() { final promise<ws.response> resultpromisejobs = ws.url("https://jobs.github.com/positions.json?description=java").get(); final promise<result> result = resultpromisejobs.flatmap(response -> { jsonnode joblistings = response.asjson(); // convert joblistings collection list<job> jobs = ... // phone call map on collection fetch stock prices each element jobs.stream().map(job -> { promise<ws.response> resultpromisestock = ws.url("secondurl?somequery=job").get()); // returns promise // let's map // ??? map , how convert 'result' resultpromisestock.map(); ... }); }); homecoming result; // right object homecoming here? } this same question scala, interested in java solution
from our chat can see want want map each ws.response jsonnode, leave list<promise<jsonnode>>. promise.sequence can used convert list<promise<jsonnode>> promise<list<jsonnode>>, can map promise<result>.
public static promise<result> index() { final promise<ws.response> resultpromisejobs = ws.url("https://jobs.github.com/positions.json?description=java").get(); final promise<result> result = resultpromisejobs.flatmap(response -> { jsonnode joblistings = response.asjson(); list<job> jobs = ... list<promise<jsonnode>> jsonresultspromise = jobs.stream().map(job -> { promise<ws.response> resultpromisestock = ws.url("secondurl?somequery=job").get()); // returns promise // map retrieved jsonnode resultpromisestock.map(...); }); // convert list of promises promise of lists promise<list<jsonnode>> jsonresults = promise.sequence(jsonresultspromise); // map promise<result> jsonresults.map(...); }); homecoming result; } the grab here though if 1 of promises fails complete, entire list fail. in many cases might want happen if need results. it's easy plenty discard failed promises list in scala using method, i'm not sure how in java without fold.
java json web-services scala playframework-2.0
No comments:
Post a Comment