Put back "Replace ListenableFuture with CompletableFuture"

Related to a165247284.
This commit is contained in:
Mahmoud Ben Hassine
2022-08-24 20:07:33 +02:00
parent bd25df6770
commit 42b1b2f2c9
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");
* 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.support.SendResult;
import org.springframework.util.Assert;
import org.springframework.util.concurrent.ListenableFuture;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
/**
@@ -42,24 +42,24 @@ public class KafkaItemWriter<K, T> extends KeyValueItemWriter<K, T> {
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;
@Override
protected void writeKeyValue(K key, T value) {
if (this.delete) {
this.listenableFutures.add(this.kafkaTemplate.sendDefault(key, null));
this.completableFutures.add(this.kafkaTemplate.sendDefault(key, null));
}
else {
this.listenableFutures.add(this.kafkaTemplate.sendDefault(key, value));
this.completableFutures.add(this.kafkaTemplate.sendDefault(key, value));
}
}
@Override
protected void flush() throws Exception {
this.kafkaTemplate.flush();
for (ListenableFuture<SendResult<K, T>> future : this.listenableFutures) {
for (var future : this.completableFutures) {
if (this.timeout >= 0) {
future.get(this.timeout, TimeUnit.MILLISECONDS);
}
@@ -67,7 +67,7 @@ public class KafkaItemWriter<K, T> extends KeyValueItemWriter<K, T> {
future.get();
}
}
this.listenableFutures.clear();
this.completableFutures.clear();
}
@Override

View File

@@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.admin.NewTopic;
@@ -39,12 +40,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.SendResult;
import org.springframework.kafka.test.EmbeddedKafkaBroker;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.kafka.test.utils.KafkaTestUtils;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.util.concurrent.ListenableFuture;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
@@ -143,12 +142,12 @@ class KafkaItemReaderTests {
@Test
void testReadFromSinglePartition() throws ExecutionException, InterruptedException {
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("val1"));
futures.add(this.template.sendDefault("val2"));
futures.add(this.template.sendDefault("val3"));
for (ListenableFuture<SendResult<String, String>> future : futures) {
for (var future : futures) {
future.get();
}
@@ -177,12 +176,12 @@ class KafkaItemReaderTests {
@Test
void testReadFromSinglePartitionFromCustomOffset() throws ExecutionException, InterruptedException {
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("val1")); // <-- offset 1
futures.add(this.template.sendDefault("val2")); // <-- offset 2
futures.add(this.template.sendDefault("val3")); // <-- offset 3
for (ListenableFuture<SendResult<String, String>> future : futures) {
for (var future : futures) {
future.get();
}
@@ -213,10 +212,10 @@ class KafkaItemReaderTests {
// first run: read a topic from the beginning
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("val1")); // <-- offset 1
for (ListenableFuture<SendResult<String, String>> future : futures) {
for (var future : futures) {
future.get();
}
this.reader = new KafkaItemReader<>(this.consumerProperties, "topic6", 0);
@@ -267,12 +266,12 @@ class KafkaItemReaderTests {
@Test
void testReadFromMultiplePartitions() throws ExecutionException, InterruptedException {
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("val1"));
futures.add(this.template.sendDefault("val2"));
futures.add(this.template.sendDefault("val3"));
for (ListenableFuture<SendResult<String, String>> future : futures) {
for (var future : futures) {
future.get();
}
@@ -295,13 +294,13 @@ class KafkaItemReaderTests {
@Test
void testReadFromSinglePartitionAfterRestart() throws ExecutionException, InterruptedException {
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("val1"));
futures.add(this.template.sendDefault("val2"));
futures.add(this.template.sendDefault("val3"));
futures.add(this.template.sendDefault("val4"));
for (ListenableFuture<SendResult<String, String>> future : futures) {
for (var future : futures) {
future.get();
}
ExecutionContext executionContext = new ExecutionContext();
@@ -331,7 +330,7 @@ class KafkaItemReaderTests {
@Test
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, "val2"));
futures.add(this.template.send("topic4", 0, null, "val4"));
@@ -341,7 +340,7 @@ class KafkaItemReaderTests {
futures.add(this.template.send("topic4", 1, null, "val5"));
futures.add(this.template.send("topic4", 1, null, "val7"));
for (ListenableFuture<?> future : futures) {
for (var future : futures) {
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");
* 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.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
@@ -29,7 +30,6 @@ import org.springframework.batch.item.Chunk;
import org.springframework.core.convert.converter.Converter;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -46,7 +46,7 @@ class KafkaItemWriterTests {
private KafkaTemplate<String, String> kafkaTemplate;
@Mock
private ListenableFuture<SendResult<String, String>> future;
private CompletableFuture<SendResult<String, String>> future;
private KafkaItemKeyMapper itemKeyMapper;