Skip to content

Commit

Permalink
Minimal constructors with Config (#2577)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Jul 8, 2021
1 parent 50c0eb5 commit b3c172c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
22 changes: 13 additions & 9 deletions src/main/java/redis/clients/jedis/JedisPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final String h
final int connectionTimeout, final int soTimeout, final String password, final int database,
final String clientName, final boolean ssl, final SSLSocketFactory sslSocketFactory,
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
super(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, password,
this(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, password,
database, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier));
}

Expand All @@ -251,19 +251,23 @@ public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final String h
final String user, final String password, final int database, final String clientName,
final boolean ssl, final SSLSocketFactory sslSocketFactory,
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
super(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, infiniteSoTimeout,
this(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, infiniteSoTimeout,
user, password, database, clientName, ssl, sslSocketFactory, sslParameters,
hostnameVerifier));
}

public JedisPool(final HostAndPort hostAndPort, final JedisClientConfig clientConfig) {
this(new GenericObjectPoolConfig<Jedis>(), hostAndPort, clientConfig);
}

public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final HostAndPort hostAndPort,
final JedisClientConfig clientConfig) {
super(poolConfig, new JedisFactory(hostAndPort, clientConfig));
this(poolConfig, new JedisFactory(hostAndPort, clientConfig));
}

public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final JedisSocketFactory jedisSocketFactory,
final JedisClientConfig clientConfig) {
super(poolConfig, new JedisFactory(jedisSocketFactory, clientConfig));
this(poolConfig, new JedisFactory(jedisSocketFactory, clientConfig));
}

public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig) {
Expand All @@ -277,21 +281,21 @@ public JedisPool(final String host, final int port, final boolean ssl) {
public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final String host, int port,
final int connectionTimeout, final int soTimeout, final String password, final int database,
final String clientName) {
super(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, password,
this(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, password,
database, clientName));
}

public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final String host, int port,
final int connectionTimeout, final int soTimeout, final String user, final String password,
final int database, final String clientName) {
super(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, user, password,
this(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, user, password,
database, clientName));
}

public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final String host, int port,
final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout,
final String user, final String password, final int database, final String clientName) {
super(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, infiniteSoTimeout,
this(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, infiniteSoTimeout,
user, password, database, clientName));
}

Expand Down Expand Up @@ -345,15 +349,15 @@ public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final URI uri,
public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final URI uri,
final int connectionTimeout, final int soTimeout, final SSLSocketFactory sslSocketFactory,
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
super(poolConfig, new JedisFactory(uri, connectionTimeout, soTimeout, null, sslSocketFactory,
this(poolConfig, new JedisFactory(uri, connectionTimeout, soTimeout, null, sslSocketFactory,
sslParameters, hostnameVerifier));
}

public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final URI uri,
final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout,
final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters,
final HostnameVerifier hostnameVerifier) {
super(poolConfig, new JedisFactory(uri, connectionTimeout, soTimeout, infiniteSoTimeout, null,
this(poolConfig, new JedisFactory(uri, connectionTimeout, soTimeout, infiniteSoTimeout, null,
sslSocketFactory, sslParameters, hostnameVerifier));
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/redis/clients/jedis/JedisSentinelPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ public JedisSentinelPool(String masterName, Set<String> sentinels,
DefaultJedisClientConfig.builder().build());
}

public JedisSentinelPool(String masterName, Set<HostAndPort> sentinels,
final JedisClientConfig masteClientConfig, final JedisClientConfig sentinelClientConfig) {
this(masterName, sentinels, new GenericObjectPoolConfig<Jedis>(), masteClientConfig, sentinelClientConfig);
}

public JedisSentinelPool(String masterName, Set<HostAndPort> sentinels,
final GenericObjectPoolConfig<Jedis> poolConfig, final JedisClientConfig masteClientConfig,
final JedisClientConfig sentinelClientConfig) {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/redis/clients/jedis/tests/JedisPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Test;

import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisFactory;
Expand All @@ -40,6 +41,19 @@ public void checkConnections() {
assertTrue(pool.isClosed());
}

@Test
public void checkResourceWithConfig() {
try (JedisPool pool = new JedisPool(HostAndPortUtil.getRedisServers().get(7),
DefaultJedisClientConfig.builder().socketTimeoutMillis(5000).build())) {

try (Jedis jedis = pool.getResource()) {
assertEquals("PONG", jedis.ping());
assertEquals(5000, jedis.getClient().getSoTimeout());
jedis.close();
}
}
}

@Test
public void checkCloseableConnections() throws Exception {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package redis.clients.jedis.tests;

import java.util.HashSet;
import java.util.Set;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
Expand Down Expand Up @@ -38,6 +38,9 @@ public void sentinelWithoutSslConnectsToRedisWithSsl() {
.hostAndPortMapper(SSL_PORT_MAPPER).build();
DefaultJedisClientConfig sentinelConfig = DefaultJedisClientConfig.builder()
.clientName("sentinel-client").ssl(false).build();
try (JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, masterConfig, sentinelConfig)) {
pool.getResource().close();
}
try (JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, POOL_CONFIG,
masterConfig, sentinelConfig)) {
pool.getResource().close();
Expand All @@ -50,6 +53,9 @@ public void sentinelWithSslConnectsToRedisWithoutSsl() {
.password("foobared").clientName("sentinel-master-client").ssl(false).build();
DefaultJedisClientConfig sentinelConfig = DefaultJedisClientConfig.builder()
.clientName("sentinel-client").ssl(true).hostAndPortMapper(SSL_PORT_MAPPER).build();
try (JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, masterConfig, sentinelConfig)) {
pool.getResource().close();
}
try (JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, POOL_CONFIG,
masterConfig, sentinelConfig)) {
pool.getResource().close();
Expand All @@ -63,6 +69,9 @@ public void sentinelWithSslConnectsToRedisWithSsl() {
.hostAndPortMapper(SSL_PORT_MAPPER).build();
DefaultJedisClientConfig sentinelConfig = DefaultJedisClientConfig.builder()
.clientName("sentinel-client").ssl(true).hostAndPortMapper(SSL_PORT_MAPPER).build();
try (JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, masterConfig, sentinelConfig)) {
pool.getResource().close();
}
try (JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, POOL_CONFIG,
masterConfig, sentinelConfig)) {
pool.getResource().close();
Expand Down

0 comments on commit b3c172c

Please sign in to comment.