-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix some defects of setting default Message headers in MessagingMessageListenerAdapter #2908
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
Changes from all commits
e69916e
bfd20b7
0ca3c88
e393b55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,7 @@ | |
* @author Gary Russell | ||
* @author Artem Bilan | ||
* @author Venil Noronha | ||
* @author Nathan Xu | ||
*/ | ||
public abstract class MessagingMessageListenerAdapter<K, V> implements ConsumerSeekAware { | ||
|
||
|
@@ -470,8 +471,8 @@ protected void sendResponse(Object result, String topic, @Nullable Object source | |
if (!returnTypeMessage && topic == null) { | ||
this.logger.debug(() -> "No replyTopic to handle the reply: " + result); | ||
} | ||
else if (result instanceof Message) { | ||
Message<?> reply = checkHeaders(result, topic, source); | ||
else if (result instanceof Message<?> mResult) { | ||
Message<?> reply = checkHeaders(mResult, topic, source); | ||
this.replyTemplate.send(reply); | ||
} | ||
else { | ||
|
@@ -483,8 +484,9 @@ else if (result instanceof Message) { | |
} | ||
if (iterableOfMessages || this.splitIterables) { | ||
((Iterable<V>) result).forEach(v -> { | ||
if (v instanceof Message) { | ||
this.replyTemplate.send((Message<?>) v); | ||
if (v instanceof Message<?> mv) { | ||
Message<?> aReply = checkHeaders(mv, topic, source); | ||
this.replyTemplate.send(aReply); | ||
} | ||
else { | ||
this.replyTemplate.send(topic, v); | ||
|
@@ -501,24 +503,23 @@ else if (result instanceof Message) { | |
} | ||
} | ||
|
||
private Message<?> checkHeaders(Object result, String topic, @Nullable Object source) { // NOSONAR (complexity) | ||
Message<?> reply = (Message<?>) result; | ||
private Message<?> checkHeaders(Message<?> reply, @Nullable String topic, @Nullable Object source) { // NOSONAR (complexity) | ||
MessageHeaders headers = reply.getHeaders(); | ||
boolean needsTopic = headers.get(KafkaHeaders.TOPIC) == null; | ||
boolean needsTopic = topic != null && headers.get(KafkaHeaders.TOPIC) == null; | ||
boolean sourceIsMessage = source instanceof Message; | ||
boolean needsCorrelation = headers.get(this.correlationHeaderName) == null && sourceIsMessage; | ||
boolean needsCorrelation = headers.get(this.correlationHeaderName) == null && sourceIsMessage | ||
&& getCorrelation((Message<?>) source) != null; | ||
boolean needsPartition = headers.get(KafkaHeaders.PARTITION) == null && sourceIsMessage | ||
&& getReplyPartition((Message<?>) source) != null; | ||
if (needsTopic || needsCorrelation || needsPartition) { | ||
MessageBuilder<?> builder = MessageBuilder.fromMessage(reply); | ||
if (needsTopic) { | ||
builder.setHeader(KafkaHeaders.TOPIC, topic); | ||
} | ||
if (needsCorrelation && sourceIsMessage) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems the second condtion has been implied in the first condition |
||
builder.setHeader(this.correlationHeaderName, | ||
((Message<?>) source).getHeaders().get(this.correlationHeaderName)); | ||
if (needsCorrelation) { | ||
setCorrelation(builder, (Message<?>) source); | ||
} | ||
if (sourceIsMessage && reply.getHeaders().get(KafkaHeaders.REPLY_PARTITION) == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems only |
||
if (needsPartition) { | ||
setPartition(builder, (Message<?>) source); | ||
} | ||
reply = builder.build(); | ||
|
@@ -531,8 +532,8 @@ private void sendSingleResult(Object result, String topic, @Nullable Object sour | |
byte[] correlationId = null; | ||
boolean sourceIsMessage = source instanceof Message; | ||
if (sourceIsMessage | ||
&& ((Message<?>) source).getHeaders().get(this.correlationHeaderName) != null) { | ||
correlationId = ((Message<?>) source).getHeaders().get(this.correlationHeaderName, byte[].class); | ||
&& getCorrelation((Message<?>) source) != null) { | ||
correlationId = getCorrelation((Message<?>) source); | ||
} | ||
if (sourceIsMessage) { | ||
sendReplyForMessageSource(result, topic, source, correlationId); | ||
|
@@ -571,6 +572,18 @@ private void sendReplyForMessageSource(Object result, String topic, Object sourc | |
this.replyTemplate.send(builder.build()); | ||
} | ||
|
||
private void setCorrelation(MessageBuilder<?> builder, Message<?> source) { | ||
byte[] correlationBytes = getCorrelation(source); | ||
if (correlationBytes != null) { | ||
builder.setHeader(this.correlationHeaderName, correlationBytes); | ||
} | ||
} | ||
|
||
@Nullable | ||
private byte[] getCorrelation(Message<?> source) { | ||
return source.getHeaders().get(this.correlationHeaderName, byte[].class); | ||
} | ||
|
||
private void setPartition(MessageBuilder<?> builder, Message<?> source) { | ||
byte[] partitionBytes = getReplyPartition(source); | ||
if (partitionBytes != null) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think choosing
Message<?>
as the first argument type makes more sense, for this method is only meant to be used with that precondition.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure whether we can use
Nullable
totopic
, but from its invocation context, seems we could not rule out the nullness possibility?