2

你们能否展示在 Akka 中限制消息的示例?

这是我的代码

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()
  }
}

这是我的配置

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

但是当我运行它时,它似乎是单线程的,因为我希望同时处理 4 条消息。

我在这里想念什么?

4

2 回答 2

7

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

于 2013-04-15T05:18:55.427 回答
1

Typesafe 最近宣布了 akka 反应式流。可以使用其背压能力来实现节流。

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

于 2014-08-05T06:16:35.143 回答