Skip to content

Commit 36f0f61

Browse files
committed
address review
1 parent 3eb7f98 commit 36f0f61

File tree

3 files changed

+45
-56
lines changed

3 files changed

+45
-56
lines changed

src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
public class DefaultJedisSocketFactory implements JedisSocketFactory {
1515

16-
private String host = Protocol.DEFAULT_HOST;
17-
private int port = Protocol.DEFAULT_PORT;
16+
protected static final HostAndPort DEFAULT_HOST_AND_PORT = new HostAndPort(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT);
17+
18+
private HostAndPort hostAndPort = DEFAULT_HOST_AND_PORT;
1819
private int connectionTimeout = Protocol.DEFAULT_TIMEOUT;
1920
private int soTimeout = Protocol.DEFAULT_TIMEOUT;
2021
private boolean ssl = false;
@@ -26,30 +27,33 @@ public class DefaultJedisSocketFactory implements JedisSocketFactory {
2627
public DefaultJedisSocketFactory() {
2728
}
2829

30+
public DefaultJedisSocketFactory(HostAndPort hostAndPort) {
31+
this(hostAndPort, null);
32+
}
33+
2934
@Deprecated
3035
public DefaultJedisSocketFactory(String host, int port, int connectionTimeout, int soTimeout,
3136
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
3237
HostnameVerifier hostnameVerifier) {
33-
setHost(host);
34-
setPort(port);
35-
setConnectionTimeout(connectionTimeout);
36-
setSoTimeout(soTimeout);
37-
setSsl(ssl);
38-
setSslSocketFactory(sslSocketFactory);
39-
setSslParameters(sslParameters);
40-
setHostnameVerifier(hostnameVerifier);
38+
this.hostAndPort = new HostAndPort(host, port);
39+
this.connectionTimeout = connectionTimeout;
40+
this.soTimeout = soTimeout;
41+
this.ssl = ssl;
42+
this.sslSocketFactory = sslSocketFactory;
43+
this.sslParameters = sslParameters;
44+
this.hostnameVerifier = hostnameVerifier;
4145
}
4246

4347
public DefaultJedisSocketFactory(HostAndPort hostAndPort, JedisClientConfig config) {
44-
setHostAndPort(hostAndPort);
48+
this.hostAndPort = hostAndPort;
4549
if (config != null) {
46-
setConnectionTimeout(config.getConnectionTimeout());
47-
setSoTimeout(config.getSoTimeout());
48-
setSsl(config.isSsl());
49-
setSslSocketFactory(config.getSslSocketFactory());
50-
setSslParameters(config.getSslParameters());
51-
setHostnameVerifier(config.getHostnameVerifier());
52-
setHostAndPortMapper(config.getHostAndPortMapper());
50+
this.connectionTimeout = config.getConnectionTimeout();
51+
this.soTimeout = config.getSoTimeout();
52+
this.ssl = config.isSsl();
53+
this.sslSocketFactory = config.getSslSocketFactory();
54+
this.sslParameters = config.getSslParameters();
55+
this.hostnameVerifier = config.getHostnameVerifier();
56+
this.hostAndPortMapper = config.getHostAndPortMapper();
5357
}
5458
}
5559

@@ -83,9 +87,9 @@ public Socket createSocket() throws JedisConnectionException {
8387

8488
HostnameVerifier hostnameVerifier = getHostnameVerifier();
8589
if (null != hostnameVerifier
86-
&& !hostnameVerifier.verify(getHost(), ((SSLSocket) socket).getSession())) {
90+
&& !hostnameVerifier.verify(hostAndPort.getHost(), ((SSLSocket) socket).getSession())) {
8791
String message = String.format(
88-
"The connection to '%s' failed ssl/tls hostname verification.", getHost());
92+
"The connection to '%s' failed ssl/tls hostname verification.", hostAndPort.getHost());
8993
throw new JedisConnectionException(message);
9094
}
9195
}
@@ -105,43 +109,44 @@ public HostAndPort getSocketHostAndPort() {
105109
HostAndPort hostAndPort = getHostAndPort();
106110
if (mapper != null) {
107111
HostAndPort mapped = mapper.getHostAndPort(hostAndPort);
108-
if (mapped != null) return mapped;
112+
if (mapped != null) {
113+
return mapped;
114+
}
109115
}
110116
return hostAndPort;
111117
}
112118

113119
public HostAndPort getHostAndPort() {
114-
return new HostAndPort(this.host, this.port);
120+
return this.hostAndPort;
115121
}
116122

117-
public void setHostAndPort(HostAndPort hostPort) {
118-
this.host = hostPort.getHost();
119-
this.port = hostPort.getPort();
123+
public void setHostAndPort(HostAndPort hostAndPort) {
124+
this.hostAndPort = hostAndPort;
120125
}
121126

122127
@Override
123128
public String getDescription() {
124-
return host + ":" + port;
129+
return this.hostAndPort.toString();
125130
}
126131

127132
@Override
128133
public String getHost() {
129-
return this.host;
134+
return this.hostAndPort.getHost();
130135
}
131136

132137
@Override
133138
public void setHost(String host) {
134-
this.host = host;
139+
this.hostAndPort = new HostAndPort(host, this.hostAndPort.getPort());
135140
}
136141

137142
@Override
138143
public int getPort() {
139-
return this.port;
144+
return this.hostAndPort.getPort();
140145
}
141146

142147
@Override
143148
public void setPort(int port) {
144-
this.port = port;
149+
this.hostAndPort = new HostAndPort(this.hostAndPort.getHost(), port);
145150
}
146151

147152
@Override

src/main/java/redis/clients/jedis/JedisSocketFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ public interface JedisSocketFactory {
2626

2727
@Deprecated String getDescription();
2828

29-
String getHost();
29+
@Deprecated String getHost();
3030

3131
@Deprecated void setHost(String host);
3232

33-
int getPort();
33+
@Deprecated int getPort();
3434

3535
@Deprecated void setPort(int port);
3636

37-
int getConnectionTimeout();
37+
@Deprecated int getConnectionTimeout();
3838

3939
@Deprecated void setConnectionTimeout(int connectionTimeout);
4040

41-
int getSoTimeout();
41+
@Deprecated int getSoTimeout();
4242

4343
@Deprecated void setSoTimeout(int soTimeout);
4444
}

src/test/java/redis/clients/jedis/tests/JedisClusterTest.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -563,27 +563,11 @@ public void testCloseable() throws IOException {
563563
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
564564
jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort()));
565565

566-
JedisCluster jc = null;
567-
try {
568-
jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT,
569-
DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG);
570-
jc.set("51", "foo");
571-
} finally {
572-
if (jc != null) {
573-
jc.close();
574-
}
575-
}
566+
JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT,
567+
DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG);
568+
jc.set("51", "foo");
569+
jc.close();
576570

577-
// Iterator<JedisPool> poolIterator = jc.getClusterNodes().values().iterator();
578-
// while (poolIterator.hasNext()) {
579-
// JedisPool pool = poolIterator.next();
580-
// try {
581-
// pool.getResource();
582-
// fail("JedisCluster's internal pools should be already destroyed");
583-
// } catch (JedisConnectionException e) {
584-
// // ok to go...
585-
// }
586-
// }
587571
assertEquals(0, jc.getClusterNodes().size());
588572
}
589573

@@ -616,8 +600,8 @@ public void testJedisClusterTimeout() {
616600
@Test
617601
public void testJedisClusterTimeoutWithConfig() {
618602
HostAndPort hp = nodeInfo1;
619-
try (JedisCluster jc = new JedisCluster(hp,
620-
DefaultJedisClientConfig.builder().withConnectionTimeout(4000).withSoTimeout(4000).withPassword("cluster").build(),
603+
try (JedisCluster jc = new JedisCluster(hp, DefaultJedisClientConfig.builder()
604+
.withConnectionTimeout(4000).withSoTimeout(4000).withPassword("cluster").build(),
621605
DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) {
622606

623607
jc.getClusterNodes().values().forEach(pool -> {

0 commit comments

Comments
 (0)