Skip to content

Reinstate starting RedisConnectionFactory through afterPropertiesSet #2636

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>3.2.0-SNAPSHOT</version>
<version>3.2.x-2635-SNAPSHOT</version>

<name>Spring Data Redis</name>
<description>Spring Data module for Redis</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ enum State {
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).
*/
Expand Down Expand Up @@ -262,7 +264,11 @@ public JedisConnectionFactory(RedisClusterConfiguration clusterConfig, JedisClie

@Override
public void afterPropertiesSet() {

clientConfig = createClientConfig(getDatabase(), getRedisUsername(), getRedisPassword());
if(isAutoStartup()) {
start();
}
}

JedisClientConfig createSentinelClientConfig(SentinelConfiguration sentinelConfiguration) {
Expand Down Expand Up @@ -853,6 +859,21 @@ public boolean isRedisClusterAware() {
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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -400,7 +402,9 @@ public boolean isRunning() {

@Override
public void afterPropertiesSet() {
// customization hook. initialization happens in start
if(isAutoStartup()) {
start();
}
}

@Override
Expand Down Expand Up @@ -1097,6 +1101,21 @@ public boolean isClusterAware() {
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,13 @@ void getConnectionShouldFailIfNotInitialized() {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1227,14 +1227,13 @@ void createFullRedisSentinelConfiguration() {
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 {
Expand Down