博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊LettucePoolingConnectionProvider
阅读量:5917 次
发布时间:2019-06-19

本文共 4296 字,大约阅读时间需要 14 分钟。

  hot3.png

本文主要研究一下LettucePoolingConnectionProvider

LettucePoolingConnectionProvider

spring-data-redis-2.0.10.RELEASE-sources.jar!/org/springframework/data/redis/connection/lettuce/LettucePoolingConnectionProvider.java

/** * {@link LettuceConnectionProvider} with connection pooling support. This connection provider holds multiple pools (one * per connection type) for contextualized connection allocation. * 

* Each allocated connection is tracked and to be returned into the pool which created the connection. Instances of this * class require {@link #destroy() disposal} to de-allocate lingering connections that were not returned to the pool and * to close the pools. * * @author Mark Paluch * @author Christoph Strobl * @since 2.0 * @see #getConnection(Class) */class LettucePoolingConnectionProvider implements LettuceConnectionProvider, RedisClientProvider, DisposableBean { private final static Log log = LogFactory.getLog(LettucePoolingConnectionProvider.class); private final LettuceConnectionProvider connectionProvider; private final GenericObjectPoolConfig poolConfig; private final Map
, GenericObjectPool
>> poolRef = new ConcurrentHashMap<>( 32); private final Map
, GenericObjectPool
>> pools = new ConcurrentHashMap<>(32); LettucePoolingConnectionProvider(LettuceConnectionProvider connectionProvider, LettucePoolingClientConfiguration clientConfiguration) { Assert.notNull(connectionProvider, "ConnectionProvider must not be null!"); Assert.notNull(clientConfiguration, "ClientConfiguration must not be null!"); this.connectionProvider = connectionProvider; this.poolConfig = clientConfiguration.getPoolConfig(); } /* * (non-Javadoc) * @see org.springframework.data.redis.connection.lettuce.LettuceConnectionProvider#getConnection(java.lang.Class) */ @Override public
> T getConnection(Class
connectionType) { GenericObjectPool
> pool = pools.computeIfAbsent(connectionType, poolType -> { return ConnectionPoolSupport.createGenericObjectPool(() -> connectionProvider.getConnection(connectionType), poolConfig, false); }); try { StatefulConnection
connection = pool.borrowObject(); poolRef.put(connection, pool); return connectionType.cast(connection); } catch (Exception e) { throw new PoolException("Could not get a resource from the pool", e); } } /* * (non-Javadoc) * @see org.springframework.data.redis.connection.lettuce.RedisClientProvider#getRedisClient() */ @Override public AbstractRedisClient getRedisClient() { if (connectionProvider instanceof RedisClientProvider) { return ((RedisClientProvider) connectionProvider).getRedisClient(); } throw new IllegalStateException( String.format("Underlying connection provider %s does not implement RedisClientProvider!", connectionProvider.getClass().getName())); } /* * (non-Javadoc) * @see org.springframework.data.redis.connection.lettuce.LettuceConnectionProvider#release(io.lettuce.core.api.StatefulConnection) */ @Override public void release(StatefulConnection
connection) { GenericObjectPool
> pool = poolRef.remove(connection); if (pool == null) { throw new PoolException("Returned connection " + connection + " was either previously returned or does not belong to this connection provider"); } pool.returnObject(connection); } /* * (non-Javadoc) * @see org.springframework.beans.factory.DisposableBean#destroy() */ @Override public void destroy() throws Exception { if (!poolRef.isEmpty()) { log.warn("LettucePoolingConnectionProvider contains unreleased connections"); poolRef.forEach((connection, pool) -> pool.returnObject(connection)); poolRef.clear(); } pools.forEach((type, pool) -> pool.close()); pools.clear(); }}
  • 这个provider与普通的连接不同,它区分了不同类型的连接池,因而pools是map结构的,而且还多了poolRef这个map来维护每个连接所在连接池
  • pools主要是用于根据连接类型获取对应连接池,用于连接的借用场景,而poolRef主要是用于连接的归还场景
  • 这里采用了ConcurrentHashMap的computeIfAbsent方法,对于key不存在的则触发创建并返回,对于key存在的则直接返回

小结

  • lettuce 4.0版本引入了StatefulConnection,它会维护登录、事务、所选数据库,读写等状态
  • StatefulConnection有几个类型,分别是StatefulRedisConnection(StatefulRedisPubSubConnection、StatefulRedisMasterSlaveConnection)、StatefulRedisSentinelConnection、StatefulRedisClusterConnection
  • LettucePoolingConnectionProvider维护了多类型的连接的连接池,因而采用map的数据结构,不然单一类型的连接直接使用GenericObjectPool即可

doc

转载于:https://my.oschina.net/go4it/blog/2052363

你可能感兴趣的文章
WEB前端研发工程师编程能力成长之路
查看>>
前端学PHP之文件操作
查看>>
LeetCode | Copy List with Random Pointer
查看>>
LeetCode | Regular Expression Matching
查看>>
C语言博客05--指针
查看>>
十四、过滤函数-筛选对象集合
查看>>
Hamburger
查看>>
Spring MVC--第一个程序
查看>>
linux- 监控命令
查看>>
hdoj 题目分类
查看>>
询问Spring Bott和高并发框架两个问题
查看>>
正则表达式验证可发短信的号码,如手机号和小灵通号码(106+区号+号码)
查看>>
Java EE (7) -- Java EE 6 Enterprise Architect Certified Master(1z0-807)
查看>>
APK签名_资料
查看>>
WebView.简单使用_ZC代码
查看>>
记录一次由屁股决定研发的狗血经历
查看>>
同步对象event
查看>>
详解Jedis连接池报错处理
查看>>
时间相关
查看>>
stl的优先级队列
查看>>