Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.2.x rocketmq multi-broker offset error and blank header support #2572

Merged
merged 1 commit into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
* @author <a href="mailto:[email protected]">Jim</a>
Expand Down Expand Up @@ -108,13 +109,15 @@ public synchronized void start() {
this.running = true;
}

private MessageQueue acquireCurrentMessageQueue(String topic,int queueId) {
Collection<MessageQueue> messageQueueSet = messageQueuesForTopic.get(topic);
if(CollectionUtils.isEmpty(messageQueueSet)){
private MessageQueue acquireCurrentMessageQueue(String topic, int queueId,
String brokerName) {
Collection<MessageQueue> messageQueueSet = messageQueuesForTopic.get(topic);
if (CollectionUtils.isEmpty(messageQueueSet)) {
return null;
}
for (MessageQueue messageQueue : messageQueueSet) {
if (messageQueue.getQueueId() == queueId) {
if (messageQueue.getQueueId() == queueId && ObjectUtils
.nullSafeEquals(brokerName, messageQueue.getBrokerName())) {
return messageQueue;
}
}
Expand Down Expand Up @@ -151,7 +154,8 @@ protected synchronized Object doReceive() {
if (null == messageExt) {
return null;
}
MessageQueue messageQueue = this.acquireCurrentMessageQueue(messageExt.getTopic(),messageExt.getQueueId());
MessageQueue messageQueue = this.acquireCurrentMessageQueue(messageExt.getTopic(), messageExt.getQueueId(),
messageExt.getBrokerName());
if (messageQueue == null) {
throw new IllegalArgumentException(
"The message queue is not in assigned list");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,11 @@ private static org.apache.rocketmq.common.message.Message getAndWrapMessage(
.filter(entry -> !Objects.equals(entry.getKey(), Headers.FLAG))
.forEach(entry -> {
if (!MessageConst.STRING_HASH_SET.contains(entry.getKey())) {
rocketMsg.putUserProperty(entry.getKey(),
String.valueOf(entry.getValue()));
String val = String.valueOf(entry.getValue());
// Remove All blank header(rocketmq not support).
if (org.apache.commons.lang3.StringUtils.isNotBlank(val)) {
rocketMsg.putUserProperty(entry.getKey(), val);
}
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.stream.binder.rocketmq;

import com.alibaba.cloud.stream.binder.rocketmq.support.RocketMQMessageConverterSupport;
import org.apache.rocketmq.common.message.MessageConst;
import org.junit.jupiter.api.Test;

import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Sorie
*/
public class RocketMQMessageConverterSupportTest {

@Test
public void convertMessage2MQBlankHeaderTest() {
String destination = "test";
Message message = MessageBuilder.withPayload("msg")
.setHeader(MessageConst.PROPERTY_TAGS, "a")
.setHeader("test", "")
.build();
org.apache.rocketmq.common.message.Message rkmqMsg =
RocketMQMessageConverterSupport.convertMessage2MQ(destination, message);
String testProp = rkmqMsg.getProperty("test");
String tagProp = rkmqMsg.getProperty(MessageConst.PROPERTY_TAGS);
assertThat(testProp).isNull();
assertThat(tagProp).isEqualTo("a");
}
}