-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
179 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/de/nerden/kafka/streams/processor/AsyncTransformerSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package de.nerden.kafka.streams.processor; | ||
|
||
import de.nerden.kafka.streams.processor.AsyncTransformer.AsyncMessage; | ||
import de.nerden.kafka.streams.serde.AsyncMessageSerde; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import org.apache.kafka.common.serialization.Serde; | ||
import org.apache.kafka.common.serialization.Serdes; | ||
import org.apache.kafka.streams.KeyValue; | ||
import org.apache.kafka.streams.kstream.Transformer; | ||
import org.apache.kafka.streams.kstream.TransformerSupplier; | ||
import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier; | ||
import org.apache.kafka.streams.state.KeyValueStore; | ||
import org.apache.kafka.streams.state.StoreBuilder; | ||
import org.apache.kafka.streams.state.Stores; | ||
|
||
public class AsyncTransformerSupplier<K, V> implements TransformerSupplier<K, V, KeyValue<K, V>> { | ||
|
||
|
||
private final String storeName; | ||
private final Serde<K> keySerde; | ||
private final Serde<V> valueSerde; | ||
private final KeyValueBytesStoreSupplier storeSupplier; | ||
private final boolean changeLoggingEnabled; | ||
private final int maxInflight; | ||
private final int timeoutMs; | ||
|
||
private final Function<KeyValue<K, V>, CompletableFuture<KeyValue<K, V>>> fn; | ||
private final Predicate<AsyncMessage<K, V>> retryDecider; | ||
|
||
public AsyncTransformerSupplier( | ||
String storeName, | ||
Serde<K> keySerde, | ||
Serde<V> valueSerde, | ||
KeyValueBytesStoreSupplier storeSupplier, | ||
boolean changeLoggingEnabled, | ||
int maxInflight, | ||
int timeoutMs, | ||
Function<KeyValue<K, V>, CompletableFuture<KeyValue<K, V>>> fn, | ||
Predicate<AsyncMessage<K, V>> retryDecider) { | ||
this.storeName = storeName; | ||
this.keySerde = keySerde; | ||
this.valueSerde = valueSerde; | ||
this.storeSupplier = storeSupplier; | ||
this.changeLoggingEnabled = changeLoggingEnabled; | ||
this.maxInflight = maxInflight; | ||
this.timeoutMs = timeoutMs; | ||
this.fn = fn; | ||
this.retryDecider = retryDecider; | ||
} | ||
|
||
@Override | ||
public Transformer<K, V, KeyValue<K, V>> get() { | ||
return new AsyncTransformer<>(fn, retryDecider, this.storeName, this.maxInflight, | ||
this.timeoutMs); | ||
} | ||
|
||
@Override | ||
public Set<StoreBuilder<?>> stores() { | ||
StoreBuilder<KeyValueStore<Long, AsyncMessage<K, V>>> builder; | ||
if (this.storeSupplier != null) { | ||
builder = | ||
Stores.keyValueStoreBuilder( | ||
this.storeSupplier, Serdes.Long(), | ||
new AsyncMessageSerde<>(this.keySerde, this.valueSerde)); | ||
} else { | ||
builder = | ||
Stores.keyValueStoreBuilder( | ||
Stores.inMemoryKeyValueStore(storeName), | ||
Serdes.Long(), | ||
new AsyncMessageSerde<>(this.keySerde, this.valueSerde)); | ||
} | ||
|
||
return Collections.singleton( | ||
changeLoggingEnabled | ||
? builder.withLoggingEnabled(Map.of()) | ||
: builder.withLoggingDisabled()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters