public static Jedis createJedis(RedisClientProfile profile) { Jedis jedis = new Jedis(profile.getHostaddress(), profile.getPort()); jedis.connect(); try { // jedis.getClient().setTimeoutInfinite(); jedis .getClient() .getSocket() .setKeepAlive(true); // keep alive, with time out set to 2 seconds // jedis.getClient().getSocket().setSoTimeout(50); // jedis.getClient().setTimeout(5); } catch (Exception e) { throw new BackendRuntimeException( BackendRuntimeException.InternalServerError, "error creating jedis connection " + profile.toString(), e); } jedis.select(profile.getDatabaseIndex()); return jedis; }
/** * renew old jedis connection * * @param old * @return */ public static RedisClient reNewRedisClient(RedisClient old) throws RedisException { Jedis jedis = null; RedisClientProfile profile = old.getProfile(); try { old.getJedis().disconnect(); } catch (Exception ignore) { logger.error("error closing old redis " + profile.toString()); } try { jedis = createJedis(old.getProfile()); } catch (Exception e) { logger.error("error creating jedis: " + profile.toString()); throw new RedisException( BackendRuntimeException.InternalServerError, "error renewing jedis: " + profile.toString(), e); } old.setJedis(jedis); return old; }
/** * create redis client * * @param profile * @return */ public static RedisClient createRedisClient(RedisClientProfile profile) { RedisClient redisClient = new RedisClient(); redisClient.setProfile(profile); Jedis jedis = null; try { jedis = createJedis(profile); } catch (Exception e) { throw new RedisException( BackendRuntimeException.InternalServerError, "error creating jedis: " + profile.toString(), e); } redisClient.setJedis(jedis); return redisClient; }