Ivan Yurchenko
Ivan Yurchenko

Reputation: 566

Long-running blocking task with Akka's actor

I want one of my Akka actors to perform long-running blocking task (finding a substring in a very long string, as an example). How should I do this following actors' phylosophy (not blocking other actors etc.)?

I'm thinking about running this actor in a separated thread with PinnedDispatcher, but I'm not sure.

Upvotes: 1

Views: 1546

Answers (2)

ktonga
ktonga

Reputation: 190

You can wrap the task in a future and send the result to the other Actor using the pipe pattern. The other actor will receive either the resulting value or an akka.actor.Status.Failure holding the cause.
If you want to respond to the sender it would be:

import akka.pattern.pipe

def receive: Receive = {
  case msg =>
    val future: Future[String] = Future(longRunningBlockingTask(msg))
    future pipeTo sender
}

Upvotes: 5

bobbyr
bobbyr

Reputation: 326

You could wrap the call in a Future

Upvotes: 2

Related Questions