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.