Java连接redis

一. 依赖包

jedis-2.1.0.jar   commons-pool-1.6.jar

二. 实例

// 连接参数
public
class RedisConfig { public static int Maxactive=0; public static int RetryNum=0; public static int Maxidle=0; public static int Maxwait=0; public static int Timeout=0;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> String ip=""<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> port=0<span style="color: rgba(0, 0, 0, 1)">;

}

 

public class RedisUtil  {  
</span><span style="color: rgba(0, 0, 255, 1)">protected</span> Logger log =<span style="color: rgba(0, 0, 0, 1)"> LoggerFactory.getLogger(getClass());  

</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> Map&lt;String,JedisPool&gt; maps  = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;String,JedisPool&gt;<span style="color: rgba(0, 0, 0, 1)">();  

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 
 * 私有构造器. 
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>  
<span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> RedisUtil() {  

}  


</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 
 * 获取连接池. 
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 连接池实例 
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>  
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> JedisPool getPool(String ip,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> port) {  
    String key </span>= ip+":" +<span style="color: rgba(0, 0, 0, 1)">port;  
    JedisPool pool </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;  
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">maps.containsKey(key)) {  
        JedisPoolConfig config </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig();  
        config.setMaxActive(RedisConfig.Maxactive);  
        config.setMaxIdle(RedisConfig.Maxidle);  
        config.setMaxWait(RedisConfig.Maxwait);
        config.setTestOnBorrow(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);  
        config.setTestOnReturn(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);  
        </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{    
            </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 
             *如果你遇到 java.net.SocketTimeoutException: Read timed out exception的异常信息 
             *请尝试在构造JedisPool的时候设置自己的超时值. JedisPool默认的超时时间是2秒(单位毫秒) 
             </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">  
            pool </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPool(config, ip, port,RedisConfig.Timeout);  
            maps.put(key, pool);  
        } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(Exception e) {  
            e.printStackTrace();  
        }  
    }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{  
        pool </span>=<span style="color: rgba(0, 0, 0, 1)"> maps.get(key);  
    }  
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> pool;  
}


</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> JedisPool getPool()
{
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> getPool(RedisConfig.ip,RedisConfig.port);
}


</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 
 *类级的内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例 
 *没有绑定关系,而且只有被调用到时才会装载,从而实现了延迟加载。 
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>  
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> RedisUtilHolder{  
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 
     * 静态初始化器,由JVM来保证线程安全 
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>  
    <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> RedisUtil instance = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RedisUtil();  
}  


</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 
 *当getInstance方法第一次被调用的时候,它第一次读取 
 *RedisUtilHolder.instance,导致RedisUtilHolder类得到初始化;而这个类在装载并被初始化的时候,会初始化它的静 
 *态域,从而创建RedisUtil的实例,由于是静态的域,因此只会在虚拟机装载类的时候初始化一次,并由虚拟机来保证它的线程安全性。 
 *这个模式的优势在于,getInstance方法并没有被同步,并且只是执行一个域的访问,因此延迟初始化并没有增加任何访问成本。 
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>  
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> RedisUtil getInstance() {  
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> RedisUtilHolder.instance;  
}  


</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 
 * 获取Redis实例. 
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> Redis工具类实例 
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>  
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> Jedis getJedis(String ip, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> port) {
    Jedis jedis </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">int</span> count = 0<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)"> {
        </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> getPool(ip, port).getResource();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> log.info("get redis master1!");</span>
        } <span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> log.error("get redis master1 failed!", e);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 销毁对象</span>

getPool(ip, port).returnBrokenResource(jedis);
}
count
++;
}
while (jedis == null && count < RedisConfig.RetryNum);
return jedis;
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">重载一下</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Jedis getJedis()
{
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> getJedis(RedisConfig.ip,RedisConfig.port);
}


</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 
 * 释放redis实例到连接池. 
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jedis redis实例 
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>  
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> closeJedis(Jedis jedis,String ip,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> port) {  
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(jedis != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {  
        getPool(ip,port).returnResource(jedis);  
    }  
}  


</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> closeJedis(Jedis jedis)
{
    closeJedis(jedis,RedisConfig.ip,RedisConfig.port);
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 退出然后关闭Jedis连接。如果Jedis为null则无动作。
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> closeJedisDirectly(Jedis jedis) {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> ((jedis != <span style="color: rgba(0, 0, 255, 1)">null</span>) &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> jedis.isConnected()) {
        </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                jedis.quit();
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
            }
            jedis.disconnect();
        } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
            
        }
    }
}

}