From 3f0ef9c5a7d8c0e12ed0a5e21b8bc026711a2433 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 23 Mar 2021 10:17:59 +0100 Subject: [PATCH 1/2] Prepare issue branch. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 74369f5ab8..4607c461e0 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-redis - 2.5.0-SNAPSHOT + 2.5.0-GH-1745-SNAPSHOT Spring Data Redis From 7155c86501381666e5cc6aaea9124ee4e135c99e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 23 Mar 2021 10:23:31 +0100 Subject: [PATCH 2/2] Return AbstractRedisClient from LettuceConnectionFactory. Closes #1745 --- .../lettuce/LettuceConnectionFactory.java | 41 ++++++++++++++++++- .../LettuceConnectionFactoryUnitTests.java | 25 +++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java index 8c4c67f1c4..6fd70119e9 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java @@ -44,6 +44,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.dao.DataAccessException; @@ -56,7 +57,6 @@ import org.springframework.data.redis.connection.RedisConfiguration.DomainSocketConfiguration; import org.springframework.data.redis.connection.RedisConfiguration.WithDatabaseIndex; import org.springframework.data.redis.connection.RedisConfiguration.WithPassword; -import org.springframework.data.redis.connection.lettuce.LettuceConnection.*; import org.springframework.data.util.Optionals; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -788,6 +788,45 @@ public void setClientName(@Nullable String clientName) { this.getMutableConfiguration().setClientName(clientName); } + /** + * Returns the native {@link AbstractRedisClient} used by this instance. The client is initialized as part of + * {@link #afterPropertiesSet() the bean initialization lifecycle} and only available when this connection factory is + * initialized. + *

+ * Depending on the configuration, the client can be either {@link RedisClient} or {@link RedisClusterClient}. + * + * @return the native {@link AbstractRedisClient}. Can be {@literal null} if not initialized. + * @since 2.5 + * @see #afterPropertiesSet() + */ + @Nullable + public AbstractRedisClient getNativeClient() { + return this.client; + } + + /** + * Returns the native {@link AbstractRedisClient} used by this instance. The client is initialized as part of + * {@link #afterPropertiesSet() the bean initialization lifecycle} and only available when this connection factory is + * initialized. Throws {@link IllegalStateException} if not yet initialized. + *

+ * Depending on the configuration, the client can be either {@link RedisClient} or {@link RedisClusterClient}. + * + * @return the native {@link AbstractRedisClient}. + * @since 2.5 + * @throws IllegalStateException if not yet initialized. + * @see #getNativeClient() + */ + public AbstractRedisClient getRequiredNativeClient() { + + AbstractRedisClient client = getNativeClient(); + + if (client == null) { + throw new IllegalStateException("Client not yet initialized"); + } + + return client; + } + @Nullable private String getRedisUsername() { return RedisConfiguration.getUsernameOrElse(configuration, standaloneConfig::getUsername); diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactoryUnitTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactoryUnitTests.java index f56aca7d49..bcdafa7134 100644 --- a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactoryUnitTests.java +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactoryUnitTests.java @@ -989,6 +989,31 @@ protected LettuceConnectionProvider doCreateConnectionProvider(AbstractRedisClie assertThat(reactiveConnection).isInstanceOf(LettuceReactiveRedisClusterConnection.class); } + @Test // GH-1745 + void getNativeClientShouldReturnClient() { + + LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(); + connectionFactory.setClientResources(getSharedClientResources()); + connectionFactory.afterPropertiesSet(); + + assertThat(connectionFactory.getNativeClient()).isInstanceOf(RedisClient.class); + + connectionFactory = new LettuceConnectionFactory(clusterConfig); + connectionFactory.setClientResources(getSharedClientResources()); + connectionFactory.afterPropertiesSet(); + + assertThat(connectionFactory.getRequiredNativeClient()).isInstanceOf(RedisClusterClient.class); + } + + @Test // GH-1745 + void getNativeClientShouldFailIfNotInitialized() { + + LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(); + + assertThatIllegalStateException().isThrownBy(connectionFactory::getRequiredNativeClient) + .withMessage("Client not yet initialized"); + } + @Data @AllArgsConstructor static class CustomRedisConfiguration implements RedisConfiguration, WithHostAndPort {