Start RedisConnectionFactory through afterPropertiesSet.
This commit restores the behaviour of RedisConnectionFactories eager initialization via afterPropertiesSet. Additionally it is now possible to modify the lifecycle phase. Closes #2635 Original pull request: #2636
This commit is contained in:
committed by
Mark Paluch
parent
ce3c439a17
commit
d98afb9154
@@ -118,6 +118,8 @@ public class JedisConnectionFactory
|
||||
private @Nullable ClusterTopologyProvider topologyProvider;
|
||||
private @Nullable ClusterCommandExecutor clusterCommandExecutor;
|
||||
|
||||
private int phase = DEFAULT_PHASE - 10;
|
||||
|
||||
/**
|
||||
* Constructs a new {@link JedisConnectionFactory} instance with default settings (default connection pooling).
|
||||
*/
|
||||
@@ -262,7 +264,11 @@ public class JedisConnectionFactory
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
|
||||
clientConfig = createClientConfig(getDatabase(), getRedisUsername(), getRedisPassword());
|
||||
if(isAutoStartup()) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
JedisClientConfig createSentinelClientConfig(SentinelConfiguration sentinelConfiguration) {
|
||||
@@ -853,6 +859,21 @@ public class JedisConnectionFactory
|
||||
return RedisConfiguration.isClusterConfiguration(configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPhase() {
|
||||
return phase;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the lifecycle phase for pausing and resuming this executor.
|
||||
* The default is {@link #DEFAULT_PHASE} - 10.
|
||||
* @since 3.2
|
||||
* @see SmartLifecycle#getPhase()
|
||||
*/
|
||||
public void setPhase(int phase) {
|
||||
this.phase = phase;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSentinelConnection getSentinelConnection() {
|
||||
|
||||
|
||||
@@ -128,6 +128,8 @@ public class LettuceConnectionFactory implements RedisConnectionFactory, Reactiv
|
||||
|
||||
private PipeliningFlushPolicy pipeliningFlushPolicy = PipeliningFlushPolicy.flushEachCommand();
|
||||
|
||||
private int phase = DEFAULT_PHASE - 10;
|
||||
|
||||
/**
|
||||
* Lifecycle state of this factory.
|
||||
*/
|
||||
@@ -400,7 +402,9 @@ public class LettuceConnectionFactory implements RedisConnectionFactory, Reactiv
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
// customization hook. initialization happens in start
|
||||
if(isAutoStartup()) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1097,6 +1101,21 @@ public class LettuceConnectionFactory implements RedisConnectionFactory, Reactiv
|
||||
return RedisConfiguration.isClusterConfiguration(configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPhase() {
|
||||
return phase;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the lifecycle phase for pausing and resuming this executor.
|
||||
* The default is {@link #DEFAULT_PHASE} - 10.
|
||||
* @since 3.2
|
||||
* @see SmartLifecycle#getPhase()
|
||||
*/
|
||||
public void setPhase(int phase) {
|
||||
this.phase = phase;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the shared connection using {@literal byte[]} encoding for imperative API use. {@literal null} if
|
||||
* {@link #getShareNativeConnection() connection sharing} is disabled or when connected to Redis Cluster.
|
||||
|
||||
@@ -326,14 +326,13 @@ class JedisConnectionFactoryUnitTests {
|
||||
assertThatIllegalStateException().isThrownBy(connectionFactory::getSentinelConnection);
|
||||
}
|
||||
|
||||
@Test // GH-2503
|
||||
void afterPropertiesSetDoesNotTriggerConnectionInitialization() {
|
||||
@Test // GH-2503, GH-2635
|
||||
void afterPropertiesTriggersConnectionInitialization() {
|
||||
|
||||
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
|
||||
connectionFactory.afterPropertiesSet();
|
||||
|
||||
assertThat(connectionFactory.isRunning()).isFalse();
|
||||
assertThatIllegalStateException().isThrownBy(() -> connectionFactory.getConnection());
|
||||
assertThat(connectionFactory.isRunning()).isTrue();
|
||||
}
|
||||
|
||||
private JedisConnectionFactory initSpyedConnectionFactory(RedisSentinelConfiguration sentinelConfig,
|
||||
|
||||
@@ -1227,14 +1227,13 @@ class LettuceConnectionFactoryUnitTests {
|
||||
assertThat(configuration).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test // GH-2503
|
||||
void afterPropertiesSetDoesNotTriggerConnectionInitialization() {
|
||||
@Test // GH-2503, GH-2635
|
||||
void afterPropertiesSetTriggersConnectionInitialization() {
|
||||
|
||||
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
|
||||
connectionFactory.afterPropertiesSet();
|
||||
|
||||
assertThat(connectionFactory.isRunning()).isFalse();
|
||||
assertThatIllegalStateException().isThrownBy(() -> connectionFactory.getConnection());
|
||||
assertThat(connectionFactory.isRunning()).isTrue();
|
||||
}
|
||||
|
||||
static class CustomRedisConfiguration implements RedisConfiguration, WithHostAndPort {
|
||||
|
||||
Reference in New Issue
Block a user