Replace ListenableFuture with CompletableFuture

Related to https://github.com/spring-projects/spring-kafka/issues/2357
This commit is contained in:
Henning Poettker
2022-07-22 22:37:31 +02:00
committed by Mahmoud Ben Hassine
parent ee7d12671b
commit a2cf74adec
3 changed files with 23 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2021 the original author or authors. * Copyright 2019-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -21,10 +21,10 @@ import org.springframework.batch.item.KeyValueItemWriter;
import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult; import org.springframework.kafka.support.SendResult;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.concurrent.ListenableFuture;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@@ -42,24 +42,24 @@ public class KafkaItemWriter<K, T> extends KeyValueItemWriter<K, T> {
protected KafkaTemplate<K, T> kafkaTemplate; protected KafkaTemplate<K, T> kafkaTemplate;
private final List<ListenableFuture<SendResult<K, T>>> listenableFutures = new ArrayList<>(); private final List<CompletableFuture<SendResult<K, T>>> completableFutures = new ArrayList<>();
private long timeout = -1; private long timeout = -1;
@Override @Override
protected void writeKeyValue(K key, T value) { protected void writeKeyValue(K key, T value) {
if (this.delete) { if (this.delete) {
this.listenableFutures.add(this.kafkaTemplate.sendDefault(key, null)); this.completableFutures.add(this.kafkaTemplate.sendDefault(key, null));
} }
else { else {
this.listenableFutures.add(this.kafkaTemplate.sendDefault(key, value)); this.completableFutures.add(this.kafkaTemplate.sendDefault(key, value));
} }
} }
@Override @Override
protected void flush() throws Exception { protected void flush() throws Exception {
this.kafkaTemplate.flush(); this.kafkaTemplate.flush();
for (ListenableFuture<SendResult<K, T>> future : this.listenableFutures) { for (var future : this.completableFutures) {
if (this.timeout >= 0) { if (this.timeout >= 0) {
future.get(this.timeout, TimeUnit.MILLISECONDS); future.get(this.timeout, TimeUnit.MILLISECONDS);
} }
@@ -67,7 +67,7 @@ public class KafkaItemWriter<K, T> extends KeyValueItemWriter<K, T> {
future.get(); future.get();
} }
} }
this.listenableFutures.clear(); this.completableFutures.clear();
} }
@Override @Override

View File

@@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.admin.NewTopic;
@@ -38,10 +39,8 @@ import org.springframework.batch.item.ExecutionContext;
import org.springframework.kafka.core.DefaultKafkaProducerFactory; import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory; import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.SendResult;
import org.springframework.kafka.test.rule.EmbeddedKafkaRule; import org.springframework.kafka.test.rule.EmbeddedKafkaRule;
import org.springframework.kafka.test.utils.KafkaTestUtils; import org.springframework.kafka.test.utils.KafkaTestUtils;
import org.springframework.util.concurrent.ListenableFuture;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder;
@@ -187,12 +186,12 @@ public class KafkaItemReaderTests {
@Test @Test
public void testReadFromSinglePartition() throws ExecutionException, InterruptedException { public void testReadFromSinglePartition() throws ExecutionException, InterruptedException {
this.template.setDefaultTopic("topic1"); this.template.setDefaultTopic("topic1");
List<ListenableFuture<SendResult<String, String>>> futures = new ArrayList<>(); var futures = new ArrayList<CompletableFuture<?>>();
futures.add(this.template.sendDefault("val0")); futures.add(this.template.sendDefault("val0"));
futures.add(this.template.sendDefault("val1")); futures.add(this.template.sendDefault("val1"));
futures.add(this.template.sendDefault("val2")); futures.add(this.template.sendDefault("val2"));
futures.add(this.template.sendDefault("val3")); futures.add(this.template.sendDefault("val3"));
for (ListenableFuture<SendResult<String, String>> future : futures) { for (var future : futures) {
future.get(); future.get();
} }
@@ -221,12 +220,12 @@ public class KafkaItemReaderTests {
@Test @Test
public void testReadFromSinglePartitionFromCustomOffset() throws ExecutionException, InterruptedException { public void testReadFromSinglePartitionFromCustomOffset() throws ExecutionException, InterruptedException {
this.template.setDefaultTopic("topic5"); this.template.setDefaultTopic("topic5");
List<ListenableFuture<SendResult<String, String>>> futures = new ArrayList<>(); var futures = new ArrayList<CompletableFuture<?>>();
futures.add(this.template.sendDefault("val0")); // <-- offset 0 futures.add(this.template.sendDefault("val0")); // <-- offset 0
futures.add(this.template.sendDefault("val1")); // <-- offset 1 futures.add(this.template.sendDefault("val1")); // <-- offset 1
futures.add(this.template.sendDefault("val2")); // <-- offset 2 futures.add(this.template.sendDefault("val2")); // <-- offset 2
futures.add(this.template.sendDefault("val3")); // <-- offset 3 futures.add(this.template.sendDefault("val3")); // <-- offset 3
for (ListenableFuture<SendResult<String, String>> future : futures) { for (var future : futures) {
future.get(); future.get();
} }
@@ -257,10 +256,10 @@ public class KafkaItemReaderTests {
// first run: read a topic from the beginning // first run: read a topic from the beginning
this.template.setDefaultTopic("topic6"); this.template.setDefaultTopic("topic6");
List<ListenableFuture<SendResult<String, String>>> futures = new ArrayList<>(); var futures = new ArrayList<CompletableFuture<?>>();
futures.add(this.template.sendDefault("val0")); // <-- offset 0 futures.add(this.template.sendDefault("val0")); // <-- offset 0
futures.add(this.template.sendDefault("val1")); // <-- offset 1 futures.add(this.template.sendDefault("val1")); // <-- offset 1
for (ListenableFuture<SendResult<String, String>> future : futures) { for (var future : futures) {
future.get(); future.get();
} }
this.reader = new KafkaItemReader<>(this.consumerProperties, "topic6", 0); this.reader = new KafkaItemReader<>(this.consumerProperties, "topic6", 0);
@@ -311,12 +310,12 @@ public class KafkaItemReaderTests {
@Test @Test
public void testReadFromMultiplePartitions() throws ExecutionException, InterruptedException { public void testReadFromMultiplePartitions() throws ExecutionException, InterruptedException {
this.template.setDefaultTopic("topic2"); this.template.setDefaultTopic("topic2");
List<ListenableFuture<SendResult<String, String>>> futures = new ArrayList<>(); var futures = new ArrayList<CompletableFuture<?>>();
futures.add(this.template.sendDefault("val0")); futures.add(this.template.sendDefault("val0"));
futures.add(this.template.sendDefault("val1")); futures.add(this.template.sendDefault("val1"));
futures.add(this.template.sendDefault("val2")); futures.add(this.template.sendDefault("val2"));
futures.add(this.template.sendDefault("val3")); futures.add(this.template.sendDefault("val3"));
for (ListenableFuture<SendResult<String, String>> future : futures) { for (var future : futures) {
future.get(); future.get();
} }
@@ -339,13 +338,13 @@ public class KafkaItemReaderTests {
@Test @Test
public void testReadFromSinglePartitionAfterRestart() throws ExecutionException, InterruptedException { public void testReadFromSinglePartitionAfterRestart() throws ExecutionException, InterruptedException {
this.template.setDefaultTopic("topic3"); this.template.setDefaultTopic("topic3");
List<ListenableFuture<SendResult<String, String>>> futures = new ArrayList<>(); var futures = new ArrayList<CompletableFuture<?>>();
futures.add(this.template.sendDefault("val0")); futures.add(this.template.sendDefault("val0"));
futures.add(this.template.sendDefault("val1")); futures.add(this.template.sendDefault("val1"));
futures.add(this.template.sendDefault("val2")); futures.add(this.template.sendDefault("val2"));
futures.add(this.template.sendDefault("val3")); futures.add(this.template.sendDefault("val3"));
futures.add(this.template.sendDefault("val4")); futures.add(this.template.sendDefault("val4"));
for (ListenableFuture<SendResult<String, String>> future : futures) { for (var future : futures) {
future.get(); future.get();
} }
ExecutionContext executionContext = new ExecutionContext(); ExecutionContext executionContext = new ExecutionContext();
@@ -375,7 +374,7 @@ public class KafkaItemReaderTests {
@Test @Test
public void testReadFromMultiplePartitionsAfterRestart() throws ExecutionException, InterruptedException { public void testReadFromMultiplePartitionsAfterRestart() throws ExecutionException, InterruptedException {
List<ListenableFuture<SendResult<String, String>>> futures = new ArrayList<>(); var futures = new ArrayList<CompletableFuture<?>>();
futures.add(this.template.send("topic4", 0, null, "val0")); futures.add(this.template.send("topic4", 0, null, "val0"));
futures.add(this.template.send("topic4", 0, null, "val2")); futures.add(this.template.send("topic4", 0, null, "val2"));
futures.add(this.template.send("topic4", 0, null, "val4")); futures.add(this.template.send("topic4", 0, null, "val4"));
@@ -385,7 +384,7 @@ public class KafkaItemReaderTests {
futures.add(this.template.send("topic4", 1, null, "val5")); futures.add(this.template.send("topic4", 1, null, "val5"));
futures.add(this.template.send("topic4", 1, null, "val7")); futures.add(this.template.send("topic4", 1, null, "val7"));
for (ListenableFuture<?> future : futures) { for (var future : futures) {
future.get(); future.get();
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2021 the original author or authors. * Copyright 2019-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package org.springframework.batch.item.kafka;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.Before; import org.junit.Before;
@@ -28,7 +29,6 @@ import org.mockito.junit.MockitoRule;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult; import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@@ -46,7 +46,7 @@ public class KafkaItemWriterTests {
private KafkaTemplate<String, String> kafkaTemplate; private KafkaTemplate<String, String> kafkaTemplate;
@Mock @Mock
private ListenableFuture<SendResult<String, String>> future; private CompletableFuture<SendResult<String, String>> future;
private KafkaItemKeyMapper itemKeyMapper; private KafkaItemKeyMapper itemKeyMapper;