Skip to content

Replace ListenableFuture with CompletableFuture #4165

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,10 +21,10 @@
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;

/**
Expand All @@ -42,32 +42,32 @@ 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);
}
else {
future.get();
}
}
this.listenableFutures.clear();
this.completableFutures.clear();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -38,10 +39,8 @@
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.rule.EmbeddedKafkaRule;
import org.springframework.kafka.test.utils.KafkaTestUtils;
import org.springframework.util.concurrent.ListenableFuture;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
Expand Down Expand Up @@ -187,12 +186,12 @@ public void testValidation() {
@Test
public 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();
}

Expand Down Expand Up @@ -221,12 +220,12 @@ public void testReadFromSinglePartition() throws ExecutionException, Interrupted
@Test
public 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();
}

Expand Down Expand Up @@ -257,10 +256,10 @@ public void testReadFromSinglePartitionFromTheOffsetStoredInKafka() throws Excep
// 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);
Expand Down Expand Up @@ -311,12 +310,12 @@ public void testReadFromSinglePartitionFromTheOffsetStoredInKafka() throws Excep
@Test
public 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();
}

Expand All @@ -339,13 +338,13 @@ public void testReadFromMultiplePartitions() throws ExecutionException, Interrup
@Test
public 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();
Expand Down Expand Up @@ -375,7 +374,7 @@ public void testReadFromSinglePartitionAfterRestart() throws ExecutionException,

@Test
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, "val2"));
futures.add(this.template.send("topic4", 0, null, "val4"));
Expand All @@ -385,7 +384,7 @@ public void testReadFromMultiplePartitionsAfterRestart() throws ExecutionExcepti
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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,6 +17,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.junit.Before;
Expand All @@ -28,7 +29,6 @@
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.Assert.assertEquals;
import static org.junit.Assert.fail;
Expand All @@ -46,7 +46,7 @@ public class KafkaItemWriterTests {
private KafkaTemplate<String, String> kafkaTemplate;

@Mock
private ListenableFuture<SendResult<String, String>> future;
private CompletableFuture<SendResult<String, String>> future;

private KafkaItemKeyMapper itemKeyMapper;

Expand Down