You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Resolves#2357
Spring Framework is planning to deprecate `ListenableFuture` in 6.0.
Add methods to the `KafkaOperations` (`KafkaTemplate`) that return
`CompletableFuture` instead; the `ListenableFuture` methods have now
been removed in 3.0.
* Remove KafkaOperations2; doc polishing.
* Fix import.
Copy file name to clipboardExpand all lines: spring-kafka-docs/src/main/asciidoc/changes-since-1.0.adoc
+9Lines changed: 9 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -33,13 +33,22 @@ You can now configure which inbound headers should be mapped.
33
33
Also available in version 2.8.8 or later.
34
34
See <<headers>> for more information.
35
35
36
+
[[x29-template-changes]]
37
+
==== `KafkaTemplate` Changes
38
+
39
+
In 3.0, the futures returned by this class will be `CompletableFuture` s instead of `ListenableFuture` s.
40
+
See <<kafka-template>> for assistance in transitioning when using this release.
41
+
36
42
[[x29-rkt-changes]]
37
43
==== `ReplyingKafkaTemplate` Changes
38
44
39
45
The template now provides a method to wait for assignment on the reply container, to avoid a race when sending a request before the reply container is initialized.
40
46
Also available in version 2.8.8 or later.
41
47
See <<replying-template>>.
42
48
49
+
In 3.0, the futures returned by this class will be `CompletableFuture` s instead of `ListenableFuture` s.
50
+
See <<replying-template>> and <<exchanging-messages>> for assistance in transitioning when using this release.
51
+
43
52
=== What's New in 2.8 Since 2.7
44
53
45
54
This section covers the changes made from version 2.7 to version 2.8.
See the https://docs.spring.io/spring-kafka/api/org/springframework/kafka/core/KafkaTemplate.html[Javadoc] for more detail.
212
212
213
+
IMPORTANT: In version 3.0, the methods that previously returned `ListenableFuture` have been changed to return `CompletableFuture`.
214
+
To facilitate the migration, the 2.9 version added a method `.usingCompletableFuture()` which provided the same methods with `CompletableFuture` return types; this method is no longer available.
215
+
213
216
The `sendDefault` API requires that a default topic has been provided to the template.
214
217
215
218
The API takes in a `timestamp` as a parameter and stores this timestamp in the record.
@@ -302,75 +305,27 @@ By default, the template is configured with a `LoggingProducerListener`, which l
302
305
303
306
For convenience, default method implementations are provided in case you want to implement only one of the methods.
304
307
305
-
Notice that the send methods return a `ListenableFuture<SendResult>`.
308
+
Notice that the send methods return a `CompletableFuture<SendResult>`.
306
309
You can register a callback with the listener to receive the result of the send asynchronously.
`SendResult` has two properties, a `ProducerRecord` and `RecordMetadata`.
330
323
See the Kafka API documentation for information about those objects.
331
324
332
-
The `Throwable` in `onFailure` can be cast to a `KafkaProducerException`; its `failedProducerRecord` property contains the failed record.
333
-
334
-
Starting with version 2.5, you can use a `KafkaSendCallback` instead of a `ListenableFutureCallback`, making it easier to extract the failed `ProducerRecord`, avoiding the need to cast the `Throwable`:
The `Throwable` can be cast to a `KafkaProducerException`; its `failedProducerRecord` property contains the failed record.
371
326
372
327
If you wish to block the sending thread to await the result, you can invoke the future's `get()` method; using the method with a timeout is recommended.
373
-
You may wish to invoke `flush()` before waiting or, for convenience, the template has a constructor with an `autoFlush` parameter that causes the template to `flush()` on each send.
328
+
If you have set a `linger.ms`, you may wish to invoke `flush()` before waiting or, for convenience, the template has a constructor with an `autoFlush` parameter that causes the template to `flush()` on each send.
374
329
Flushing is only needed if you have set the `linger.ms` producer property and want to immediately send a partial batch.
375
330
376
331
====== Examples
@@ -384,19 +339,14 @@ This section shows examples of sending messages to Kafka:
384
339
public void sendToKafka(final MyOutputData data) {
385
340
final ProducerRecord<String, String> record = createRecord(data);
The result is a `ListenableFuture` that is asynchronously populated with the result (or an exception, for a timeout).
502
+
The result is a `CompletableFuture` that is asynchronously populated with the result (or an exception, for a timeout).
553
503
The result also has a `sendFuture` property, which is the result of calling `KafkaTemplate.send()`.
554
504
You can use this future to determine the result of the send operation.
555
505
506
+
IMPORTANT: In version 3.0, the futures returned by these methods (and their `sendFuture` properties) have been changed to `CompletableFuture` s instead of `ListenableFuture` s.
507
+
556
508
If the first method is used, or the `replyTimeout` argument is `null`, the template's `defaultReplyTimeout` property is used (5 seconds by default).
557
509
558
510
Starting with version 2.8.8, the template has a new method `waitForAssignment`.
These will use the template's default `replyTimeout`, there are also overloaded versions that can take a timeout in the method call.
793
745
746
+
IMPORTANT: In version 3.0, the futures returned by these methods (and their `sendFuture` properties) have been changed to `CompletableFuture` s instead of `ListenableFuture` s.
747
+
794
748
Use the first method if the consumer's `Deserializer` or the template's `MessageConverter` can convert the payload without any additional information, either via configuration or type metadata in the reply message.
795
749
796
750
Use the second method if you need to provide type information for the return type, to assist the message converter.
@@ -2236,6 +2190,7 @@ public ConcurrentKafkaListenerContainerFactory<Integer, String> kafkaListenerCon
2236
2190
====
2237
2191
2238
2192
When you use `@SendTo`, you must configure the `ConcurrentKafkaListenerContainerFactory` with a `KafkaTemplate` in its `replyTemplate` property to perform the send.
2193
+
Spring Boot will automatically wire in its auto configured template (or any if a single instance is present).
2239
2194
2240
2195
NOTE: Unless you use <<replying-template,request/reply semantics>> only the simple `send(topic, value)` method is used, so you may wish to create a subclass to generate the partition or key.
2241
2196
The following example shows how to do so:
@@ -2248,7 +2203,7 @@ public KafkaTemplate<String, String> myReplyingTemplate() {
2248
2203
return new KafkaTemplate<Integer, String>(producerFactory()) {
2249
2204
2250
2205
@Override
2251
-
public ListenableFuture<SendResult<String, String>> send(String topic, String data) {
2206
+
public CompletableFuture<SendResult<String, String>> send(String topic, String data) {
0 commit comments