-
Notifications
You must be signed in to change notification settings - Fork 1.6k
GH-1189: Asynchronous server-side processing in a request/reply scenario #2996
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9852ebc
Refactor `MessagingMessageListenerAdapter`
4067cfb
GH-1189: support `Mono` and `Future`
3fef61c
GH-1189: support kotlin suspend
aaa6fa4
GH-1189: Auto detect async reply
412d320
GH-1189: `@SendTo` for `@KafkaHandler` after error is handled
a406c85
* add javadoc in `AdapterUtils`
Wzy19930507 92639b6
review fix
Wzy19930507 2a3068e
review fix
Wzy19930507 76a2e0b
* polish `DelegatingInvocableHandler` and add javadoc
Wzy19930507 916dfaf
Mention version in the doc for async return types
artembilan 865de6a
Fix typo for the sentence in doc
artembilan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
.../src/main/antora/modules/ROOT/pages/kafka/receiving-messages/async-returns.adoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[[async-returns]] | ||
= Asynchronous `@KafkaListener` Return Types | ||
|
||
Starting with version 3.2, `@KafkaListener` (and `@KafkaHandler`) methods can be specified with asynchronous return types, letting the reply be sent asynchronously. | ||
return types include `CompletableFuture<?>`, `Mono<?>` and Kotlin `suspend` functions. | ||
|
||
[source, java] | ||
---- | ||
@KafkaListener(id = "myListener", topics = "myTopic") | ||
public CompletableFuture<String> listen(String data) { | ||
... | ||
CompletableFuture<String> future = new CompletableFuture<>(); | ||
future.complete("done"); | ||
return future; | ||
} | ||
---- | ||
|
||
[source, java] | ||
---- | ||
@KafkaListener(id = "myListener", topics = "myTopic") | ||
public Mono<Void> listen(String data) { | ||
... | ||
return Mono.empty(); | ||
} | ||
---- | ||
|
||
IMPORTANT: The `AckMode` will be automatically set the `MANUAL` and enable out-of-order commits when async return types are detected; instead, the asynchronous completion will ack when the async operation completes. | ||
When the async result is completed with an error, whether the message is recover or not depends on the container error handler. | ||
If some exception occurs within the listener method that prevents creation of the async result object, you MUST catch that exception and return an appropriate return object that will cause the message to be ack or recover. | ||
|
||
If a `KafkaListenerErrorHandler` is configured on a listener with an async return type (including Kotlin suspend functions), the error handler is invoked after a failure. | ||
See xref:kafka/annotation-error-handling.adoc[Handling Exceptions] for more information about this error handler and its purpose. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
spring-kafka/src/main/java/org/springframework/kafka/listener/adapter/AsyncRepliesAware.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2023-2024 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 org.springframework.kafka.listener.adapter; | ||
|
||
/** | ||
* Message handler adapter implementing this interface can detect {@link HandlerAdapter} async return types. | ||
* | ||
* @author Wang zhiyang | ||
* | ||
* @since 3.2 | ||
*/ | ||
public interface AsyncRepliesAware { | ||
|
||
/** | ||
* Return true if the {@link HandlerAdapter} return type is async. | ||
* @return true for async replies. | ||
* @since 3.2 | ||
*/ | ||
default boolean isAsyncReplies() { | ||
return false; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.