Add timeout when flushing items to Kafka in KafkaItemWriter

Issue #3773
This commit is contained in:
Mahmoud Ben Hassine
2021-03-09 14:48:25 +01:00
parent 4ffc0827ce
commit 96beb18bf6
4 changed files with 52 additions and 7 deletions

View File

@@ -25,6 +25,7 @@ import org.springframework.util.concurrent.ListenableFuture;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* <p>
@@ -33,6 +34,7 @@ import java.util.List;
* </p>
*
* @author Mathieu Ouellet
* @author Mahmoud Ben Hassine
* @since 4.2
*
*/
@@ -40,6 +42,7 @@ 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 long timeout = -1;
@Override
protected void writeKeyValue(K key, T value) {
@@ -55,7 +58,12 @@ public class KafkaItemWriter<K, T> extends KeyValueItemWriter<K, T> {
protected void flush() throws Exception{
this.kafkaTemplate.flush();
for(ListenableFuture<SendResult<K,T>> future: this.listenableFutures){
future.get();
if (this.timeout >= 0) {
future.get(this.timeout, TimeUnit.MILLISECONDS);
}
else {
future.get();
}
}
this.listenableFutures.clear();
}
@@ -73,4 +81,15 @@ public class KafkaItemWriter<K, T> extends KeyValueItemWriter<K, T> {
public void setKafkaTemplate(KafkaTemplate<K, T> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}
/**
* The time limit to wait when flushing items to Kafka.
*
* @param timeout milliseconds to wait, defaults to -1 (no timeout).
* @since 4.2.6
*/
public void setTimeout(long timeout) {
this.timeout = timeout;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2019-2021 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.
@@ -25,6 +25,7 @@ import org.springframework.util.Assert;
* A builder implementation for the {@link KafkaItemWriter}
*
* @author Mathieu Ouellet
* @author Mahmoud Ben Hassine
* @since 4.2
*/
public class KafkaItemWriterBuilder<K, V> {
@@ -35,6 +36,8 @@ public class KafkaItemWriterBuilder<K, V> {
private boolean delete;
private long timeout = -1;
/**
* Establish the KafkaTemplate to be used by the KafkaItemWriter.
* @param kafkaTemplate the template to be used
@@ -71,6 +74,19 @@ public class KafkaItemWriterBuilder<K, V> {
return this;
}
/**
* The time limit to wait when flushing items to Kafka.
*
* @param timeout milliseconds to wait, defaults to -1 (no timeout).
* @return The current instance of the builder.
* @see KafkaItemWriter#setTimeout(long)
* @since 4.2.6
*/
public KafkaItemWriterBuilder<K, V> timeout(long timeout) {
this.timeout = timeout;
return this;
}
/**
* Validates and builds a {@link KafkaItemWriter}.
* @return a {@link KafkaItemWriter}
@@ -83,6 +99,7 @@ public class KafkaItemWriterBuilder<K, V> {
writer.setKafkaTemplate(this.kafkaTemplate);
writer.setItemKeyMapper(this.itemKeyMapper);
writer.setDelete(this.delete);
writer.setTimeout(this.timeout);
return writer;
}
}

View File

@@ -17,6 +17,7 @@ package org.springframework.batch.item.kafka;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
@@ -49,7 +50,7 @@ public class KafkaItemWriterTests {
@Before
public void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
MockitoAnnotations.initMocks(this);
when(this.kafkaTemplate.getDefaultTopic()).thenReturn("defaultTopic");
when(this.kafkaTemplate.sendDefault(any(), any())).thenReturn(this.future);
this.itemKeyMapper = new KafkaItemKeyMapper();
@@ -57,6 +58,7 @@ public class KafkaItemWriterTests {
this.writer.setKafkaTemplate(this.kafkaTemplate);
this.writer.setItemKeyMapper(this.itemKeyMapper);
this.writer.setDelete(false);
this.writer.setTimeout(10L);
this.writer.afterPropertiesSet();
}
@@ -99,7 +101,7 @@ public class KafkaItemWriterTests {
verify(this.kafkaTemplate).sendDefault(items.get(0), items.get(0));
verify(this.kafkaTemplate).sendDefault(items.get(1), items.get(1));
verify(this.kafkaTemplate).flush();
verify(this.future, times(2)).get();
verify(this.future, times(2)).get(10L, TimeUnit.MILLISECONDS);
}
@Test
@@ -112,7 +114,7 @@ public class KafkaItemWriterTests {
verify(this.kafkaTemplate).sendDefault(items.get(0), null);
verify(this.kafkaTemplate).sendDefault(items.get(1), null);
verify(this.kafkaTemplate).flush();
verify(this.future, times(2)).get();
verify(this.future, times(2)).get(10L, TimeUnit.MILLISECONDS);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2019-2021 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.
@@ -33,6 +33,7 @@ import static org.junit.Assert.assertTrue;
/**
* @author Mathieu Ouellet
* @author Mahmoud Ben Hassine
*/
public class KafkaItemWriterBuilderTests {
@@ -70,13 +71,19 @@ public class KafkaItemWriterBuilderTests {
public void testKafkaItemWriterBuild() {
// given
boolean delete = true;
long timeout = 10L;
// when
KafkaItemWriter<String, String> writer = new KafkaItemWriterBuilder<String, String>()
.kafkaTemplate(this.kafkaTemplate).itemKeyMapper(this.itemKeyMapper).delete(delete).build();
.kafkaTemplate(this.kafkaTemplate)
.itemKeyMapper(this.itemKeyMapper)
.delete(delete)
.timeout(timeout)
.build();
// then
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "delete"));
assertEquals(timeout, ReflectionTestUtils.getField(writer, "timeout"));
assertEquals(this.itemKeyMapper, ReflectionTestUtils.getField(writer, "itemKeyMapper"));
assertEquals(this.kafkaTemplate, ReflectionTestUtils.getField(writer, "kafkaTemplate"));
}