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

Use the original key in reply messages when using ReplyingKafkaTemplate and SendTo #3395

Closed
neshkeev opened this issue Jul 28, 2024 · 2 comments · Fixed by #3404
Closed

Use the original key in reply messages when using ReplyingKafkaTemplate and SendTo #3395

neshkeev opened this issue Jul 28, 2024 · 2 comments · Fixed by #3404

Comments

@neshkeev
Copy link

Expected Behavior

Response messages in addition to the kafka_correlationId Header should have the same keys as their corresponding request messages when constructing the Request-Reply semantics on top of ReplyingKafkaTemplate and SendTo.

Current Behavior

The response messages have no keys (null)

Context

When one sends a message/request it's only natural to expect that the response message have same key as the original message (request). Such an explicit formalization can allow engineers to reason about the workload. Also it might come in handy when there are different consumers listening the reply topic.

Here is a brief example of what this issue is about

Sender:

void sender(ReplyingKafkaTemplate<String, String, String> template) {
    final ProducerRecord<String, String> record = new ProducerRecord<>(
            "mytopic",
            "key",
            "value"
    );

    var replay = template.sendAndReceive(record, responseTimeout);

    // THIS ASSERTION FAILS
    assert "key".equals(replay.get().key()) : "The response should have the same key";
}

Listener:

@SendTo
@Listener(topics="mytopic")
String listen(String value) {
    return "Hello, " + value;
}

If you run this snippet, you will get the assertion error, because the key of the replying message is null.

@artembilan
Copy link
Member

This makes sense.
Feel free to contribute the fix: https://github.com/spring-projects/spring-kafka/blob/main/CONTRIBUTING.adoc
Thanks

@sobychacko
Copy link
Contributor

I am taking a look at this issue. It's straightforward with a single return record, as in the example above. I wonder what the semantics are if the return type is a batch or a collection. In the case of a collection, the framework may not detect what keys to provide, and it is up to the application to provide the keys by wrapping them in a Message. What do you think?

sobychacko added a commit to sobychacko/spring-kafka that referenced this issue Aug 2, 2024
Fixes: spring-projects#3395

When using `ReplyingKafkaTemplate`, include the original key from the
request record if there exists such a key.
sobychacko added a commit to sobychacko/spring-kafka that referenced this issue Aug 2, 2024
Fixes: spring-projects#3395

When using `ReplyingKafkaTemplate,` include the original key from the request record if such a key exists.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants