Go to: a bolt in storm sends emit messages of the same type multiple times.

Source: Internet
Author: User

After the logic can be processed in bolts in storm, the message will be sent by the back-to-end blot.

You can send multiple different messages, such:

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

You can also send different messages of the same type, for example;

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));            }

The above statement is correct, because the object sent each time in the for loop is a new instance, but if you put the action of creating an instance outside, such:

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));            }

In this case, the problem occurs. According to the logic, we want to have its emit multiple messages of different emaildeliverstatus objects. But in fact, this is not the case because storm's emit operations are not executed immediately,

The above Code is based on the assumption that after emit is called, storm will immediately send messages. If you follow the preceding statement, bolts will find that multiple messages in the for loop received by the receiver are repeated for multiple times in the last message.

Storm does not execute emit immediately, but instead executes emit at a fixed time of the bolt execution. Therefore, the initialization of the emaildeliverstatus instance must be executed outside the for loop.

Address: http://blog.csdn.net/jsjwk/article/details/8495915

Go to: a bolt in storm sends emit messages of the same type multiple times.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.