From 712698c76c61a2b2b863e05cefafa93fea72939f Mon Sep 17 00:00:00 2001 From: Tux Date: Sat, 6 Jun 2015 17:30:45 -0400 Subject: [PATCH] Changed all previous uses of return(Broken)Resource to try-with-resources --- .../minecraft/redisbungee/DataManager.java | 28 +++---------------- .../minecraft/redisbungee/RedisBungee.java | 23 +++------------ 2 files changed, 8 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java index 5971297..0fd4e9a 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java @@ -56,8 +56,7 @@ public class DataManager implements Listener { if (server != null) return server; - Jedis tmpRsc = plugin.getPool().getResource(); - try { + try (Jedis tmpRsc = plugin.getPool().getResource()) { server = tmpRsc.hget("player:" + uuid, "server"); if (server == null) @@ -68,11 +67,7 @@ public class DataManager implements Listener { } catch (JedisConnectionException e) { // Redis server has disappeared! plugin.getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - if (tmpRsc != null) - plugin.getPool().returnBrokenResource(tmpRsc); throw new RuntimeException("Unable to get server for " + uuid, e); - } finally { - plugin.getPool().returnResource(tmpRsc); } } @@ -87,8 +82,7 @@ public class DataManager implements Listener { if (server != null) return server; - Jedis tmpRsc = plugin.getPool().getResource(); - try { + try (Jedis tmpRsc = plugin.getPool().getResource()) { server = tmpRsc.hget("player:" + uuid, "proxy"); if (server == null) @@ -99,11 +93,7 @@ public class DataManager implements Listener { } catch (JedisConnectionException e) { // Redis server has disappeared! plugin.getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - if (tmpRsc != null) - plugin.getPool().returnBrokenResource(tmpRsc); throw new RuntimeException("Unable to get server for " + uuid, e); - } finally { - plugin.getPool().returnResource(tmpRsc); } } @@ -118,8 +108,7 @@ public class DataManager implements Listener { if (address != null) return address; - Jedis tmpRsc = plugin.getPool().getResource(); - try { + try (Jedis tmpRsc = plugin.getPool().getResource()) { String result = tmpRsc.hget("player:" + uuid, "ip"); if (result != null) { address = InetAddresses.forString(result); @@ -130,11 +119,7 @@ public class DataManager implements Listener { } catch (JedisConnectionException e) { // Redis server has disappeared! plugin.getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - if (tmpRsc != null) - plugin.getPool().returnBrokenResource(tmpRsc); throw new RuntimeException("Unable to get server for " + uuid, e); - } finally { - plugin.getPool().returnResource(tmpRsc); } } @@ -149,8 +134,7 @@ public class DataManager implements Listener { if (time != null) return time; - Jedis tmpRsc = plugin.getPool().getResource(); - try { + try (Jedis tmpRsc = plugin.getPool().getResource()) { String result = tmpRsc.hget("player:" + uuid, "online"); if (result != null) try { @@ -188,11 +172,7 @@ public class DataManager implements Listener { } catch (JedisConnectionException e) { // Redis server has disappeared! plugin.getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - if (tmpRsc != null) - plugin.getPool().returnBrokenResource(tmpRsc); throw new RuntimeException("Unable to get server for " + uuid, e); - } finally { - plugin.getPool().returnResource(tmpRsc); } } diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index d4ab354..90a66b8 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -87,8 +87,7 @@ public final class RedisBungee extends Plugin { } final List getCurrentServerIds() { - Jedis jedis = pool.getResource(); - try { + try (Jedis jedis = pool.getResource()) { int nag = nagAboutServers.decrementAndGet(); if (nag <= 0) { nagAboutServers.set(10); @@ -109,11 +108,7 @@ public final class RedisBungee extends Plugin { return servers.build(); } catch (JedisConnectionException e) { getLogger().log(Level.SEVERE, "Unable to fetch all server IDs", e); - if (jedis != null) - pool.returnBrokenResource(jedis); return Collections.singletonList(configuration.getServerId()); - } finally { - pool.returnResource(jedis); } } @@ -212,26 +207,19 @@ public final class RedisBungee extends Plugin { throw new RuntimeException("Unable to connect to your Redis server!", e); } if (pool != null) { - Jedis tmpRsc = pool.getResource(); - try { + try (Jedis tmpRsc = pool.getResource()) { tmpRsc.hset("heartbeats", configuration.getServerId(), String.valueOf(System.currentTimeMillis())); - } finally { - pool.returnResource(tmpRsc); } serverIds = getCurrentServerIds(); uuidTranslator = new UUIDTranslator(this); heartbeatTask = getProxy().getScheduler().schedule(this, new Runnable() { @Override public void run() { - Jedis rsc = pool.getResource(); - try { + try (Jedis rsc = pool.getResource()) { rsc.hset("heartbeats", configuration.getServerId(), String.valueOf(System.currentTimeMillis())); } catch (JedisConnectionException e) { // Redis server has disappeared! getLogger().log(Level.SEVERE, "Unable to update heartbeat - did your Redis server go away?", e); - pool.returnBrokenResource(rsc); - } finally { - pool.returnResource(rsc); } serverIds = getCurrentServerIds(); } @@ -316,16 +304,13 @@ public final class RedisBungee extends Plugin { } catch (InterruptedException ignored) { } - Jedis tmpRsc = pool.getResource(); - try { + try (Jedis tmpRsc = pool.getResource()) { tmpRsc.hdel("heartbeats", configuration.getServerId()); if (tmpRsc.scard("proxy:" + configuration.getServerId() + ":usersOnline") > 0) { Set players = tmpRsc.smembers("proxy:" + configuration.getServerId() + ":usersOnline"); for (String member : players) RedisUtil.cleanUpPlayer(member, tmpRsc); } - } finally { - pool.returnResource(tmpRsc); } pool.destroy();