Skip to content

Commit 7b53e05

Browse files
committed
Add convenience method for converting Kafka future
1 parent 84dfe5a commit 7b53e05

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.aiven.guardian.kafka
2+
3+
import org.apache.kafka.common.KafkaFuture
4+
5+
import java.util.concurrent.CompletableFuture
6+
7+
object Utils {
8+
9+
// Taken from https://stackoverflow.com/a/56763206/1519631
10+
implicit final class KafkaFutureToCompletableFuture[T](kafkaFuture: KafkaFuture[T]) {
11+
@SuppressWarnings(Array("DisableSyntax.null"))
12+
def toCompletableFuture: CompletableFuture[T] = {
13+
val wrappingFuture = new CompletableFuture[T]
14+
kafkaFuture.whenComplete { (value, throwable) =>
15+
if (throwable != null)
16+
wrappingFuture.completeExceptionally(throwable)
17+
else
18+
wrappingFuture.complete(value)
19+
}
20+
wrappingFuture
21+
}
22+
}
23+
24+
}

0 commit comments

Comments
 (0)