On 03/01/2014 07:31 AM, Etienne Couritas wrote:
Hello,

1) I'm starting to play with akka, and i was wondering how I could get
one result from different APIs, let me explain:

I try to get reviews on movies from different APIs because some APIs may
not know one movie, or not have any data on some TV shows...

I'd like to send a broadcast message to a pannel of actors, and for
exemple keep the first response or wait for a while and keep the
response from the most trusted API. (I could convert trust to timeout
for exemple, the more I trust an API the greater the timeout is, but
after that I need to get the response I trust the most among all responses).

I know ScatterGatherFirstCompletedRouter could make sens, but if I'm not
wrong, it can only be used with one actor.

I would take a look at Effective Akka:
http://shop.oreilly.com/product/0636920028789.do

Part of the book (at 74 pages it is a pretty short read) discusses actor patterns that are relevant to the problem you are trying to solve.

And i'd like each API actor to be able to manage alone whether it can
process the message: for instance, "I'm an movie API, I can't process TV
shows". But I don't know what to do if there is no result.

That is my first problem.

I think if you're going to use an actor system to make calls to a web service, you should take a look at Spray.io , here's what an actor would look like that makes calls to a web service api using akka and spray: https://gist.github.com/ambantis/9299720

2) Here's my other problem. I ask an actor to return a sequence of
movies and ask each movie to send a message to other actors to get
complementary data,

I would also take a look at scala async:  https://github.com/scala/async

The beauty of this project is that it allows you to write code that LOOKS procedural but ACTS very functional and asynchronously. Here's an example of a function that makes a series of web service calls to amazon until there are no more results:

https://gist.github.com/ambantis/9300653

The function is non-blocking and asynchronous, but has procedural control flow.

Another suggestion would be to using actors INSTEAD of futures & promises.

For example, the first gist I posted above is an actor that makes a web service request and the sends the response when the future is complete. There are no futures to map or promises to complete. The other actor responsible for gathering all the web requests is creating child actors that make these web service requests. When a certain amount of time has elapsed, or when all of the requests have been received, then all the responses are gathered together for the next step in the chain.

hope this helps.

alex

 case WatchEveningShows(day : Date) =>       
   val originalSender = sender
   implicit val ec: ExecutionContext = context.dispatcher
   //technique de la doc akka


                //return a list of programme
val futurJson  = for {
   programmes <- (XmlTvProviderActor.xmlTvProviderActor ? 
WatchEveningShows(day)).mapTo[Shows]

} yield programmes.programmes.map(xmlProgramme =>{ 
TvProgrammeFactory.makeTvProgrammeFromXmlProgramme(xmlProgramme)}) //transform the 
data to OO

futurJson map (programmes => {

                        //Here a ask for each programme
val futurProg = Future.sequence(
   programmes.map(
       x => (XmlTvProviderActor.xmlTvProviderActor ? GetChannelName(x.channel)).
        mapTo[ChannelName].
        map({y =>
                                                         // i'm not sure if is 
it the right way , very very not sure.
                x.channel = y.name //set the true value of each object
                x // return the programme in the seq
                })
        )
       )

val futurNote = Future.sequence(
   programmes.map(
       x => (BetaserieActor.betaserieActor ?  GetNote(x)).
        mapTo[Double].
        map({y =>
                x.rating = y //set the true value of each object
                x // return the programme in the seq
                })
        )
       )
for {
  a <- futurProg
  b <- futurNote
} yield originalSender ! JSonShows(Json.toJson(b.sortBy(x => 
(x.rating)).reverse.map(x =>Json.toJson(x))))

 })


But i'm wondering if there is not a more proper way to do this composition.

--
     Read the docs: http://akka.io/docs/
     Check the FAQ: http://akka.io/faq/
     Search the archives: https://groups.google.com/group/akka-user
--- You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to