日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用Akka处理1000万条消息

發布時間:2023/12/3 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Akka处理1000万条消息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Akka演員承諾并發。 有什么更好的模擬方式,看看使用商品硬件和軟件處理1000萬條消息需要花費多少時間,而無需進行任何低級調整。我用Java編寫了整個1000萬條消息的處理過程,整個結果令我驚訝。

當我在具有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

總結

以上是生活随笔為你收集整理的使用Akka处理1000万条消息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。