Async call inside actor's receive method

318 views
Skip to first unread message

newuser001

unread,
Aug 3, 2012, 12:57:31 AM8/3/12
to akka...@googlegroups.com
Hi i have been trying to use the async-http-client in actor's receive method with no success, my method looks like,

class HttpActor extends Actor {

  def receive = {
    case GET => {
          asyncHttpClient.prepareGet("http://www.ning.com/").execute(new AsyncCompletionHandler[Response]{

              override def onCompleted(response:Response) = {
                  sender ! "ok"
                  response
              }
         }
  }
}
case class GET


But i never get a reply for ( httpactor ? GET ) and it always timesout? Does the methods inside actors needs to be synchronous?

Andrew Headrick

unread,
Aug 3, 2012, 1:24:01 AM8/3/12
to akka...@googlegroups.com
You need to extract out the sender to make it thread safe.

class HttpActor extends Actor {

  def receive = {
    case GET => {
          val sendTo = context.sender
          asyncHttpClient.prepareGet("http://www.ning.com/").execute(new AsyncCompletionHandler[Response]{

              override def onCompleted(response:Response) = {
                  sendTo ! "ok"
                  response

Derek Wyatt

unread,
Aug 3, 2012, 1:25:01 AM8/3/12
to akka...@googlegroups.com
There are a couple of things wrong here.

1) Obviously you need to send back the response, not "ok", but I'll assume you have been playing with it and left it in a weird state.
2) You're closing over 'sender' directly.  'sender' is a method, not a value.  Freeze the value first.

i.e. for number 2:

// freeze
val requester = sender

asyncHttpClient.prepareGet(…).execute(… {
  override def onCompleted(response: Response) = {
    // use
    requester ! response
  }
}

--
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/-/cnzHpuYNBXwJ.
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.

signature.asc

√iktor Ҡlang

unread,
Aug 3, 2012, 7:32:00 AM8/3/12
to akka...@googlegroups.com
All the replies are correct.
But I'd recommend you to instead of doing that, create a bridge between async-http listeners and Akka Futures instead, then you can use all the goodness of Akka Futures and compose freely.

sendSomeHttpRequest(...) pipeTo sender

Cheers,
--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang

Havoc Pennington

unread,
Aug 3, 2012, 10:21:19 AM8/3/12
to akka...@googlegroups.com
Agree with Viktor (having tried it both ways myself). There's an old
(akka 1.2) asynchttpclient-to-Future bridge here:
https://github.com/typesafehub/webwords/blob/heroku-devcenter/common/src/main/scala/com/typesafe/webwords/common/URLFetcher.scala#L41

This code could be improved quite a bit but maybe it's a starting point for you.

Havoc

newuser001

unread,
Aug 7, 2012, 4:12:41 PM8/7/12
to akka...@googlegroups.com
Thanks all ! "freezing" the actorref seems to work although it sounds odd to me as i dont understand how does assigning a reference outside the callback makes a difference. I have not yet tried the bridge method as it sounds lot of work :P.

Roland Kuhn

unread,
Aug 7, 2012, 4:19:30 PM8/7/12
to akka...@googlegroups.com
Scala closures are aggressively opposed to the “by value” concept: closing over “sender” means that that method will be called when the callback is run, which will be on a different thread at a much later time, when the actual value you want is long gone. The only trick you have at your disposal is to reference a val on the stack (i.e. a local name in a method), because the stack frame where that lives cannot be referenced and hence the value must be copied.

If this sounds outlandish to you, don’t worry: just make it a habit to create a “useless” copy of the sender.

Regards,

Roland

--
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/-/RgDaDu1ReF4J.

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.

Roland Kuhn
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn


Reply all
Reply to author
Forward
0 new messages