Last active
August 29, 2015 14:23
-
-
Save RobertFischer/ea4194137c02973c20f6 to your computer and use it in GitHub Desktop.
JedisPool/Jedis Resource Handling Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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