Remove passing both connection info and pool to LettuceConnFactory
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.redis.connection.lettuce;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.pool.BasePoolableObjectFactory;
|
||||
import org.apache.commons.pool.impl.GenericObjectPool;
|
||||
import org.apache.commons.pool.impl.GenericObjectPool.Config;
|
||||
import org.springframework.data.redis.connection.PoolException;
|
||||
|
||||
import com.lambdaworks.redis.RedisAsyncConnection;
|
||||
import com.lambdaworks.redis.RedisClient;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link LettucePool}
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
*
|
||||
*/
|
||||
public class DefaultLettucePool implements LettucePool {
|
||||
private final GenericObjectPool internalPool;
|
||||
private static final int DEFAULT_TIMEOUT = (int) TimeUnit.MILLISECONDS.convert(60, TimeUnit.SECONDS);
|
||||
private RedisClient client;
|
||||
|
||||
/**
|
||||
* Uses the {@link Config} and {@link RedisClient} defaults for configuring
|
||||
* the connection pool
|
||||
*
|
||||
* @param hostName
|
||||
* The Redis host
|
||||
* @param port
|
||||
* The Redis port
|
||||
*/
|
||||
public DefaultLettucePool(String hostName, int port) {
|
||||
this(hostName, port, 0, DEFAULT_TIMEOUT, new Config());
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the {@link RedisClient} defaults for configuring the connection pool
|
||||
*
|
||||
* @param hostName
|
||||
* The Redis host
|
||||
* @param port
|
||||
* The Redis port
|
||||
* @param poolConfig
|
||||
* The pool {@link Config}
|
||||
*/
|
||||
public DefaultLettucePool(String hostName, int port, Config poolConfig) {
|
||||
this(hostName, port, 0, DEFAULT_TIMEOUT, poolConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Uses the {@link Config} defaults for configuring the connection pool
|
||||
*
|
||||
* @param client
|
||||
* The {@link RedisClient} for connecting to Redis
|
||||
*
|
||||
*/
|
||||
public DefaultLettucePool(RedisClient client) {
|
||||
this.client = client;
|
||||
this.internalPool = new GenericObjectPool(new LettuceFactory(client, 0), new Config());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param client
|
||||
* The {@link RedisClient} for connecting to Redis
|
||||
*
|
||||
* @param poolConfig
|
||||
* The pool {@link Config}
|
||||
* @param dbIndex
|
||||
* The index of the database all connections should use. The
|
||||
* database will be selected when connections are activated.
|
||||
*/
|
||||
public DefaultLettucePool(RedisClient client, Config poolConfig, int dbIndex) {
|
||||
this.client = client;
|
||||
this.internalPool = new GenericObjectPool(new LettuceFactory(client, dbIndex), poolConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the {@link Config} defaults for configuring the connection pool
|
||||
*
|
||||
* @param hostName
|
||||
* The Redis host
|
||||
* @param port
|
||||
* The Redis port
|
||||
* @param dbIndex
|
||||
* The index of the database all connections should use. The
|
||||
* database will be selected when connections are activated.
|
||||
* @param password
|
||||
* The password used for authenticating with the Redis server or
|
||||
* null if no password required
|
||||
* @param timeout
|
||||
* The socket timeout or 0 to use the default socket timeout
|
||||
*/
|
||||
public DefaultLettucePool(String hostName, int port, int dbIndex, int timeout) {
|
||||
this(hostName, port, dbIndex, timeout, new Config());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param hostName
|
||||
* The Redis host
|
||||
* @param port
|
||||
* The Redis port
|
||||
* @param dbIndex
|
||||
* The index of the database all connections should use. The
|
||||
* database will be selected when connections are activated.
|
||||
* @param password
|
||||
* The password used for authenticating with the Redis server or
|
||||
* null if no password required
|
||||
* @param timeout
|
||||
* The socket timeout or 0 to use the default socket timeout
|
||||
* @param poolConfig
|
||||
* The pool {@link COnfig}
|
||||
*/
|
||||
public DefaultLettucePool(String hostName, int port, int dbIndex, int timeout, Config poolConfig) {
|
||||
this.client = new RedisClient(hostName, port);
|
||||
client.setDefaultTimeout(timeout, TimeUnit.MILLISECONDS);
|
||||
this.internalPool = new GenericObjectPool(new LettuceFactory(client, dbIndex), poolConfig);
|
||||
}
|
||||
|
||||
public RedisClient getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public RedisAsyncConnection<byte[], byte[]> getResource() {
|
||||
try {
|
||||
return (RedisAsyncConnection<byte[], byte[]>) internalPool.borrowObject();
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not get a resource from the pool", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void returnBrokenResource(final RedisAsyncConnection<byte[], byte[]> resource) {
|
||||
try {
|
||||
internalPool.invalidateObject(resource);
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not invalidate the broken resource", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void returnResource(final RedisAsyncConnection<byte[], byte[]> resource) {
|
||||
try {
|
||||
internalPool.returnObject(resource);
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not return the resource to the pool", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
try {
|
||||
internalPool.close();
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not destroy the pool", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static class LettuceFactory extends BasePoolableObjectFactory {
|
||||
|
||||
private final RedisClient client;
|
||||
|
||||
private int dbIndex;
|
||||
|
||||
public LettuceFactory(RedisClient client, int dbIndex) {
|
||||
super();
|
||||
this.client = client;
|
||||
this.dbIndex = dbIndex;
|
||||
}
|
||||
|
||||
public Object makeObject() throws Exception {
|
||||
return client.connectAsync(LettuceUtils.CODEC);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public void activateObject(Object obj) throws Exception {
|
||||
if (obj instanceof RedisAsyncConnection) {
|
||||
((RedisAsyncConnection) obj).select(dbIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void destroyObject(final Object obj) throws Exception {
|
||||
if (obj instanceof RedisAsyncConnection) {
|
||||
try {
|
||||
((RedisAsyncConnection) obj).close();
|
||||
} catch (Exception e) {
|
||||
// Errors may happen if returning a broken resource
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public boolean validateObject(final Object obj) {
|
||||
if (obj instanceof RedisAsyncConnection) {
|
||||
try {
|
||||
((RedisAsyncConnection) obj).ping();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,6 @@ import org.springframework.data.redis.RedisConnectionFailureException;
|
||||
import org.springframework.data.redis.RedisSystemException;
|
||||
import org.springframework.data.redis.connection.DataType;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
import org.springframework.data.redis.connection.Pool;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisPipelineException;
|
||||
import org.springframework.data.redis.connection.RedisSubscribedConnectionException;
|
||||
@@ -82,26 +81,57 @@ public class LettuceConnection implements RedisConnection {
|
||||
private List<Command<?, ?, ?>> ppline;
|
||||
private RedisClient client;
|
||||
private volatile LettuceSubscription subscription;
|
||||
private Pool<RedisAsyncConnection<byte[], byte[]>> pool;
|
||||
private LettucePool pool;
|
||||
/** flag indicating whether the connection needs to be dropped or not */
|
||||
private boolean broken = false;
|
||||
|
||||
/**
|
||||
* Instantiates a new lettuce connection.
|
||||
*
|
||||
* @param dedicatedConnection
|
||||
* A dedicated native connection. Can be used for any operation.
|
||||
* @param timeout
|
||||
* The connection timeout (in milliseconds)
|
||||
* @param client
|
||||
* The {@link RedisClient} to use when making pub/sub connections
|
||||
*/
|
||||
public LettuceConnection(com.lambdaworks.redis.RedisAsyncConnection<byte[], byte[]> dedicatedConnection, long timeout,
|
||||
RedisClient client) {
|
||||
this(null, dedicatedConnection, timeout, client, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new lettuce connection.
|
||||
*
|
||||
* @param dedicatedConnection
|
||||
* A dedicated native connection. Can be used for any operation.
|
||||
* @param timeout
|
||||
* The connection timeout (in milliseconds)
|
||||
* @param client
|
||||
* The {@link RedisClient} to use when making pub/sub connections
|
||||
* @param pool
|
||||
* An optional connection pool, from which the dedicated
|
||||
* connection came. If pool is set, the dedicated connection will
|
||||
* be returned to the pool on close
|
||||
*/
|
||||
public LettuceConnection(com.lambdaworks.redis.RedisAsyncConnection<byte[], byte[]> dedicatedConnection, long timeout,
|
||||
RedisClient client, Pool<RedisAsyncConnection<byte[], byte[]>> pool) {
|
||||
RedisClient client, LettucePool pool) {
|
||||
this(null, dedicatedConnection, timeout, client, pool);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new lettuce connection.
|
||||
*
|
||||
* @param connection underlying Lettuce async connection
|
||||
* @param timeout the connection timeout (in milliseconds)
|
||||
* @param client The Lettuce client
|
||||
* @param sharedConnection
|
||||
* A native connection that is shared with other
|
||||
* {@link LettuceConnection}s. Should not be used for
|
||||
* transactions or blocking operations
|
||||
* @param dedicatedConnection
|
||||
* A dedicated native connection. Can be used for any operation.
|
||||
* @param timeout
|
||||
* The connection timeout (in milliseconds)
|
||||
* @param client
|
||||
* The {@link RedisClient} to use when making pub/sub connections
|
||||
*/
|
||||
public LettuceConnection(com.lambdaworks.redis.RedisAsyncConnection<byte[], byte[]> sharedConnection,
|
||||
com.lambdaworks.redis.RedisAsyncConnection<byte[], byte[]> dedicatedConnection, long timeout,
|
||||
@@ -112,15 +142,24 @@ public class LettuceConnection implements RedisConnection {
|
||||
/**
|
||||
* Instantiates a new lettuce connection.
|
||||
*
|
||||
* @param connection The underlying Lettuce async connection
|
||||
* @param timeout The connection timeout (in milliseconds)
|
||||
* @param client The Lettuce client
|
||||
* @param closeNativeConnection True if native connection should be called on {@link #close()}. This should
|
||||
* be set to false if the native connection is shared.
|
||||
* @param sharedConnection
|
||||
* A native connection that is shared with other
|
||||
* {@link LettuceConnection}s. Should not be used for
|
||||
* transactions or blocking operations
|
||||
* @param dedicatedConnection
|
||||
* A dedicated native connection. Can be used for any operation.
|
||||
* @param timeout
|
||||
* The connection timeout (in milliseconds)
|
||||
* @param client
|
||||
* The {@link RedisClient} to use when making pub/sub connections
|
||||
* @param pool
|
||||
* An optional connection pool, from which the dedicated
|
||||
* connection came. If pool is set, the dedicated connection will
|
||||
* be returned to the pool on close
|
||||
*/
|
||||
public LettuceConnection(com.lambdaworks.redis.RedisAsyncConnection<byte[], byte[]> sharedConnection,
|
||||
com.lambdaworks.redis.RedisAsyncConnection<byte[], byte[]> dedicatedConnection, long timeout,
|
||||
RedisClient client, Pool<RedisAsyncConnection<byte[], byte[]>> pool) {
|
||||
RedisClient client, LettucePool pool) {
|
||||
Assert.notNull(dedicatedConnection, "a valid dedicated connection is required");
|
||||
this.asyncSharedConn = sharedConnection;
|
||||
this.asyncDedicatedConn = dedicatedConnection;
|
||||
|
||||
@@ -66,7 +66,7 @@ public class LettuceConnectionFactory implements InitializingBean, DisposableBea
|
||||
private boolean validateConnection = false;
|
||||
private boolean shareNativeConnection = true;
|
||||
private RedisAsyncConnection<byte[], byte[]> connection;
|
||||
private Pool<RedisAsyncConnection<byte[], byte[]>> pool;
|
||||
private LettucePool pool;
|
||||
private int dbIndex = 0;
|
||||
/** Synchronization monitor for the shared Connection */
|
||||
private final Object connectionMonitor = new Object();
|
||||
@@ -87,15 +87,12 @@ public class LettuceConnectionFactory implements InitializingBean, DisposableBea
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public LettuceConnectionFactory(String host, int port, Pool<RedisAsyncConnection<byte[], byte[]>> pool) {
|
||||
this.hostName = host;
|
||||
this.port = port;
|
||||
public LettuceConnectionFactory(LettucePool pool) {
|
||||
this.pool = pool;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() {
|
||||
client = new RedisClient(hostName, port);
|
||||
client.setDefaultTimeout(timeout, TimeUnit.MILLISECONDS);
|
||||
this.client = createRedisClient();
|
||||
resetConnection();
|
||||
if (shareNativeConnection) {
|
||||
initConnection();
|
||||
@@ -312,4 +309,13 @@ public class LettuceConnectionFactory implements InitializingBean, DisposableBea
|
||||
getHostName() + ":" + getPort(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private RedisClient createRedisClient() {
|
||||
if(pool != null) {
|
||||
return pool.getClient();
|
||||
}
|
||||
RedisClient client = new RedisClient(hostName, port);
|
||||
client.setDefaultTimeout(timeout, TimeUnit.MILLISECONDS);
|
||||
return client;
|
||||
}
|
||||
}
|
||||
@@ -13,206 +13,27 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.redis.connection.lettuce;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.pool.BasePoolableObjectFactory;
|
||||
import org.apache.commons.pool.impl.GenericObjectPool;
|
||||
import org.apache.commons.pool.impl.GenericObjectPool.Config;
|
||||
import org.springframework.data.redis.connection.Pool;
|
||||
import org.springframework.data.redis.connection.PoolException;
|
||||
|
||||
import com.lambdaworks.redis.RedisAsyncConnection;
|
||||
import com.lambdaworks.redis.RedisClient;
|
||||
|
||||
/**
|
||||
* Lettuce implementation of {@link Pool}
|
||||
*
|
||||
*
|
||||
* Pool of Lettuce {@link RedisAsyncConnection}s
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class LettucePool implements Pool<RedisAsyncConnection<byte[], byte[]>> {
|
||||
private final GenericObjectPool internalPool;
|
||||
private static final int DEFAULT_TIMEOUT = (int) TimeUnit.MILLISECONDS.convert(60, TimeUnit.SECONDS);
|
||||
|
||||
/**
|
||||
* Uses the {@link Config} and {@link RedisClient} defaults for configuring
|
||||
* the connection pool
|
||||
*
|
||||
* @param hostName
|
||||
* The Redis host
|
||||
* @param port
|
||||
* The Redis port
|
||||
*/
|
||||
public LettucePool(String hostName, int port) {
|
||||
this(hostName, port, 0, DEFAULT_TIMEOUT, new Config());
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the {@link RedisClient} defaults for configuring the connection pool
|
||||
*
|
||||
* @param hostName
|
||||
* The Redis host
|
||||
* @param port
|
||||
* The Redis port
|
||||
* @param poolConfig
|
||||
* The pool {@link Config}
|
||||
*/
|
||||
public LettucePool(String hostName, int port, Config poolConfig) {
|
||||
this(hostName, port, 0, DEFAULT_TIMEOUT, poolConfig);
|
||||
}
|
||||
public interface LettucePool extends Pool<RedisAsyncConnection<byte[], byte[]>> {
|
||||
|
||||
/**
|
||||
*
|
||||
* Uses the {@link Config} defaults for configuring the connection pool
|
||||
*
|
||||
* @param client
|
||||
* The {@link RedisClient} for connecting to Redis
|
||||
*
|
||||
* @return The {@link RedisClient} used to create pooled connections
|
||||
*/
|
||||
public LettucePool(RedisClient client) {
|
||||
this.internalPool = new GenericObjectPool(new LettuceFactory(client, 0), new Config());
|
||||
}
|
||||
RedisClient getClient();
|
||||
|
||||
/**
|
||||
*
|
||||
* @param client
|
||||
* The {@link RedisClient} for connecting to Redis
|
||||
*
|
||||
* @param poolConfig
|
||||
* The pool {@link Config}
|
||||
* @param dbIndex
|
||||
* The index of the database all connections should use. The
|
||||
* database will be selected when connections are activated.
|
||||
*/
|
||||
public LettucePool(RedisClient client, Config poolConfig, int dbIndex) {
|
||||
this.internalPool = new GenericObjectPool(new LettuceFactory(client, dbIndex), poolConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the {@link Config} defaults for configuring the connection pool
|
||||
*
|
||||
* @param hostName
|
||||
* The Redis host
|
||||
* @param port
|
||||
* The Redis port
|
||||
* @param dbIndex
|
||||
* The index of the database all connections should use. The
|
||||
* database will be selected when connections are activated.
|
||||
* @param password
|
||||
* The password used for authenticating with the Redis server or
|
||||
* null if no password required
|
||||
* @param timeout
|
||||
* The socket timeout or 0 to use the default socket timeout
|
||||
*/
|
||||
public LettucePool(String hostName, int port, int dbIndex, int timeout) {
|
||||
this(hostName, port, dbIndex, timeout, new Config());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param hostName
|
||||
* The Redis host
|
||||
* @param port
|
||||
* The Redis port
|
||||
* @param dbIndex
|
||||
* The index of the database all connections should use. The
|
||||
* database will be selected when connections are activated.
|
||||
* @param password
|
||||
* The password used for authenticating with the Redis server or
|
||||
* null if no password required
|
||||
* @param timeout
|
||||
* The socket timeout or 0 to use the default socket timeout
|
||||
* @param poolConfig
|
||||
* The pool {@link COnfig}
|
||||
*/
|
||||
public LettucePool(String hostName, int port, int dbIndex, int timeout, Config poolConfig) {
|
||||
RedisClient client = new RedisClient(hostName, port);
|
||||
client.setDefaultTimeout(timeout, TimeUnit.MILLISECONDS);
|
||||
this.internalPool = new GenericObjectPool(new LettuceFactory(client, dbIndex), poolConfig);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public RedisAsyncConnection<byte[], byte[]> getResource() {
|
||||
try {
|
||||
return (RedisAsyncConnection<byte[], byte[]>) internalPool.borrowObject();
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not get a resource from the pool", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void returnBrokenResource(final RedisAsyncConnection<byte[], byte[]> resource) {
|
||||
try {
|
||||
internalPool.invalidateObject(resource);
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not invalidate the broken resource", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void returnResource(final RedisAsyncConnection<byte[], byte[]> resource) {
|
||||
try {
|
||||
internalPool.returnObject(resource);
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not return the resource to the pool", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
try {
|
||||
internalPool.close();
|
||||
} catch (Exception e) {
|
||||
throw new PoolException("Could not destroy the pool", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static class LettuceFactory extends BasePoolableObjectFactory {
|
||||
|
||||
private final RedisClient client;
|
||||
|
||||
private int dbIndex;
|
||||
|
||||
public LettuceFactory(RedisClient client, int dbIndex) {
|
||||
super();
|
||||
this.client = client;
|
||||
this.dbIndex = dbIndex;
|
||||
}
|
||||
|
||||
public Object makeObject() throws Exception {
|
||||
return client.connectAsync(LettuceUtils.CODEC);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public void activateObject(Object obj) throws Exception {
|
||||
if (obj instanceof RedisAsyncConnection) {
|
||||
((RedisAsyncConnection) obj).select(dbIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void destroyObject(final Object obj) throws Exception {
|
||||
if (obj instanceof RedisAsyncConnection) {
|
||||
try {
|
||||
((RedisAsyncConnection) obj).close();
|
||||
} catch (Exception e) {
|
||||
// Errors may happen if returning a broken resource
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public boolean validateObject(final Object obj) {
|
||||
if (obj instanceof RedisAsyncConnection) {
|
||||
try {
|
||||
((RedisAsyncConnection) obj).ping();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user