akka - How to return actor received message to java? -
i new akka , using akka rpc service . know akka more , started.
there userserviceactor
reports how many users in service :
@inject private userservice userservice; @override public void onreceive(object message) throws exception { if (message instanceof countreq) { long count = userservice.getusercount(); getsender().tell(new countres(count) , getself()); } else { unhandled(message); } }
this userserviceactor
runs on remote machine. , in local machine , there localactor
negotiates remote actor.
if (message instanceof countreq) { remote.tell(message, getself()); } if (message instanceof countres) { getsender().tell(message, getself()); } else { unhandled(message); }
follow example : an akka actors 'ask' example
i write client :
@inject private actorref localactor; public long getusercount() { timeout timeout = new timeout(duration.create(10, timeunit.seconds)); future<object> future = patterns.ask(localactor, new countreq(), 10000); try { object res = await.result(future, timeout.duration()); logger.info("res = {}" , res); return (long) res; } catch (exception e) { throw new runtimeexception(e); } }
the code example in 1 returns string sender , non-actor world can result.
but in situation , client sends countreq
localactor , , actor send countreq
remote server . , remote server returns countres
. @ time , sender becomes remote userserviceactor
, not non-actor client .
some page suggests reading futures , still cannot find how accomplish ?
is there java code example or sample project such rpc-style actor , , pass result non-actor world ?
environment : 2 springboot apps , akka-actor_2.11 version 2.3.11
thanks.
i solve way :
private actorref caller = null; inside onreceive() : if (message instanceof countreq) { this.caller = getsender(); remote.tell(message, getself()); } if (message instanceof countres) { caller.tell(message , getself()); }
i don't know if 'standard' way , @ least works.
any better solutions welcome !
Comments
Post a Comment