Skip to content

Commit 5059887

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-576 - Support client name for Redis connections using Lettuce.
We now allow configuration of the client name that is applied to connections using the Lettuce driver. LettuceClientConfiguration configuration = LettuceClientConfiguration.builder().clientName("foo-bar").build(); LettuceConnectionFactory factory = new LettuceConnectionFactory(new RedisStandaloneConfiguration(), configuration); Fixed some typos, javadoc and method visibility along the way. Original Pull Request: #285
1 parent 1d528f0 commit 5059887

9 files changed

+171
-19
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisClientConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* <li>Optional {@link HostnameVerifier}</li>
4343
* <li>Whether to use connection-pooling</li>
4444
* <li>Optional {@link GenericObjectPoolConfig}</li>
45+
* <li>Optional client name</li>
4546
* <li>Connect {@link Duration timeout}</li>
4647
* <li>Read {@link Duration timeout}</li>
4748
* </ul>

src/main/java/org/springframework/data/redis/connection/lettuce/DefaultLettuceClientConfiguration.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,71 +37,89 @@ class DefaultLettuceClientConfiguration implements LettuceClientConfiguration {
3737
private final boolean startTls;
3838
private final Optional<ClientResources> clientResources;
3939
private final Optional<ClientOptions> clientOptions;
40+
private final Optional<String> clientName;
4041
private final Duration timeout;
4142
private final Duration shutdownTimeout;
4243

4344
DefaultLettuceClientConfiguration(boolean useSsl, boolean verifyPeer, boolean startTls,
44-
@Nullable ClientResources clientResources, @Nullable ClientOptions clientOptions, Duration timeout,
45-
Duration shutdownTimeout) {
45+
@Nullable ClientResources clientResources, @Nullable ClientOptions clientOptions, @Nullable String clientName,
46+
Duration timeout, Duration shutdownTimeout) {
4647

4748
this.useSsl = useSsl;
4849
this.verifyPeer = verifyPeer;
4950
this.startTls = startTls;
5051
this.clientResources = Optional.ofNullable(clientResources);
5152
this.clientOptions = Optional.ofNullable(clientOptions);
53+
this.clientName = Optional.ofNullable(clientName);
5254
this.timeout = timeout;
5355
this.shutdownTimeout = shutdownTimeout;
5456
}
5557

56-
/* (non-Javadoc)
58+
/*
59+
* (non-Javadoc)
5760
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#useSsl()
5861
*/
5962
@Override
6063
public boolean isUseSsl() {
6164
return useSsl;
6265
}
6366

64-
/* (non-Javadoc)
67+
/*
68+
* (non-Javadoc)
6569
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#isVerifyPeer()
6670
*/
6771
@Override
6872
public boolean isVerifyPeer() {
6973
return verifyPeer;
7074
}
7175

72-
/* (non-Javadoc)
76+
/*
77+
* (non-Javadoc)
7378
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#isStartTls()
7479
*/
7580
@Override
7681
public boolean isStartTls() {
7782
return startTls;
7883
}
7984

80-
/* (non-Javadoc)
85+
/*
86+
* (non-Javadoc)
8187
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientResources()
8288
*/
8389
@Override
8490
public Optional<ClientResources> getClientResources() {
8591
return clientResources;
8692
}
8793

88-
/* (non-Javadoc)
94+
/*
95+
* (non-Javadoc)
8996
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientOptions()
9097
*/
9198
@Override
9299
public Optional<ClientOptions> getClientOptions() {
93100
return clientOptions;
94101
}
95102

96-
/* (non-Javadoc)
103+
/*
104+
* (non-Javadoc)
105+
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientName()
106+
*/
107+
@Override
108+
public Optional<String> getClientName() {
109+
return clientName;
110+
}
111+
112+
/*
113+
* (non-Javadoc)
97114
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getTimeout()
98115
*/
99116
@Override
100117
public Duration getCommandTimeout() {
101118
return timeout;
102119
}
103120

104-
/* (non-Javadoc)
121+
/*
122+
* (non-Javadoc)
105123
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getShutdownTimeout()
106124
*/
107125
@Override

src/main/java/org/springframework/data/redis/connection/lettuce/DefaultLettucePoolingClientConfiguration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ public Optional<ClientOptions> getClientOptions() {
8787
return clientConfiguration.getClientOptions();
8888
}
8989

90+
/*
91+
* (non-Javadoc)
92+
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientName()
93+
*/
94+
@Override
95+
public Optional<String> getClientName() {
96+
return clientConfiguration.getClientName();
97+
}
98+
9099
/*
91100
* (non-Javadoc)
92101
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getCommandTimeout()

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceClientConfiguration.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* <li>Whether to use StartTLS</li>
3838
* <li>Optional {@link ClientResources}</li>
3939
* <li>Optional {@link ClientOptions}</li>
40+
* <li>Optional client name</li>
4041
* <li>Client {@link Duration timeout}</li>
4142
* <li>Shutdown {@link Duration timeout}</li>
4243
* </ul>
@@ -75,6 +76,12 @@ public interface LettuceClientConfiguration {
7576
*/
7677
Optional<ClientOptions> getClientOptions();
7778

79+
/**
80+
* @return the optional client name to be set with {@code CLIENT SETNAME}.
81+
* @since 2.1
82+
*/
83+
Optional<String> getClientName();
84+
7885
/**
7986
* @return the timeout.
8087
*/
@@ -109,6 +116,8 @@ static LettuceClientConfigurationBuilder builder() {
109116
* <dd>none</dd>
110117
* <dt>Client Resources</dt>
111118
* <dd>none</dd>
119+
* <dt>Client name</dt>
120+
* <dd>none</dd>
112121
* <dt>Connect Timeout</dt>
113122
* <dd>60 Seconds</dd>
114123
* <dt>Shutdown Timeout</dt>
@@ -132,6 +141,7 @@ class LettuceClientConfigurationBuilder {
132141
boolean startTls;
133142
@Nullable ClientResources clientResources;
134143
@Nullable ClientOptions clientOptions;
144+
@Nullable String clientName;
135145
Duration timeout = Duration.ofSeconds(RedisURI.DEFAULT_TIMEOUT);
136146
Duration shutdownTimeout = Duration.ofMillis(100);
137147

@@ -178,6 +188,21 @@ public LettuceClientConfigurationBuilder clientOptions(ClientOptions clientOptio
178188
return this;
179189
}
180190

191+
/**
192+
* Configure a {@code clientName} to be set with {@code CLIENT SETNAME}.
193+
*
194+
* @param clientName must not be {@literal null} or empty.
195+
* @return {@literal this} builder.
196+
* @throws IllegalArgumentException if clientName is {@literal null}.
197+
*/
198+
public LettuceClientConfigurationBuilder clientName(String clientName) {
199+
200+
Assert.hasText(clientName, "Client name must not be null or empty!");
201+
202+
this.clientName = clientName;
203+
return this;
204+
}
205+
181206
/**
182207
* Configure a command timeout.
183208
*
@@ -216,7 +241,7 @@ public LettuceClientConfigurationBuilder shutdownTimeout(Duration shutdownTimeou
216241
public LettuceClientConfiguration build() {
217242

218243
return new DefaultLettuceClientConfiguration(useSsl, verifyPeer, startTls, clientResources, clientOptions,
219-
timeout, shutdownTimeout);
244+
clientName, timeout, shutdownTimeout);
220245
}
221246
}
222247

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,30 @@ public void setDatabase(int index) {
586586
standaloneConfig.setDatabase(index);
587587
}
588588

589+
/**
590+
* Returns the client name.
591+
*
592+
* @return the client name.
593+
* @since 2.1
594+
*/
595+
@Nullable
596+
public String getClientName() {
597+
return clientConfiguration.getClientName().orElse(null);
598+
}
599+
600+
/**
601+
* Sets the client name used by this connection factory. Defaults to none which does not set a client name.
602+
*
603+
* @param clientName the client name.
604+
* @since 2.1
605+
* @deprecated configure the client name using {@link LettuceClientConfiguration}.
606+
* @throws IllegalStateException if {@link JedisClientConfiguration} is immutable.
607+
*/
608+
@Deprecated
609+
public void setClientName(String clientName) {
610+
this.getMutableConfiguration().setClientName(clientName);
611+
}
612+
589613
/**
590614
* Returns the password used for authenticating with the Redis server.
591615
*
@@ -849,6 +873,7 @@ private RedisURI getSentinelRedisURI() {
849873
RedisURI redisUri = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);
850874

851875
getRedisPassword().toOptional().ifPresent(redisUri::setPassword);
876+
clientConfiguration.getClientName().ifPresent(redisUri::setClientName);
852877
redisUri.setTimeout(clientConfiguration.getCommandTimeout());
853878

854879
return redisUri;
@@ -859,6 +884,7 @@ private RedisURI createRedisURIAndApplySettings(String host, int port) {
859884
RedisURI.Builder builder = RedisURI.Builder.redis(host, port);
860885

861886
getRedisPassword().toOptional().ifPresent(builder::withPassword);
887+
clientConfiguration.getClientName().ifPresent(builder::withClientName);
862888

863889
builder.withSsl(clientConfiguration.isUseSsl());
864890
builder.withVerifyPeer(clientConfiguration.isVerifyPeer());
@@ -897,6 +923,7 @@ static class MutableLettuceClientConfiguration implements LettuceClientConfigura
897923
private boolean verifyPeer = true;
898924
private boolean startTls;
899925
private @Nullable ClientResources clientResources;
926+
private @Nullable String clientName;
900927
private Duration timeout = Duration.ofSeconds(RedisURI.DEFAULT_TIMEOUT);
901928
private Duration shutdownTimeout = Duration.ofMillis(100);
902929

@@ -908,7 +935,7 @@ public boolean isUseSsl() {
908935
return useSsl;
909936
}
910937

911-
public void setUseSsl(boolean useSsl) {
938+
void setUseSsl(boolean useSsl) {
912939
this.useSsl = useSsl;
913940
}
914941

@@ -920,7 +947,7 @@ public boolean isVerifyPeer() {
920947
return verifyPeer;
921948
}
922949

923-
public void setVerifyPeer(boolean verifyPeer) {
950+
void setVerifyPeer(boolean verifyPeer) {
924951
this.verifyPeer = verifyPeer;
925952
}
926953

@@ -932,7 +959,7 @@ public boolean isStartTls() {
932959
return startTls;
933960
}
934961

935-
public void setStartTls(boolean startTls) {
962+
void setStartTls(boolean startTls) {
936963
this.startTls = startTls;
937964
}
938965

@@ -944,7 +971,7 @@ public Optional<ClientResources> getClientResources() {
944971
return Optional.ofNullable(clientResources);
945972
}
946973

947-
public void setClientResources(ClientResources clientResources) {
974+
void setClientResources(ClientResources clientResources) {
948975
this.clientResources = clientResources;
949976
}
950977

@@ -956,6 +983,19 @@ public Optional<ClientOptions> getClientOptions() {
956983
return Optional.empty();
957984
}
958985

986+
/*
987+
* (non-Javadoc)
988+
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientName()
989+
*/
990+
@Override
991+
public Optional<String> getClientName() {
992+
return Optional.ofNullable(clientName);
993+
}
994+
995+
void setClientName(String clientName) {
996+
this.clientName = clientName;
997+
}
998+
959999
/* (non-Javadoc)
9601000
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getTimeout()
9611001
*/
@@ -964,7 +1004,7 @@ public Duration getCommandTimeout() {
9641004
return timeout;
9651005
}
9661006

967-
public void setTimeout(Duration timeout) {
1007+
void setTimeout(Duration timeout) {
9681008
this.timeout = timeout;
9691009
}
9701010

@@ -976,7 +1016,7 @@ public Duration getShutdownTimeout() {
9761016
return shutdownTimeout;
9771017
}
9781018

979-
public void setShutdownTimeout(Duration shutdownTimeout) {
1019+
void setShutdownTimeout(Duration shutdownTimeout) {
9801020
this.shutdownTimeout = shutdownTimeout;
9811021
}
9821022
}

src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionFactorySentinelIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void tearDown() {
4848
}
4949

5050
@Test // DATAREDIS-574
51-
public void shouldInitiaizeWithSentinelConfiguration() {
51+
public void shouldInitializeWithSentinelConfiguration() {
5252

5353
JedisClientConfiguration clientConfiguration = JedisClientConfiguration.builder() //
5454
.clientName("clientName") //

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceClientConfigurationUnitTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
public class LettuceClientConfigurationUnitTests {
3434

35-
@Test // DATAREDIS-574, DATAREDIS-667
35+
@Test // DATAREDIS-574, DATAREDIS-576, DATAREDIS-667
3636
public void shouldCreateEmptyConfiguration() {
3737

3838
LettuceClientConfiguration configuration = LettuceClientConfiguration.defaultConfiguration();
@@ -42,11 +42,12 @@ public void shouldCreateEmptyConfiguration() {
4242
assertThat(configuration.isStartTls()).isFalse();
4343
assertThat(configuration.getClientOptions()).isEmpty();
4444
assertThat(configuration.getClientResources()).isEmpty();
45+
assertThat(configuration.getClientName()).isEmpty();
4546
assertThat(configuration.getCommandTimeout()).isEqualTo(Duration.ofSeconds(60));
4647
assertThat(configuration.getShutdownTimeout()).isEqualTo(Duration.ofMillis(100));
4748
}
4849

49-
@Test // DATAREDIS-574, DATAREDIS-667
50+
@Test // DATAREDIS-574, DATAREDIS-576, DATAREDIS-667
5051
public void shouldConfigureAllProperties() {
5152

5253
ClientOptions clientOptions = ClientOptions.create();
@@ -58,6 +59,7 @@ public void shouldConfigureAllProperties() {
5859
.startTls().and() //
5960
.clientOptions(clientOptions) //
6061
.clientResources(sharedClientResources) //
62+
.clientName("foo") //
6163
.commandTimeout(Duration.ofMinutes(5)) //
6264
.shutdownTimeout(Duration.ofHours(2)) //
6365
.build();
@@ -67,6 +69,7 @@ public void shouldConfigureAllProperties() {
6769
assertThat(configuration.isStartTls()).isTrue();
6870
assertThat(configuration.getClientOptions()).contains(clientOptions);
6971
assertThat(configuration.getClientResources()).contains(sharedClientResources);
72+
assertThat(configuration.getClientName()).contains("foo");
7073
assertThat(configuration.getCommandTimeout()).isEqualTo(Duration.ofMinutes(5));
7174
assertThat(configuration.getShutdownTimeout()).isEqualTo(Duration.ofHours(2));
7275
}

0 commit comments

Comments
 (0)