Skip to content

Commit 1a332c0

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-506 - Upgrade to Jedis 2.9.0.
We now support Jedis 2.9.0 with Redis Standalone SSL and Redis Cluster with passwords. The code is compatible with Jedis 2.8.0 when not using SSL or Redis Cluster with passwords. Original Pull Request: spring-projects#211
1 parent 34f2d95 commit 1a332c0

File tree

6 files changed

+58
-14
lines changed

6 files changed

+58
-14
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ work/sentinel-%.conf:
5757

5858
echo port $* >> $@
5959
echo daemonize yes >> $@
60+
echo bind 0.0.0.0 >> $@
6061
echo pidfile $(shell pwd)/work/sentinel-$*.pid >> $@
6162
echo logfile $(shell pwd)/work/sentinel-$*.log >> $@
6263
echo save \"\" >> $@

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<xstream>1.4.8</xstream>
2424
<pool>2.2</pool>
2525
<lettuce>3.4.2.Final</lettuce>
26-
<jedis>2.8.1</jedis>
26+
<jedis>2.9.0</jedis>
2727
<srp>0.7</srp>
2828
<jredis>06052013</jredis>
2929
<multithreadedtc>1.01</multithreadedtc>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ public List<Object> exec() {
770770
throw new InvalidDataAccessApiUsageException("No ongoing transaction. Did you forget to call multi?");
771771
}
772772
List<Object> results = transaction.exec();
773-
return convertPipelineAndTxResults
773+
return convertPipelineAndTxResults && results != null && !results.isEmpty()
774774
? new TransactionResultConverter<Response<?>>(txResults, JedisConverters.exceptionConverter())
775775
.convert(results)
776776
: results;

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
9797
private int timeout = Protocol.DEFAULT_TIMEOUT;
9898
private String password;
9999
private boolean usePool = true;
100+
private boolean useSsl = false;
100101
private Pool<Jedis> pool;
101102
private JedisPoolConfig poolConfig = new JedisPoolConfig();
102103
private int dbIndex = 0;
@@ -264,8 +265,12 @@ protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config)
264265
* @since 1.4
265266
*/
266267
protected Pool<Jedis> createRedisPool() {
267-
return new JedisPool(getPoolConfig(), getShardInfo().getHost(), getShardInfo().getPort(),
268-
getTimeoutFrom(getShardInfo()), getShardInfo().getPassword());
268+
269+
return useSsl
270+
? new JedisPool(getPoolConfig(), getShardInfo().getHost(), getShardInfo().getPort(),
271+
getTimeoutFrom(getShardInfo()), getShardInfo().getPassword(), true)
272+
: new JedisPool(getPoolConfig(), getShardInfo().getHost(), getShardInfo().getPort(),
273+
getTimeoutFrom(getShardInfo()), getShardInfo().getPassword());
269274
}
270275

271276
private JedisCluster createCluster() {
@@ -295,14 +300,15 @@ protected JedisCluster createCluster(RedisClusterConfiguration clusterConfig, Ge
295300

296301
int redirects = clusterConfig.getMaxRedirects() != null ? clusterConfig.getMaxRedirects().intValue() : 5;
297302

298-
if (StringUtils.hasText(getPassword())) {
299-
throw new IllegalArgumentException("Jedis does not support password protected Redis Cluster configurations!");
300-
}
301-
302303
if (poolConfig != null) {
303-
return new JedisCluster(hostAndPort, timeout, redirects, poolConfig);
304+
return StringUtils.hasText(getPassword())
305+
? new JedisCluster(hostAndPort, timeout, timeout, redirects, getPassword(), poolConfig)
306+
: new JedisCluster(hostAndPort, timeout, redirects, poolConfig);
304307
}
305-
return new JedisCluster(hostAndPort, timeout, redirects, poolConfig);
308+
309+
return StringUtils.hasText(getPassword())
310+
? new JedisCluster(hostAndPort, timeout, timeout, redirects, getPassword(), poolConfig)
311+
: new JedisCluster(hostAndPort, timeout, redirects, poolConfig);
306312
}
307313

308314
/*
@@ -388,6 +394,26 @@ public void setHostName(String hostName) {
388394
this.hostName = hostName;
389395
}
390396

397+
/**
398+
* Sets whether to use SSL.
399+
*
400+
* @param useSsl {@literal true} to use SSL.
401+
* @since 1.8
402+
*/
403+
public void setUseSsl(boolean useSsl) {
404+
this.useSsl = useSsl;
405+
}
406+
407+
/**
408+
* Returns whether to use SSL.
409+
*
410+
* @return use of SSL
411+
* @since 1.8
412+
*/
413+
public boolean isUseSsl() {
414+
return useSsl;
415+
}
416+
391417
/**
392418
* Returns the password used for authenticating with the Redis server.
393419
*
@@ -413,7 +439,6 @@ public void setPassword(String password) {
413439
*/
414440
public int getPort() {
415441
return port;
416-
417442
}
418443

419444
/**

src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
7272
import org.springframework.data.redis.connection.SortParameters.Order;
7373
import org.springframework.data.redis.connection.StringRedisConnection.StringTuple;
74+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
7475
import org.springframework.data.redis.core.Cursor;
7576
import org.springframework.data.redis.core.ScanOptions;
7677
import org.springframework.data.redis.core.StringRedisTemplate;
@@ -830,7 +831,12 @@ public void testWatch() throws Exception {
830831
connection.set("testitnow", "somethingelse");
831832
actual.add(connection.exec());
832833
actual.add(connection.get("testitnow"));
833-
verifyResults(Arrays.asList(new Object[] { null, "something" }));
834+
835+
if (connectionFactory instanceof JedisConnectionFactory) {
836+
verifyResults(Arrays.asList(new Object[] { Collections.emptyList(), "something" }));
837+
} else {
838+
verifyResults(Arrays.asList(new Object[] { null, "something" }));
839+
}
834840
}
835841

836842
@SuppressWarnings("unchecked")

src/test/java/org/springframework/data/redis/core/RedisTemplateTests.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
* @author Christoph Strobl
7373
* @author Anqing Shao
7474
* @author Duobiao Ou
75+
* @author Mark Paluch
7576
*/
7677
@RunWith(Parameterized.class)
7778
public class RedisTemplateTests<K, V> {
@@ -785,7 +786,12 @@ public List<Object> execute(RedisOperations operations) throws DataAccessExcepti
785786
return operations.exec();
786787
}
787788
});
788-
assertNull(results);
789+
790+
if (redisTemplate.getConnectionFactory() instanceof JedisConnectionFactory) {
791+
assertThat(results, is(empty()));
792+
} else {
793+
assertNull(results);
794+
}
789795
assertThat(redisTemplate.opsForValue().get(key1), isEqual(value2));
790796
}
791797

@@ -848,7 +854,13 @@ public List<Object> execute(RedisOperations operations) throws DataAccessExcepti
848854
return operations.exec();
849855
}
850856
});
851-
assertNull(results);
857+
858+
if (redisTemplate.getConnectionFactory() instanceof JedisConnectionFactory) {
859+
assertThat(results, is(empty()));
860+
} else {
861+
assertNull(results);
862+
}
863+
852864
assertThat(redisTemplate.opsForValue().get(key1), isEqual(value2));
853865
}
854866

0 commit comments

Comments
 (0)