I am looking at how I can hook up an actor system with synchronous
controller code. Essentially there are REST endpoints and other methods
that have to make certain calls and then wait for the response for going
on. Essentially bridging the actor system to a non-actor system. I know I
can use the Ask pattern to create Akka futures but to be honest the pattern
is very complex to use and might be beyond the skills of some of the Akka
newbies. One other problem with the Ask pattern is that Akka futures are
cumbersome to use in Java code and we have a large mix of java and Scala
code. What I was wondering is if anyone had tried to use micro actors to
implement synchronous callbacks. Erlang has the capability to create
thousands of actors and dispose of them quickly which is very useful for
things like micro actors that do only one specific task. A first pass at
how it might look, integrating with a Java Completable future is below.
The questions I want to pose are basically:
1) Has anyone tried to use MicroActors and if so, are there any code
samples or experiences you can relate to make the process easier.
2) Is Akka going to be performant creating actors that exist merely for the
span of a specific method call or will the overhead be too high?
3) Is my implementation on the right track or is there an easier or simpler
way to do this to make it easy. Given that the goal I have is user calls
send and waits for response but its done with either completable futures or
some other mechanism that blocks for response.
Thanks in advance. Draft code below:
import java.util.concurrent.CompletableFuture
import akka.actor.{Props, ActorRef, UntypedActor}
import play.libs.Akka
class CompletableFutureMicroActor[T](val responseMessageType: Class[T], val
future: CompletableFuture[T])
extends UntypedActor{
override def onReceive(message: Any): Unit = message match {
case response: T =>
future.complete(response)
context.stop(context.self)
}
}
object CompletableFutureMicroActor {
def send[T](responseMessageType: Class[T], target: ActorRef, message: Any) :
CompletableFuture[T] = {
val future = new CompletableFuture[T]()
val props = Props(classOf[CompletableFutureMicroActor], responseMessageType,
future)
val receiver = Akka.system().actorOf(props)
target.tell(message, receiver)
future
}
}
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> 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/d/optout.