Skip to content

Commit 586a0f1

Browse files
committed
polishing, JedisShardInfo, etc
1 parent 314dcf5 commit 586a0f1

15 files changed

+117
-48
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public class BinaryClient extends Connection {
4141

4242
private boolean isInMulti;
4343

44-
private String user;
45-
private String password;
44+
@Deprecated private String user;
45+
@Deprecated private String password;
4646

4747
private int db;
4848

@@ -103,10 +103,12 @@ private byte[][] joinParameters(byte[] first, byte[] second, byte[][] rest) {
103103
return result;
104104
}
105105

106+
@Deprecated
106107
public void setUser(final String user) {
107108
this.user = user;
108109
}
109110

111+
@Deprecated
110112
public void setPassword(final String password) {
111113
this.password = password;
112114
}

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public BinaryJedis(final String host, final int port, final boolean ssl,
8686
final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters,
8787
final HostnameVerifier hostnameVerifier) {
8888
this(host, port, DefaultJedisSocketConfig.builder().withSsl(ssl)
89-
.withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters).withHostnameVerifier(hostnameVerifier).build());
89+
.withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters)
90+
.withHostnameVerifier(hostnameVerifier).build());
9091
}
9192

9293
public BinaryJedis(final String host, final int port, final int timeout) {
@@ -126,7 +127,8 @@ public BinaryJedis(final String host, final int port, final int connectionTimeou
126127
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
127128
this(host, port, DefaultJedisSocketConfig.builder()
128129
.withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout).withSsl(ssl)
129-
.withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters).withHostnameVerifier(hostnameVerifier).build());
130+
.withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters)
131+
.withHostnameVerifier(hostnameVerifier).build());
130132
}
131133

132134
public BinaryJedis(final String host, final int port, final int connectionTimeout,
@@ -142,13 +144,32 @@ public BinaryJedis(final HostAndPort hostAndPort, final JedisSocketConfig config
142144
client.setInfiniteSoTimeout(infiniteSoTimeout);
143145
}
144146

147+
/**
148+
* @param shardInfo
149+
* @deprecated Internal behavior of this constructor has been changed.
150+
*/
151+
@Deprecated
145152
public BinaryJedis(final JedisShardInfo shardInfo) {
146153
this(shardInfo.getHost(), shardInfo.getPort(), shardInfo.getConnectionTimeout(),
147154
shardInfo.getSoTimeout(), shardInfo.getSsl(), shardInfo.getSslSocketFactory(),
148155
shardInfo.getSslParameters(), shardInfo.getHostnameVerifier());
149-
client.setUser(shardInfo.getUser());
150-
client.setPassword(shardInfo.getPassword());
151-
client.setDb(shardInfo.getDb());
156+
initializeFromShardInfo(shardInfo);
157+
}
158+
159+
private void initializeFromShardInfo(JedisShardInfo shardInfo) {
160+
String password = shardInfo.getPassword();
161+
if (password != null) {
162+
String user = shardInfo.getUser();
163+
if (user != null) {
164+
auth(user, password);
165+
} else {
166+
auth(password);
167+
}
168+
}
169+
int dbIndex = shardInfo.getDb();
170+
if (dbIndex > 0) {
171+
select(dbIndex);
172+
}
152173
}
153174

154175
public BinaryJedis(URI uri) {
@@ -207,10 +228,6 @@ public BinaryJedis(final URI uri, JedisSocketConfig config) {
207228
initializeFromURI(uri);
208229
}
209230

210-
public BinaryJedis(final JedisSocketFactory jedisSocketFactory) {
211-
client = new Client(jedisSocketFactory);
212-
}
213-
214231
private static Client createClientFromURI(URI uri) {
215232
if (!JedisURIHelper.isValid(uri)) {
216233
throw new InvalidURIException(String.format("Cannot open Redis connection due invalid URI \"%s\".", uri.toString()));
@@ -235,6 +252,14 @@ private void initializeFromURI(URI uri) {
235252
}
236253
}
237254

255+
public BinaryJedis(final JedisSocketFactory jedisSocketFactory) {
256+
client = new Client(jedisSocketFactory);
257+
}
258+
259+
public boolean isBroken() {
260+
return client.isBroken();
261+
}
262+
238263
/**
239264
* @return <code>PONG</code>
240265
*/

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ public class Connection implements Closeable {
2323

2424
private static final byte[][] EMPTY_ARGS = new byte[0][];
2525

26-
private final JedisSocketFactory socketFactory;
26+
private boolean socketParamModified = false; // for backward compatibility
27+
private JedisSocketFactory socketFactory; // TODO: sould be final
2728
private Socket socket;
2829
private RedisOutputStream outputStream;
2930
private RedisInputStream inputStream;
31+
private int soTimeout = Protocol.DEFAULT_TIMEOUT;
3032
private int infiniteSoTimeout = 0;
3133
private boolean broken = false;
3234

3335
public Connection() {
34-
this(Protocol.DEFAULT_HOST);
36+
this(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT);
3537
}
3638

39+
@Deprecated
3740
public Connection(final String host) {
3841
this(host, Protocol.DEFAULT_PORT);
3942
}
@@ -66,6 +69,7 @@ public Connection(final HostAndPort hostAndPort, final JedisSocketConfig jedisSo
6669

6770
public Connection(final JedisSocketFactory jedisSocketFactory) {
6871
this.socketFactory = jedisSocketFactory;
72+
this.soTimeout = jedisSocketFactory.getSoTimeout();
6973
}
7074

7175
public Socket getSocket() {
@@ -77,16 +81,16 @@ public int getConnectionTimeout() {
7781
}
7882

7983
public int getSoTimeout() {
80-
return socketFactory.getSoTimeout();
84+
return soTimeout;
8185
}
8286

8387
@Deprecated
8488
public void setConnectionTimeout(int connectionTimeout) {
8589
socketFactory.setConnectionTimeout(connectionTimeout);
8690
}
8791

88-
@Deprecated
8992
public void setSoTimeout(int soTimeout) {
93+
this.soTimeout = soTimeout;
9094
socketFactory.setSoTimeout(soTimeout);
9195
}
9296

@@ -161,6 +165,7 @@ public String getHost() {
161165
@Deprecated
162166
public void setHost(final String host) {
163167
socketFactory.setHost(host);
168+
socketParamModified = true;
164169
}
165170

166171
public int getPort() {
@@ -170,9 +175,17 @@ public int getPort() {
170175
@Deprecated
171176
public void setPort(final int port) {
172177
socketFactory.setPort(port);
178+
socketParamModified = true;
173179
}
174180

175-
public void connect() {
181+
public void connect() throws JedisConnectionException {
182+
if (socketParamModified) { // this is only for backward compatibility
183+
try {
184+
disconnect();
185+
} catch(Exception e) {
186+
// swallow
187+
}
188+
}
176189
if (!isConnected()) {
177190
try {
178191
socket = socketFactory.createSocket();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ public String getClientName() {
8080
}
8181

8282
@Override
83-
public boolean isSSL() {
83+
public boolean isSsl() {
8484
return ssl;
8585
}
8686

8787
@Override
88-
public SSLSocketFactory getSSLSocketFactory() {
88+
public SSLSocketFactory getSslSocketFactory() {
8989
return sslSocketFactory;
9090
}
9191

9292
@Override
93-
public SSLParameters getSSLParameters() {
93+
public SSLParameters getSslParameters() {
9494
return sslParameters;
9595
}
9696

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ public int getSoTimeout() {
4141
}
4242

4343
@Override
44-
public boolean isSSL() {
44+
public boolean isSsl() {
4545
return ssl;
4646
}
4747

4848
@Override
49-
public SSLSocketFactory getSSLSocketFactory() {
49+
public SSLSocketFactory getSslSocketFactory() {
5050
return sslSocketFactory;
5151
}
5252

5353
@Override
54-
public SSLParameters getSSLParameters() {
54+
public SSLParameters getSslParameters() {
5555
return sslParameters;
5656
}
5757

@@ -159,7 +159,7 @@ public HostAndPortMapper getHostAndPortMapper() {
159159

160160
static DefaultJedisSocketConfig withSsl(boolean ssl, JedisSocketConfig copy) {
161161
return new DefaultJedisSocketConfig(copy.getConnectionTimeout(), copy.getSoTimeout(),
162-
ssl, copy.getSSLSocketFactory(), copy.getSSLParameters(), copy.getHostnameVerifier(),
162+
ssl, copy.getSslSocketFactory(), copy.getSslParameters(), copy.getHostnameVerifier(),
163163
copy.getHostAndPortMapper());
164164
}
165165
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public class DefaultJedisSocketFactory implements JedisSocketFactory {
1515

16-
private final HostAndPort hostPort;
16+
private HostAndPort hostPort; // TODO: should be final
1717
private final JedisSocketConfig config;
1818

1919
@Deprecated
@@ -53,14 +53,14 @@ public Socket createSocket() throws JedisConnectionException {
5353
socket.connect(new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort()), getConnectionTimeout());
5454
socket.setSoTimeout(getSoTimeout());
5555

56-
if (config.isSSL()) {
57-
SSLSocketFactory sslSocketFactory = config.getSSLSocketFactory();
56+
if (config.isSsl()) {
57+
SSLSocketFactory sslSocketFactory = config.getSslSocketFactory();
5858
if (null == sslSocketFactory) {
5959
sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
6060
}
6161
socket = sslSocketFactory.createSocket(socket, hostAndPort.getHost(), hostAndPort.getPort(), true);
6262

63-
SSLParameters sslParameters = config.getSSLParameters();
63+
SSLParameters sslParameters = config.getSslParameters();
6464
if (null != sslParameters) {
6565
((SSLSocket) socket).setSSLParameters(sslParameters);
6666
}
@@ -109,8 +109,9 @@ public String getHost() {
109109
}
110110

111111
@Override
112+
@Deprecated
112113
public void setHost(String host) {
113-
// throw exception?
114+
this.hostPort = new HostAndPort(host, this.hostPort.getPort());
114115
}
115116

116117
@Override
@@ -119,8 +120,9 @@ public int getPort() {
119120
}
120121

121122
@Override
123+
@Deprecated
122124
public void setPort(int port) {
123-
// throw exception?
125+
this.hostPort = new HostAndPort(this.hostPort.getHost(), port);
124126
}
125127

126128
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public Jedis(final String host, final int port, final int connectionTimeout, fin
113113
sslParameters, hostnameVerifier);
114114
}
115115

116+
/**
117+
* @param shardInfo
118+
* @deprecated Internal behavior of this constructor has been changed.
119+
*/
120+
@Deprecated
116121
public Jedis(JedisShardInfo shardInfo) {
117122
super(shardInfo);
118123
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ public PooledObject<Jedis> makeObject() throws Exception {
144144
jedis.clientSetname(clientConfig.getClientName());
145145
}
146146
} catch (JedisException je) {
147+
try {
148+
if (jedis.isConnected() && !jedis.isBroken()) {
149+
jedis.quit();
150+
}
151+
} catch(Exception e) {
152+
// swallow
153+
}
147154
jedis.close();
148155
throw je;
149156
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public interface JedisSocketConfig {
1010

1111
int getSoTimeout();
1212

13-
boolean isSSL();
13+
boolean isSsl();
1414

15-
SSLSocketFactory getSSLSocketFactory();
15+
SSLSocketFactory getSslSocketFactory();
1616

17-
SSLParameters getSSLParameters();
17+
SSLParameters getSslParameters();
1818

1919
HostnameVerifier getHostnameVerifier();
2020

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
public interface JedisSocketFactory {
1818

1919
/**
20+
* @deprecated throwing IOException will not be supported in future
2021
* @return Socket
2122
* @throws IOException this will be removed in future
2223
* @throws JedisConnectionException
23-
* @deprecated throwing IOException will not be supported in future
2424
*/
2525
@Deprecated
2626
Socket createSocket() throws IOException, JedisConnectionException;
2727

2828
@Deprecated String getDescription();
2929

30-
@Deprecated String getHost();
30+
String getHost();
3131

3232
@Deprecated void setHost(String host);
3333

34-
@Deprecated int getPort();
34+
int getPort();
3535

3636
@Deprecated void setPort(int port);
3737

0 commit comments

Comments
 (0)