聊聊LettucePoolingConnectionProvider
生活随笔
收集整理的這篇文章主要介紹了
聊聊LettucePoolingConnectionProvider
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
序
本文主要研究一下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.* <p />* 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<StatefulConnection<?, ?>, GenericObjectPool<StatefulConnection<?, ?>>> poolRef = new ConcurrentHashMap<>(32);private final Map<Class<?>, GenericObjectPool<StatefulConnection<?, ?>>> 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)*/@Overridepublic <T extends StatefulConnection<?, ?>> T getConnection(Class<T> connectionType) {GenericObjectPool<StatefulConnection<?, ?>> 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()*/@Overridepublic 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)*/@Overridepublic void release(StatefulConnection<?, ?> connection) {GenericObjectPool<StatefulConnection<?, ?>> 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()*/@Overridepublic 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
- Stateful Connections
總結
以上是生活随笔為你收集整理的聊聊LettucePoolingConnectionProvider的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Slog64_项目上线之ArthurSl
- 下一篇: ansible+powershell D