當我在具有Intel i5 – 4核,4 Gb RAM計算機和JVM堆的iMac計算機上以1024Mb運行該程序時,該程序在23秒內處理了1000萬臺計算機。 我多次運行該程序,平均時間為25秒。 因此,我收到的吞吐量幾乎在每秒40萬條消息的范圍內,這是驚人的。
下圖說明了用于模擬負載生成方案的流程。

警告:每條消息在1秒鐘后發送響應,這對于實際情況而言并非正確的模擬。 在這種情況下,消息處理將消耗堆和gc活動上的一些資源,這些資源未考慮在內。
該程序使用了Akka發布者的總體指導:在75秒內處理了1000萬條消息(每條消息1秒)! 盡管沒有任何限制。
該程序的代碼庫位于以下位置– https://github.com/write2munish/Akka-Essentials
ApplicationManagerSystem創建actor,并在到WorkerActor的流量中進行泵送
private ActorSystem system;private final ActorRef router;private final static int no_of_msgs = 10 * 1000000;public ApplicationManagerSystem() {final int no_of_workers = 10;system = ActorSystem.create('LoadGeneratorApp');final ActorRef appManager = system.actorOf(new Props(new UntypedActorFactory() {public UntypedActor create() {return new JobControllerActor(no_of_msgs);}}), 'jobController');router = system.actorOf(new Props(new UntypedActorFactory() {public UntypedActor create() {return new WorkerActor(appManager);}}).withRouter(new RoundRobinRouter(no_of_workers)));}private void generateLoad() {for (int i = no_of_msgs; i >= 0; i--) {router.tell('Job Id ' + i + '# send');}System.out.println('All jobs sent successfully');}
一旦WorkerActor收到了消息,則計劃將響應在1000毫秒后發送
public class WorkerActor extends UntypedActor {private ActorRef jobController;@Overridepublic void onReceive(Object message) throws Exception {using scheduler to send the reply after 1000 millisecondsgetContext().system().scheduler().scheduleOnce(Duration.create(1000, TimeUnit.MILLISECONDS),jobController, 'Done');}public WorkerActor(ActorRef inJobController) {jobController = inJobController;}}
來自WorkerActor的響應消息被發送到JobControllerActor,后者收集所有響應。
public class JobControllerActor extends UntypedActor {int count = 0;long startedTime = System.currentTimeMillis();int no_of_msgs = 0;@Overridepublic void onReceive(Object message) throws Exception {if (message instanceof String) {if (((String) message).compareTo('Done') == 0) {count++;if (count == no_of_msgs) {long now = System.currentTimeMillis();System.out.println('All messages processed in '+ (now - startedTime) 1000 + ' seconds');System.out.println('Total Number of messages processed '+ count);getContext().system().shutdown();}}}}}
參考: 教程:Hibernate,JPA和Spring MVC –來自Akka Essentials博客的JCG合作伙伴 Munish K Gupta的第2部分 。
翻譯自: https://www.javacodegeeks.com/2012/05/processing-10-million-messages-with.html