Monday 15 August 2011

scala - How to implement simple protocol using Actors and Akka? -



scala - How to implement simple protocol using Actors and Akka? -

what right approach implement next using akka , actors?

i need phone call remote rest service , pass set of parameters , specific date. app getting set of input parameters , date range. if date range 365 days, app needs create 365 calls remote service, process output info (for days in range) , save results of processing in database.

the initial phone call rest service not homecoming results immediately, rather homecoming "request id". app need phone call rest api check request status until "completed". needs results calling yet rest api.

the initial phone call or subsequent may fail error, should retried after specific delay.

there improve solution within akka actors directly. type of problem can solved akka-http library uses streams (that utilize actors "underneath hood").

you first need actorsystem , actormaterializer:

import akka.actor.actorsystem import akka.stream.actormaterializer implicit val actorsystem = actorsystem() implicit val actormaterializer = actormaterializer() import actorsystem.dispatcher

then establish connection request-id service:

import scala.concurrent.future import akka.stream.scaladsl.{source, flow} import akka.http.scaladsl.http import akka.http.scaladsl.model.{httprequest, httpresponse} val idserviceurl = "idserviceurl.com" val requestidflow : flow[httprequest, httpresponse, future[http.outgoingconnection]] = http().outgoingconnection(idserviceurl)

the question not specific "set of parameters , specific date". not specific on how parameters , date should form httprequest:

type parameterstype = ??? type datetype = ??? def paramstoidrequest(paramaters : parameterstype, date : datetype) : httprequest = ???

this method can used query request-id:

type idtype = ??? //converts http response id service id def httpresponsetoid(httpresponse : httpresponse) : idtype = ??? val requestidfuture : future[idtype] = source.single(parameterstoidrequest(...)) .via(requestidflow) .map(httprequesttoid) .runwith(sink.head)

similar techniques can used status , results...

scala protocols akka fsm

No comments:

Post a Comment