2013-04-15 40 views
2

請問你們可以在阿卡展示節流消息的例子嗎?如何在Akka中節流消息(2.1.2)?

這裏是我的代碼

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個消息,在被處理同時。

我在這裏錯過了什麼?

回答

7

我沒有看到問題標題和內容之間的聯繫。

以下是有關在阿卡節流消息的文章:

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

但是,你似乎感到疑惑的事實,你的演員在同一時間處理只有一個消息。但這就是Akka演員的工作方式。他們有一個單一的郵件信箱,他們一次只能連續處理一條信息。

如果你想用相同的工作處理單元,我建議你看看路由器同時處理多個任務:

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