Search code examples
scalablockingakka

Suspend Akka Actors


I am trying to use Akka to implement the following (I think I'm trying to use Akka the proper way):

I have a system where I have n resource listeners. Essentially a resource listener is an entity that will listen on an input resource and publish what it sees (i.e. polling a database, tailing a log file, etc.).

So I want to use Akka actors to do these little bits of work units (listening on a resource). I've noticed that the Akka gives me a thread pool of t threads which may be less than the number of listeners. Unfortunately for me, getting a message from these resource listeners might be blocking, so it could take seconds, minutes, before the next message pops up.

Is there any way to suspend a resource listener so it leaves the thread to another actor and we'll come back to it a little later in time?


Solution

  • In general, you should try to avoid blocking operations in actors. For file IO, there are asynchronous libraries and for some databases, too. If that is not an option for you, you can set change the default dispatcher so that the underlying thread pool expands as needed.