DATAREDIS-930 - Return port/host of WithHostAndPort configurations using LettuceConnectionFactory.getPort() and getHost().
We now correctly return host and port from configurations that implement WithHostAndPort instead of always using RedisStandaloneConfiguration. Original pull request: #385.
This commit is contained in:
committed by
Mark Paluch
parent
ff9802ad3d
commit
34951495ba
@@ -18,6 +18,7 @@ package org.springframework.data.redis.connection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.IntSupplier;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -29,6 +30,7 @@ import org.springframework.util.Assert;
|
||||
* configurations for the individual purposes.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Luis De Bello
|
||||
* @since 2.1
|
||||
*/
|
||||
public interface RedisConfiguration {
|
||||
@@ -85,6 +87,14 @@ public interface RedisConfiguration {
|
||||
return configuration instanceof SentinelConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param configuration can be {@literal null}.
|
||||
* @return {@code true} if given {@link RedisConfiguration} is instance of {@link WithHostAndPort}.
|
||||
*/
|
||||
static boolean isHostAndPortAware(@Nullable RedisConfiguration configuration) {
|
||||
return configuration instanceof WithHostAndPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param configuration can be {@literal null}.
|
||||
* @return {@code true} if given {@link RedisConfiguration} is instance of {@link ClusterConfiguration}.
|
||||
@@ -135,6 +145,32 @@ public interface RedisConfiguration {
|
||||
return isPasswordAware(configuration) ? ((WithPassword) configuration).getPassword() : other.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param configuration can be {@literal null}.
|
||||
* @param other a {@code Supplier} whose result is returned if given {@link RedisConfiguration} is not
|
||||
* {@link #isHostAndPortAware(RedisConfiguration) port aware}.
|
||||
* @return never {@literal null}.
|
||||
* @throws IllegalArgumentException if {@code other} is {@literal null}.
|
||||
*/
|
||||
static int getPortOrElse(@Nullable RedisConfiguration configuration, IntSupplier other) {
|
||||
|
||||
Assert.notNull(other, "Other must not be null!");
|
||||
return isHostAndPortAware(configuration) ? ((WithHostAndPort) configuration).getPort() : other.getAsInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param configuration can be {@literal null}.
|
||||
* @param other a {@code Supplier} whose result is returned if given {@link RedisConfiguration} is not
|
||||
* {@link #isHostAndPortAware(RedisConfiguration) host aware}.
|
||||
* @return never {@literal null}.
|
||||
* @throws IllegalArgumentException if {@code other} is {@literal null}.
|
||||
*/
|
||||
static String getHostOrElse(@Nullable RedisConfiguration configuration, Supplier<String> other) {
|
||||
|
||||
Assert.notNull(other, "Other must not be null!");
|
||||
return isHostAndPortAware(configuration) ? ((WithHostAndPort) configuration).getHostName() : other.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link RedisConfiguration} part suitable for configurations that may use authentication when connecting.
|
||||
*
|
||||
|
||||
@@ -86,6 +86,7 @@ import org.springframework.util.ClassUtils;
|
||||
* @author Mark Paluch
|
||||
* @author Balázs Németh
|
||||
* @author Ruben Cervilla
|
||||
* @author Luis De Bello
|
||||
*/
|
||||
public class LettuceConnectionFactory
|
||||
implements InitializingBean, DisposableBean, RedisConnectionFactory, ReactiveRedisConnectionFactory {
|
||||
@@ -472,7 +473,7 @@ public class LettuceConnectionFactory
|
||||
* @return the host.
|
||||
*/
|
||||
public String getHostName() {
|
||||
return standaloneConfig.getHostName();
|
||||
return RedisConfiguration.getHostOrElse(configuration, standaloneConfig::getHostName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -492,7 +493,7 @@ public class LettuceConnectionFactory
|
||||
* @return the port.
|
||||
*/
|
||||
public int getPort() {
|
||||
return standaloneConfig.getPort();
|
||||
return RedisConfiguration.getPortOrElse(configuration, standaloneConfig::getPort);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user