Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/redis/clients/jedis/JedisPool.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package redis.clients.jedis;

import java.net.URI;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;
Expand Down Expand Up @@ -392,4 +394,17 @@ public void returnResource(final Jedis resource) {
}
}
}

public void withJedisPoolDo(Consumer<Jedis> consumer) {
try (Jedis jedis = this.getResource()) {
consumer.accept(jedis);
}
}

public <K> K withJedisPoolGet(Function<Jedis, K> function) {
try (Jedis jedis = this.getResource()) {
return function.apply(jedis);
}
}

}
26 changes: 26 additions & 0 deletions src/test/java/redis/clients/jedis/JedisPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,30 @@ public void testResetValidCredentials() {
}
}
}

@Test
public void testWithJedisPoolDoWithConnection() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000);
pool.withJedisPoolDo(jedis -> {
jedis.auth(endpointStandalone0.getPassword());
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
});
pool.close();
assertTrue(pool.isClosed());
}

@Test
public void testWithJedisPoolGetWithConnection() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000);
String result = pool.withJedisPoolGet(jedis -> {
jedis.auth(endpointStandalone0.getPassword());
jedis.set("foo", "bar");
return jedis.get("foo");
});
assertEquals("bar", result);
pool.close();
assertTrue(pool.isClosed());
}

}
Loading