mongodb - Play framework 2.4 (Java) mongo async db driver 3.0 query -
hello i'm trying write async code mongodb async driver (3.0) http://mongodb.github.io/mongo-java-driver/3.0/driver-async/ play framework 2.4 (java) in controller async result https://www.playframework.com/documentation/2.3.x/javaasync , when i'm testing promise results outside async call mongodb have empty json in response, please can me ?
public f.promise<result> list() { final list<document> accounts = new arraylist<document>(); f.promise<list<document>> promiseofaccounts = f.promise.promise( new f.function0<list<document>>() { public list<document> apply() { accountrepository.getcollection().find().into(accounts, new singleresultcallback<list<document>>() { @override public void onresult(final list<document> result, final throwable t) { } }); return accounts; } } ); return promiseofaccounts.map( new f.function<list<document>, result>() { public result apply(list<document> i) { return ok(i); } } ); }
when return accounts
, sigleresultcallback closure hasn't been executed. results in list being empty when it's serialized in ok(i)
expression. make work, have resolve promise inside singleresultcallback
. remember play promises
sit on scala future
s , scala promises (which different play f.promise
s). how you'd it:
promise<list<document>> accountspromise = promise$.module$.apply(); arraylist<document> accounts = new arraylist<document>(); accountrepository.getcollection().find().into(accounts, new singleresultcallback<list<document>>() { @override public void onresult(final list<document> result, final throwable t) { accountspromise.success(result); } }); promiseofaccounts=f.promise.wrap(accountspromise.future()); return promiseofaccounts.map( new f.function<list<document>, result>() { public result apply(list<document> i) { return ok(i); } } );
the moment call success
method of scala promise
resolves, , value of future becomes available, return play f.promise
before happens, awesomeness of reactive programming.
Comments
Post a Comment