spring中集成使用jedis(2)
来源:互联网 发布:搜狗桌面整理软件 编辑:程序博客网 时间:2024/05/01 06:17
本文主要就spring注入的连接池使用问题,做简要说明。
使用过JedisPool的同学会发现,通过JedisPool获取资源,在使用完毕后,需要显式的将资源放回连接池中,
如下:
JedisPool jedisPool;Jedis jedis = jedisPool.getResource();//操作jedisPool.returnResource(jedis);如果未显示的回收资源,则在连接池中资源使用完毕后,系统会出现阻塞。
因为如果使用连接池,就相当于将客户端连接托管给池,而池中资源又是事先设定,及有限资源,
那么在资源都被占用,且无人回收时,就会出现一直阻塞等待资源。
但是在每个操作后都显示的调用资源回收,又显得代码臃肿,此处建议考虑通过动态代理实现,及在每次操作前
通过代理获取资源,操作后再将资源回收。代码设计如下:
public class RedisService implements IMemCached {/** * 连接池 */@Getter@Setterprivate JedisPool jedisPool;@Getter@Setter private Jedis jedis; /** * * @Title: initJedis * @Description: 从连接池获取连接 */public void initJedis(){ this.jedis = jedisPool.getResource(); }/** * * @Title: returnJedis * @Description: 将连接放回连接池 */ public void returnJedis(){ jedisPool.returnResource(jedis); } @Overridepublic boolean setValue(String key, Object value)throws MemcachedOpException {return OK.equals(jedis.set(key,null == value ? StringUtils.EMPTY : value.toString()));}@Overridepublic boolean setValue(String key, Object value, int expTime)throws MemcachedOpException {return OK.equals(jedis.setex(key,expTime,null == value ? StringUtils.EMPTY : value.toString()));}@Overridepublic Object getValue(String key) throws MemcachedOpException {return jedis.get(key);}@Overridepublic Object getValue(String key, long timeout)throws MemcachedOpException {return getValue(key);}public List<String> getValue(String... keys) throws MemcachedOpException {return jedis.mget(keys);} /** * * @Title: sadd * @Description: 添加一个或者多个元素到集合(set)里 * @param key * @param members * @return */ public boolean sadd(final String key,String... members) throws MemcachedOpException{return 1==jedis.sadd(key, members); } /** * * @Title: sRandomMember * @Description: 从集合里面随机获取一个值 * @param key * @return */ public String sRandomMember(final String key) throws MemcachedOpException{ return jedis.srandmember(key); }@Overridepublic IMemCached getProxy() {return new ProxyHandler().bind(this);}@Overridepublic void flush() throws MemcachedFlushException { jedis.flushDB();}}/** * 动态代理类 用于解决jedis需手工放回连接池的问题 * @author guozb * */class ProxyHandler implements InvocationHandler{ private RedisService redis; /** * * @Title: bind * @Description: 绑定代理对象 * @param imem * @return */ public IMemCached bind(RedisService imem){ this.redis = (RedisService) imem; return (IMemCached)Proxy.newProxyInstance(RedisService.class.getClassLoader(), RedisService.class.getInterfaces(), this); }@Overridepublic Object invoke(Object arg0, Method arg1, Object[] arg2)throws Throwable {//从连接池获取资源redis.initJedis();Object obj = arg1.invoke(redis, arg2);//将资源放回连接池redis.returnJedis();return obj;}}
这样在使用时,通过调用getProxy()进行具体操作即可。
0 0
- spring中集成使用jedis(2)
- spring中集成使用jedis(1)
- 使用Spring + Jedis集成Redis
- 使用Spring + Jedis集成Redis
- 使用Spring + Jedis集成Redis
- Spring中简单使用jedis
- spring集成jedis
- Spring 集成jedis
- Jedis+spring集成
- Spring Jedis集成Redis
- Spring + Jedis集成Redis
- Spring 集成 Jedis
- spring boot中使用redis -Jedis
- Spring与Jedis的集成
- Spring与Jedis的集成
- spring集成Jedis Cluster配置
- Spring boot上集成Jedis
- redis 学习笔记(5)-Spring与Jedis的集成
- jemalloc
- JAVA中各种去除空格
- javascript——从「最被误解的语言」到「最流行的语言」
- CreateEvent() SetEvent() ResetEvent() 进程互斥
- 最小生成树-并查集-Kruskal-zoj-2048-special judge
- spring中集成使用jedis(2)
- Apache 代理(Proxy) 转发请求
- MFC下DLL/lib的调用
- delphi打造气泡窗口的实现
- 手动实现多任务调度控制器
- VS.NET打印页面设置(PageSetupDialog)页边距总是改变
- 剑指Offer:替换空格
- asp.net 中设置静态内容缓存时间
- hdu 1176