diff --git a/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java b/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java index 0cd3ff04e5..33c7bbd1fd 100644 --- a/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java +++ b/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java @@ -21,7 +21,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; @@ -48,7 +47,7 @@ /** * Services pertinent to the interactive query capabilities of Kafka Streams. This class * provides services such as querying for a particular store, which instance is hosting a - * particular store etc. This is part of the public API of the kafka streams binder, and + * particular store etc. This is part of the public API of the Kafka Streams binder, and * the users can inject this service in their applications to make use of it. * * @author Soby Chacko @@ -57,6 +56,7 @@ * @author Nico Pommerening * @author Chris Bono * @author Artem Bilan + * @author Baljit Singh * @since 2.1.0 */ public class InteractiveQueryService { @@ -69,18 +69,26 @@ public class InteractiveQueryService { private final KafkaStreamsVersionAgnosticTopologyInfoFacade topologyInfoFacade; + private final RetryTemplate retryTemplate; + private StoreQueryParametersCustomizer storeQueryParametersCustomizer; /** * Constructor for InteractiveQueryService. * @param kafkaStreamsRegistry holding {@link KafkaStreamsRegistry} - * @param binderConfigurationProperties kafka Streams binder configuration properties + * @param binderConfigurationProperties Kafka Streams binder configuration properties */ public InteractiveQueryService(KafkaStreamsRegistry kafkaStreamsRegistry, KafkaStreamsBinderConfigurationProperties binderConfigurationProperties) { this.kafkaStreamsRegistry = kafkaStreamsRegistry; this.binderConfigurationProperties = binderConfigurationProperties; this.topologyInfoFacade = new KafkaStreamsVersionAgnosticTopologyInfoFacade(); + + var stateStoreRetry = this.binderConfigurationProperties.getStateStoreRetry(); + this.retryTemplate = new RetryTemplate(RetryPolicy.builder() + .maxRetries(stateStoreRetry.getMaxAttempts()) + .delay(Duration.ofMillis(stateStoreRetry.getBackoffPeriod())) + .build()); } /** @@ -103,7 +111,7 @@ public T getQueryableStore(String storeName, QueryableStoreType storeType AtomicReference> storeQueryParametersAtomicReference = new AtomicReference<>(storeQueryParams); try { - return getRetryTemplate().execute(() -> { + return retryTemplate.execute(() -> { T store = null; Throwable throwable = null; if (contextSpecificKafkaStreams != null) { @@ -187,7 +195,7 @@ private boolean filterByThreadName(KafkaStreams streams) { } /** - * Gets the current {@link HostInfo} that the calling kafka streams application is + * Gets the current {@link HostInfo} that the calling Kafka Streams application is * running on. * * Note that the end user applications must provide `application.server` as a @@ -224,16 +232,14 @@ public HostInfo getCurrentHostInfo() { * @return the {@link HostInfo} where the key for the provided store is hosted currently */ public HostInfo getHostInfo(String store, K key, Serializer serializer) { - final RetryTemplate retryTemplate = getRetryTemplate(); - try { return retryTemplate.execute(() -> { Throwable throwable = null; try { final KeyQueryMetadata keyQueryMetadata = this.kafkaStreamsRegistry.getKafkaStreams() .stream() - .map((k) -> Optional.ofNullable(k.queryMetadataForKey(store, key, serializer))) - .filter(Optional::isPresent).map(Optional::get).findFirst().orElse(null); + .map(k -> k.queryMetadataForKey(store, key, serializer)) + .filter(Objects::nonNull).findFirst().orElse(null); if (keyQueryMetadata != null) { return keyQueryMetadata.activeHost(); } @@ -251,17 +257,6 @@ public HostInfo getHostInfo(String store, K key, Serializer serializer) { } } - private RetryTemplate getRetryTemplate() { - - var stateStoreRetry = this.binderConfigurationProperties.getStateStoreRetry(); - RetryPolicy retryPolicy = RetryPolicy.builder() - .maxRetries(stateStoreRetry.getMaxAttempts()) - .delay(Duration.ofMillis(stateStoreRetry.getBackoffPeriod())) - .build(); - - return new RetryTemplate(retryPolicy); - } - /** * Retrieves and returns the {@link KeyQueryMetadata} associated with the given combination of * key and state store. If none found, it will return null. @@ -275,8 +270,8 @@ private RetryTemplate getRetryTemplate() { public KeyQueryMetadata getKeyQueryMetadata(String store, K key, Serializer serializer) { return this.kafkaStreamsRegistry.getKafkaStreams() .stream() - .map((k) -> Optional.ofNullable(k.queryMetadataForKey(store, key, serializer))) - .filter(Optional::isPresent).map(Optional::get).findFirst().orElse(null); + .map(k -> k.queryMetadataForKey(store, key, serializer)) + .filter(Objects::nonNull).findFirst().orElse(null); } /**