轉:storm中一個Bolt發emit多次相同類型訊息

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   ar   for   sp   div   

在storm中的Bolt中可以處理完成邏輯後,向後面的Blot繼續發送訊息。

可以發送多個不同的訊息,如:

collector.emit("update-delivered-status",new Values(emailDeliverStatus));    collector.emit("save-request",new Values(udsn));  

也可以同一個類型的訊息發送多個不同內容如;

for (int i = 0; i < emailParamVo.getReceiverNum(); i++)             {                EmailDeliverStatus emailDeliverStatus = new EmailDeliverStatus();                emailDeliverStatus.setCategoryId(emailParamVo.getCategoryId());                emailDeliverStatus.setUpdateTime(emailParamVo.getUpdateTime());                emailDeliverStatus.setStatus(emailParamVo.getEventType());                emailDeliverStatus.setUserId(emailParamVo.getUserId());                emailDeliverStatus.setMessageDetail(emailParamVo.getMessageDetail());                                StringBuilder receiverBuilder = new StringBuilder(emailParamVo.getReceivers());                receiverBuilder = receiverBuilder.deleteCharAt(0);                receiverBuilder = receiverBuilder.deleteCharAt(receiverBuilder.length()-1);                String[] receivers = receiverBuilder.toString().split(" ");                String receiver = receivers[i];                emailDeliverStatus.setEmailId(emailParamVo.getEmailIdPre() + i + "$" + receiver);                emailDeliverStatus.setReceiver(receiver);                collector.emit("update-delivered-status",new Values(emailDeliverStatus));            }

上面的寫法是沒有問題的,因為for迴圈裡面每次發送的對象都是一個新的執行個體,但是如果把建立執行個體的動作放到外面,如:

EmailDeliverStatus emailDeliverStatus = new EmailDeliverStatus();            emailDeliverStatus.setCategoryId(emailParamVo.getCategoryId());            emailDeliverStatus.setUpdateTime(emailParamVo.getUpdateTime());            emailDeliverStatus.setStatus(emailParamVo.getEventType());            emailDeliverStatus.setUserId(emailParamVo.getUserId());            emailDeliverStatus.setMessageDetail(emailParamVo.getMessageDetail());                        for (int i = 0; i < emailParamVo.getReceiverNum(); i++)             {                StringBuilder receiverBuilder = new StringBuilder(emailParamVo.getReceivers());                receiverBuilder = receiverBuilder.deleteCharAt(0);                receiverBuilder = receiverBuilder.deleteCharAt(receiverBuilder.length()-1);                String[] receivers = receiverBuilder.toString().split(" ");                String receiver = receivers[i];                emailDeliverStatus.setEmailId(emailParamVo.getEmailIdPre() + i + "$" + receiver);                emailDeliverStatus.setReceiver(receiver);                collector.emit("update-delivered-status",new Values(emailDeliverStatus));            }

這樣就有問題,按照邏輯,我們是想讓其emit多個不同emailDeliverStatus對象的訊息,但是實際上這樣不不行的,因為storm的emit操作並不是立即執行的,

上面的代碼就是建立在假設調用emit後,storm就會立即去發送訊息。如果按照上面寫法,會發現接收訊息的bolt收到的for迴圈中的多個訊息都是最後一個訊息的重複多次。

因為storm並不是立即執行emit,而是在這個bolt執行的一個固定時間去emit的,所以emailDeliverStatus執行個體的初始化必須放到for迴圈的外面執行。

原文地址:http://blog.csdn.net/jsjwk/article/details/8495915

轉:storm中一個Bolt發emit多次相同類型訊息

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.