expert
expert

Reputation: 30095

How do I throttle messages in Akka (2.1.2)?

Could you guys please show example of throttling messages in Akka ?

Here is my code

object Program {
  def main(args: Array[String]) {
    val system = ActorSystem()
    val actor: ActorRef = system.actorOf(Props[HelloActor].withDispatcher("akka.actor.my-thread-pool-dispatcher"))

    val zzz : Function0[Unit] = () => {
      println(System.currentTimeMillis())
      Thread.sleep(5000)
    }

    var i: Int = 0
    while (i < 100) {
      actor ! zzz
      i += 1
    }

    println("DONE")

//    system.shutdown()
  }
}

class HelloActor extends Actor {
  def receive = {
    case func : Function0[Unit] => func()
  }
}

and here is my config

akka {
  actor {
    my-thread-pool-dispatcher {
      type = Dispatcher
      executor = "thread-pool-executor"
      thread-pool-executor {
        task-queue-type = "array"
        task-queue-size = 4
      }
    }
  }
}

But when I run it it appears to be single-threaded where as I expect 4 messages to be processed at the same time.

What am I missing here ?

Upvotes: 2

Views: 1799

Answers (2)

Prakhyat
Prakhyat

Reputation: 1019

Typesafe has recently announced akka reactive streams. Throttling can be achieved using its backpressure capability.

http://java.dzone.com/articles/reactive-queue-akka-reactive

Upvotes: 1

Marius Danila
Marius Danila

Reputation: 10411

I don't see the connection between the question's title and the content.

Here is an article about throttling messages in Akka:

http://letitcrash.com/post/28901663062/throttling-messages-in-akka-2

However, you seem puzzled about the fact that your actor is processing only one message at a time. But that's how Akka actors work. They have a single mailbox of messages and they process only one message at a time in a continuous loop.

If you want to handle multiple tasks concurrently with the same work processing unit I suggest you take a look at routers:

http://doc.akka.io/docs/akka/2.1.2/scala/routing.html

Upvotes: 7

Related Questions