Skip to content

Return AbstractRedisClient from LettuceConnectionFactory #2013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.5.0-SNAPSHOT</version>
<version>2.5.0-GH-1745-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
* <p/>
* 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.
* <p/>
* 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down