Akka actor一次处理一条消息

时间:2016-04-21 19:18:34

标签: java akka reactive-programming

Akka actor一次只能处理一条消息,因此我们不需要担心并发性。

考虑一下我有披萨店的系统。

设计就像这样

1)Webservice 
2)Akka ecosystem with actors like PizaaCreater, DeliveryHandler 
3)Web service accepts ther order and passes to PizzaCreater,PizzaCreater creates pizza and passes it to DeliveryHanlder.

现在只有一个PizzaCreater和DeliveryHandler实例。这会降低整个系统的速度。

1)Should i create multiple PizzaCreaters and DeliveryHandler?
2)How will CustomerService pass order to PizzaCreater with minimum load or how would PizzaCreater pass the pizza to DelivieryHandler with minimum load then?
3)Is creating multiple instance of PizzaCreaters / DeliveryHandler ok?

1 个答案:

答案 0 :(得分:2)

  1. If the "pizza creation" is a blocking operation (uses network/db) create a router with PizzaCreatorWorkers (the number of workers/threads depends on your scenario). If the "pizza creation" is non-blocking - just create one actor per request. Same thing for Delivery: create a router with a fixed number of DeliveryHandles if the delivery is blocking otherwise just create an actor and pass it "the pizza".

  2. In all cases the order passing should be done through non-mutable messages : you should not modify the order once you passed it down the chain. You should not worry about the "load" as passing messages is what Akka does so it is very efficient - you just pass references between actors.

  3. The akka docs specify that the akka system is designed to handle millions of actors, so never worry about "multiple instances" of actors as long as this is what the logic of your application requires.

From akka.io main page:

50 million msg/sec on a single machine. Small memory footprint; ~2.5 million actors per GB of heap.