public interface IJedisClientFactory { Jedis getJedis(); }
JedisClientFactoryImpl.java
@Service public class JedisClientFactoryImpl implements IJedisClientFactory { private final JedisPool pool; private final String password; public JedisClientFactoryImpl() { JedisPoolConfig config = new JedisPoolConfig(); config.setBlockWhenExhausted(false); config.setMaxTotal(150); config.setMaxIdle(150); config.setMinIdle(100); config.setMaxWaitMillis(2000); config.setMinEvictableIdleTimeMillis(300000); config.setTestOnBorrow(false); InputStream inputStream = this.getClass().getResourceAsStream("/jedis.properties"); Properties properties = new Properties(); try { properties.load(inputStream); } catch (IOException e) { e.printStackTrace(); } String url = (String)properties.get("url"); int port = Integer.parseInt((String)properties.get("port")); int timeout = Integer.parseInt((String)properties.get("timeout")); password = (String)properties.get("password"); pool = new JedisPool(config, url,port,timeout); } @Override public synchronized Jedis getJedis() { Jedis resource = pool.getResource(); resource.auth(password); return resource; } @PreDestroy public void closePool(){ pool.destroy(); } }