A Bolt in the storm emits the same type of message multiple times

Hits: 0

After the logic can be processed in the Bolt in storm, continue to send messages to the following Blot.

Several different messages can be sent, such as:

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

collector.emit("save-request",new Values(udsn));

It is also possible to send multiple different content in the same type of message such as;

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 way of writing is no problem, because each object sent in the for loop is a new instance, but if you put the action of creating an instance outside, such as:

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

There is a problem in this. According to the logic, we want to let it emit the messages of multiple different emailDeliverStatus objects, but in fact, this is not possible, because the emit operation of storm is not executed immediately.

The above code is based on the assumption that after calling emit, Storm will send the message immediately. If you follow the above writing method, you will find that the multiple messages in the for loop received by the bolt receiving the message are the repetition of the last message multiple times.

Because Storm does not execute the emit immediately, but at a fixed time when the bolt is executed, the initialization of the emailDeliverStatus instance must be executed outside the for loop.

The e-book “Talking about Microservice Architecture” written by me includes microservice technology, from the introduction of microservices, to technical books on construction, deployment, and the use of various modules. Most of the content is derived from CSDN articles I wrote.

/download/jsjwk/13585763

You may also like...

Leave a Reply

Your email address will not be published.