void unsetEndpoint(ServiceReference<Endpoint> endpoint) { lock.writeLock().lock(); try { JedisPool pool = readableMap.remove(endpoint); if (pool != null) pool.destroy(); pool = writableMap.remove(endpoint); if (pool != null) pool.destroy(); } finally { lock.writeLock().unlock(); } }
public void destroy() { super.destroy(); try { expireFuture.cancel(true); } catch (Throwable t) { logger.warn(t.getMessage(), t); } try { for (RedisRegistryNotifier notifier : notifiers.values()) { notifier.shutdown(); } } catch (Throwable t) { logger.warn(t.getMessage(), t); } for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) { JedisPool jedisPool = entry.getValue(); try { jedisPool.destroy(); } catch (Throwable t) { logger.warn( "Failed to destroy the redis registry client. registry: " + entry.getKey() + ", cause: " + t.getMessage(), t); } } }
public static void main(String[] args) { JedisPool jedisPool = new JedisPool("localhost"); try { Jedis jedis = jedisPool.getResource(); String result = jedis.get("msg"); System.out.println(result); jedis.set("msg_java", "Hello Java"); jedisPool.returnResource(jedis); String key, value; Scanner sc = new Scanner(System.in); do { System.out.println("veuillez entrer la clé"); key = sc.next(); System.out.println("veuillez entrer la valeur"); value = sc.next(); jedis.set(key, value); jedisPool.returnResource(jedis); System.out.println(key + " " + value); System.out.println(key + " " + jedis.get(key)); } while (!key.equals("")); sc.close(); } finally { jedisPool.destroy(); } }
public static void main(String[] args) throws Exception { JedisPool pool = JedisPoolFactory.createJedisPool( JedisUtils.DEFAULT_HOST, JedisUtils.DEFAULT_PORT, JedisUtils.DEFAULT_TIMEOUT, 1); try { JobDispatcher dispatcher = new JobDispatcher("ss", pool); JobStatistics statistics = new JobStatistics("ss", pool); startPrintStatistics(statistics); dispatcher.start(); System.out.println("Hit enter to stop."); while (true) { char c = (char) System.in.read(); if (c == '\n') { System.out.println("Shuting down"); dispatcher.stop(); stopPrintStatistics(); return; } } } finally { pool.destroy(); } }
public static void main(String[] args) { JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost"); Jedis jedis = null; try { jedis = pool.getResource(); /// ... do stuff here ... for example jedis.set("foo", "bar"); String foobar = jedis.get("foo"); System.out.println("foobar=" + foobar); jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike"); Set<String> sose = jedis.zrange("sose", 0, -1); jedis.watch("foo"); Transaction t = jedis.multi(); t.set("foo", "bar"); t.exec(); } finally { if (jedis != null) { jedis.close(); } } /// ... when closing your application: pool.destroy(); // Jedis jedis = new Jedis("localhost"); // //jedis.set("foo", "bar"); // String value = jedis.get("foo"); // System.out.println("value=" + value); }
void updatedEndpoint(ServiceReference<Endpoint> endpoint) { lock.writeLock().lock(); try { JedisPool oldPool = writableMap.remove(endpoint); if (oldPool == null) oldPool = readableMap.remove(endpoint); try { if (oldPool != null) oldPool.destroy(); } catch (RuntimeException re) { } setEndpoint(endpoint); } finally { lock.writeLock().unlock(); } }
// 通过JedisPool对象与Redis建立连接池 // @Test public void testMutiThread() { JedisPool pool = null; Jedis jedis = null; try { // JedisPool依赖于apache-commons-pools1 pool = new JedisPool(new JedisPoolConfig(), HOST, PORT); jedis = pool.getResource(); jedis.set("muti", "bar"); System.out.println(jedis.get("muti")); } catch (Exception e) { e.printStackTrace(); } finally { if (null != jedis) { // 释放已经用过的连接 jedis.close(); pool.destroy(); } } }
@Override public void onDisable() { if (pool != null) { // Poison the PubSub listener psl.poison(); getProxy().getScheduler().cancel(this); integrityCheck.cancel(); heartbeatTask.cancel(); getProxy().getPluginManager().unregisterListeners(this); try (Jedis tmpRsc = pool.getResource()) { tmpRsc.hdel("heartbeats", configuration.getServerId()); if (tmpRsc.scard("proxy:" + configuration.getServerId() + ":usersOnline") > 0) { Set<String> players = tmpRsc.smembers("proxy:" + configuration.getServerId() + ":usersOnline"); for (String member : players) RedisUtil.cleanUpPlayer(member, tmpRsc); } } pool.destroy(); } }
@Override public void close() { pool.destroy(); pool.close(); }
@Override public void stop() { pool.destroy(); }
public void destroy() { pool.destroy(); }
private void loadConfig() throws IOException, JedisConnectionException { if (!getDataFolder().exists()) { getDataFolder().mkdir(); } File file = new File(getDataFolder(), "config.yml"); if (!file.exists()) { file.createNewFile(); try (InputStream in = getResourceAsStream("example_config.yml"); OutputStream out = new FileOutputStream(file)) { ByteStreams.copy(in, out); } } final Configuration configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file); final String redisServer = configuration.getString("redis-server", "localhost"); final int redisPort = configuration.getInt("redis-port", 6379); String redisPassword = configuration.getString("redis-password"); String serverId = configuration.getString("server-id"); if (redisPassword != null && (redisPassword.isEmpty() || redisPassword.equals("none"))) { redisPassword = null; } // Configuration sanity checks. if (serverId == null || serverId.isEmpty()) { throw new RuntimeException("server-id is not specified in the configuration or is empty"); } if (redisServer != null && !redisServer.isEmpty()) { final String finalRedisPassword = redisPassword; FutureTask<JedisPool> task = new FutureTask<>( new Callable<JedisPool>() { @Override public JedisPool call() throws Exception { // With recent versions of Jedis, we must set the classloader to the one // BungeeCord used // to load RedisBungee with. ClassLoader previous = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(RedisBungee.class.getClassLoader()); // Create the pool... JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(configuration.getInt("max-redis-connections", 8)); JedisPool pool = new JedisPool(config, redisServer, redisPort, 0, finalRedisPassword); // Reset classloader and return the pool Thread.currentThread().setContextClassLoader(previous); return pool; } }); getProxy().getScheduler().runAsync(this, task); try { pool = task.get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException("Unable to create Redis pool", e); } // Test the connection try (Jedis rsc = pool.getResource()) { rsc.ping(); // If that worked, now we can check for an existing, alive Bungee: File crashFile = new File(getDataFolder(), "restarted_from_crash.txt"); if (crashFile.exists()) { crashFile.delete(); } else if (rsc.hexists("heartbeats", serverId)) { try { long value = Long.parseLong(rsc.hget("heartbeats", serverId)); if (System.currentTimeMillis() < value + 20000) { getLogger() .severe( "You have launched a possible impostor BungeeCord instance. Another instance is already running."); getLogger() .severe("For data consistency reasons, RedisBungee will now disable itself."); getLogger() .severe( "If this instance is coming up from a crash, create a file in your RedisBungee plugins directory with the name 'restarted_from_crash.txt' and RedisBungee will not perform this check."); throw new RuntimeException("Possible impostor instance!"); } } catch (NumberFormatException ignored) { } } FutureTask<Void> task2 = new FutureTask<>( new Callable<Void>() { @Override public Void call() throws Exception { httpClient = new OkHttpClient(); Dispatcher dispatcher = new Dispatcher(getExecutorService()); httpClient.setDispatcher(dispatcher); NameFetcher.setHttpClient(httpClient); UUIDFetcher.setHttpClient(httpClient); RedisBungee.configuration = new RedisBungeeConfiguration(RedisBungee.this.getPool(), configuration); return null; } }); getProxy().getScheduler().runAsync(this, task2); try { task2.get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException("Unable to create HTTP client", e); } getLogger().log(Level.INFO, "Successfully connected to Redis."); } catch (JedisConnectionException e) { pool.destroy(); pool = null; throw e; } } else { throw new RuntimeException("No redis server specified!"); } }
public void finish() { pool.returnResource(jedis); pool.destroy(); }