Skip to content

Instantly share code, notes, and snippets.

@RobertFischer
Last active August 29, 2015 14:23
Show Gist options
  • Save RobertFischer/ea4194137c02973c20f6 to your computer and use it in GitHub Desktop.
Save RobertFischer/ea4194137c02973c20f6 to your computer and use it in GitHub Desktop.
JedisPool/Jedis Resource Handling Example
private interface RedisCallback<T> {
T apply(Jedis jedis) throws IOException;
}
private <T> T withRedis(RedisCallback<T> callback) throws IOException {
Objects.requireNonNull(redisConnectionPool, "redis connection pool must be initialized");
Objects.requireNonNull(callback, "callback needs to be provided");
try(Jedis jedis = redisConnectionPool.getResource()) {
T result = callback.apply(jedis);
redisConnectionPool.returnResource(jedis);
return result;
} catch(IOException ioe) {
redisConnectionPool.returnBrokenResource(jedis);
throw ioe;
} catch(Exception e) {
redisConnectionPool.returnBrokenResource(jedis);
throw new RuntimeException("Error working with Redis", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment