Best practice when calling web service from an actor (Play2)

904 views
Skip to first unread message

Leon Radley

unread,
May 6, 2012, 4:02:24 AM5/6/12
to akka...@googlegroups.com
I'm implementing a system for syncing users from a web service that returns json.

The first part with using the scheduler wasn't hard.
so the actor runs every five minutes.

and using plays WS library it wasn't hard to get the json.

But I have some questions about best practices.

Layout
I've now got everything in one actor, it calls a webservice, parses the json response into user objects, and sync these users with mongodb.
Should I separate it into one supervisor actor that spawns a web service actor that when it returns spawns a user parser actor that when it returns spawns a db sync actor?

How would something like this look, and what happens if the web service actor fails, or the user parser fails?

The web service
Should I be using plays built in WS library which is built on top of https://github.com/sonatype/async-http-client
Since this library itself is built on top of Futures...
What are you guys using to call web services?

I know it's a lot of questions, I hope someone has time to answer them.

Ps.
I am truly impressed the typesafe stack and the documentation for akka is extremely detailed which has helped alot :)

Viktor Klang

unread,
May 6, 2012, 8:11:16 AM5/6/12
to akka...@googlegroups.com
On Sun, May 6, 2012 at 10:02 AM, Leon Radley <le...@radley.se> wrote:
I'm implementing a system for syncing users from a web service that returns json.

The first part with using the scheduler wasn't hard.
so the actor runs every five minutes.

and using plays WS library it wasn't hard to get the json.

But I have some questions about best practices.

Layout
I've now got everything in one actor, it calls a webservice, parses the json response into user objects, and sync these users with mongodb.
Should I separate it into one supervisor actor that spawns a web service actor that when it returns spawns a user parser actor that when it returns spawns a db sync actor?

Sunds like a plan. I'd say Actors is Failure Driven development
 

How would something like this look, and what happens if the web service actor fails, or the user parser fails?

Read up on the supervision semantics. You'll need to think about which failures are recoverable and which ones are not.
 

The web service
Should I be using plays built in WS library which is built on top of https://github.com/sonatype/async-http-client
Since this library itself is built on top of Futures...

I'd recommend you to build a bridge between async-http-client and Akka Futures,
here's an example I did for bridging to Netty:

object NettyFutureBridge {

  import akka.dispatch.{ Promise, Future, ExecutionContext }

  import java.util.concurrent.CancellationException

  def apply(createFuture: ⇒ ChannelFuture)(implicit ec: ExecutionContext): Future[Channel] = {

    val p = Promise[Channel]()

    createFuture.addListener(new ChannelFutureListener {

      def operationComplete(future: ChannelFuture): Unit = try {

        if (future.isSuccess) p.success(future.getChannel)

        else if (future.isCancelled) throw new CancellationException

        else throw future.getCause

      } catch {

        case t ⇒ p failure t

      }

    })

    p.future

  }

}

 
What are you guys using to call web services?

I fortunately don't call webservices anymore. I used to use Axis, but SOAP is just junk IMHO.
 

I know it's a lot of questions, I hope someone has time to answer them.

Ps.
I am truly impressed the typesafe stack and the documentation for akka is extremely detailed which has helped alot :)

You're most welcome!

Cheers,
 

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/ErdzTMJro_4J.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

Leon Radley

unread,
May 6, 2012, 10:57:37 AM5/6/12
to akka...@googlegroups.com
I'll see if I can get a blog post together with the solution.

I found this repo on github which seems to be doing just that. https://github.com/HendraWijaya/syndor

Thanks again!

Jason Mason

unread,
May 6, 2012, 10:32:53 PM5/6/12
to akka...@googlegroups.com


On Sunday, May 6, 2012 5:11:16 AM UTC-7, √ wrote:


What are you guys using to call web services?

I fortunately don't call webservices anymore. I used to use Axis, but SOAP is just junk IMHO.
 
amen brother. it took us (software industry) years to get over that shit. a good example of how a stampede to the flavour of the month can leave the earth toxic for years... 
Reply all
Reply to author
Forward
0 new messages