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
24 changes: 24 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Jedis implements ServerCommands, DatabaseCommands, JedisCommands, J
protected static final byte[][] DUMMY_ARRAY = new byte[0][];

private Pool<Jedis> dataSource = null;
private Pool<Connection> connectionSource = null;

public Jedis() {
connection = new Connection();
Expand Down Expand Up @@ -297,9 +298,24 @@ public void resetState() {
}

protected void setDataSource(Pool<Jedis> jedisPool) {
assertNotPooled();
this.dataSource = jedisPool;
}

protected void setConnectionSource(Pool<Connection> connectionPool) {
assertNotPooled();
this.connectionSource = connectionPool;
}

private void assertNotPooled() {
if (connectionSource != null) {
throw new IllegalStateException("Connection source is already set.");
}
if (dataSource != null) {
throw new IllegalStateException("Data source is already set.");
}
}

@Override
public void close() {
if (dataSource != null) {
Expand All @@ -310,6 +326,14 @@ public void close() {
} else {
pool.returnResource(this);
}
} else if (connectionSource != null) {
Pool<Connection> pool = connectionSource;
this.connectionSource = null;
if (isBroken()) {
pool.returnBrokenResource(connection);
} else {
pool.returnResource(connection);
}
} else {
connection.close();
}
Expand Down
15 changes: 1 addition & 14 deletions src/main/java/redis/clients/jedis/JedisPool.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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 All @@ -16,7 +14,7 @@
import redis.clients.jedis.util.Pool;

// Legacy
public class JedisPool extends Pool<Jedis> {
public class JedisPool extends Pool<Jedis> implements JedisProvider {

private static final Logger log = LoggerFactory.getLogger(JedisPool.class);

Expand Down Expand Up @@ -395,16 +393,5 @@ public void returnResource(final Jedis resource) {
}
}

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

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

}
18 changes: 17 additions & 1 deletion src/main/java/redis/clients/jedis/JedisPooled.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import redis.clients.jedis.util.JedisURIHelper;
import redis.clients.jedis.util.Pool;

public class JedisPooled extends UnifiedJedis {
public class JedisPooled extends UnifiedJedis implements JedisProvider {

public JedisPooled() {
this(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT);
Expand Down Expand Up @@ -463,4 +463,20 @@ public final Pool<Connection> getPool() {
public Pipeline pipelined() {
return (Pipeline) super.pipelined();
}

@Override
public Jedis getResource() {
Pool<Connection> pool = getPool();
Connection conn = pool.getResource();
Jedis jedis = new Jedis(conn);
jedis.setConnectionSource(pool);
return jedis;
}

@Override
public void returnResource(final Jedis resource) {
Pool<Connection> pool = getPool();
pool.returnResource(resource.getConnection());
}

}
44 changes: 44 additions & 0 deletions src/main/java/redis/clients/jedis/JedisProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package redis.clients.jedis;

import java.util.function.Consumer;
import java.util.function.Function;

/**
* Interface providing Jedis resources from a pool
*/
public interface JedisProvider {

/**
* Get a Jedis resource from the pool
* @return Jedis object
*/
Jedis getResource();

/**
* Return a Jedis resource to the pool
* @param resource Jedis object
*/
void returnResource(final Jedis resource);

/**
* Execute a consumer with a Jedis resource
* @param consumer code to execute with the Jedis resource
*/
default void withResource(Consumer<Jedis> consumer) {
try (Jedis jedis = this.getResource()) {
consumer.accept(jedis);
}
}

/**
* Execute a function with a Jedis resource and return a value
* @param function code to execute with the Jedis resource
* @return return value from the function
*/
default <K> K withResourceGet(Function<Jedis, K> function) {
try (Jedis jedis = this.getResource()) {
return function.apply(jedis);
}
}

}
Loading
Loading