批量消息发送--基于SpringBootStarter

代码示例:

import org.springframework.messaging.Message;

@RestController
public class HelloController {
    
    @Autowired
    private RocketMQTemplate mqTemplate;
    
    private void testBatchMessages() {
        List<Message> msgs = new ArrayList<Message>();
        for (int i = 1; i <= 10; i++) {
            msgs.add(MessageBuilder.withPayload("hello makai" + i).
                setHeader(RocketMQHeaders.KEYS, "KEY_" + i).build());
        }

        SendResult sr = mqTemplate.syncSend("mk-test:abc", msgs, 60000);

        System.out.printf("--- 批量发送消息结果 :" + sr);
    }
}

注意事项:
1.返回的msgId是多个,顺序,并且以逗号隔开。
2.需要修改broker和提供者的 maxMessageSize  (本地会有校验,如果过不去,有可能到不了broker上就报错)
3.报以下错,也是因为大小的事。

Caused by: org.apache.rocketmq.client.exception.MQBrokerException: CODE: 13  DESC: the message is illegal, maybe msg body or properties length not matched. msg body length limit 128k, msg properties length limit 32k.
Last modification:January 24th, 2020 at 01:31 am
如果觉得我的文章对你有用,请随意赞赏