riorio
riorio

Reputation: 6826

Is there a way to pause akka actor for X time

We have an Akka actor that is reading a big folder with many files.

It reads them for the processing of other actors.

It seems like it reads the too fast and eventually we run into OutOfMemoryException .

We like to know if we can somehow pause/sleep it for X time?

Upvotes: 0

Views: 683

Answers (2)

Frederic A.
Frederic A.

Reputation: 3514

You can pause it yourself, by that I mean you should have the actor stop processing files and setup a timer to send itself a resume message after X seconds/minutes.

But... you'll never find the right amount of time (X above), it'll always be too long or too short. This kind of problem is why akka-stream exists. I would recommend you to have a look at it because it is the only good solution to that kind of problems.

Upvotes: 1

Raman Mishra
Raman Mishra

Reputation: 2686

For out of memory problem you can increase the queue size of the actors mail box. And for sleeping/pausing you can use schedular to schedule the message to the actor for a specific amount of time. There's no need to explicitly cause an actor to sleep: using loop and react for each actor means that the underlying thread pool will have waiting threads whilst there are no messages for the actors to process.

In the case that you want to schedule events for your actors to process, this is pretty easy using a single-threaded scheduler from the java.util.concurrent utilities:

object Scheduler {
import java.util.concurrent.Executors
import scala.compat.Platform
import java.util.concurrent.TimeUnit
private lazy val sched =Executor.
new SingleThreadScheduledExecutor();
def schedule(f: => Unit, time: Long) {
sched.schedule(new Runnable {
def run = f
},
time , TimeUnit.MILLISECONDS)}}

Upvotes: 0

Related Questions