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 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 {