Skip to content

Commit 408b52a

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
DATAREDIS-261 - Upgrade to commons-pool2.
'JRedisPool' and 'DefaultLettucePool' have been migrated to 'commons-pool2'. 'PoolConfig' has been deprecated, and will be removed in '1.4', as all required operations are available via 'GenericObjectPoolConfig'. Along the way a minor bug within 'LettuceConnection' has been resolved, which prevented 'RedisAsyncConnections' from being properly closed/returned to pool. Original Pull Request: #50
1 parent d116d38 commit 408b52a

File tree

10 files changed

+162
-176
lines changed

10 files changed

+162
-176
lines changed

build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ apply plugin: 'javadocHotfix'
2929
[compileJava, compileTestJava]*.options*.compilerArgs = ["-Xlint:-serial"]
3030
[compileJava, compileTestJava]*.options*.compilerArgs = ["-Xlint:-serial", "-Xlint:deprecation"]
3131

32+
configurations.all {
33+
resolutionStrategy {
34+
force 'org.apache.commons:commons-pool2:2.2'
35+
}
36+
}
37+
3238
// Common dependencies
3339
dependencies {
3440
// Logging
@@ -60,7 +66,7 @@ dependencies {
6066
compile("com.fasterxml.jackson.core:jackson-databind:$fasterXmlJacksonDatabindVersion", optional)
6167

6268
// Pool
63-
compile("commons-pool:commons-pool:1.5.6", optional)
69+
compile("org.apache.commons:commons-pool2:2.2", optional)
6470

6571
// Testing
6672
testCompile "junit:junit:$junitVersion"
Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,64 +15,22 @@
1515
*/
1616
package org.springframework.data.redis.connection;
1717

18-
import org.apache.commons.pool.impl.GenericObjectPool.Config;
18+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
1919

2020
/**
21-
* Subclass of {@link Config} that includes setters for instantiation in Spring
21+
* Subclass of {@link GenericObjectPoolConfig} that includes setters for instantiation in Spring
2222
*
2323
* @author Jennifer Hickey
24+
* @author Christoph Strobl
25+
* @deprecated use {@link GenericObjectPoolConfig} instead. Will be removed in {@literal 1.4}.
2426
*/
25-
public class PoolConfig extends Config {
27+
public class PoolConfig extends GenericObjectPoolConfig {
2628

2729
public PoolConfig() {
2830
super();
2931
}
3032

31-
public void setMaxIdle(int maxIdle) {
32-
this.maxIdle = maxIdle;
33-
}
34-
35-
public void setMinIdle(int minIdle) {
36-
this.minIdle = minIdle;
37-
}
38-
3933
public void setMaxActive(int maxActive) {
40-
this.maxActive = maxActive;
41-
}
42-
43-
public void setMaxWait(long maxWait) {
44-
this.maxWait = maxWait;
45-
}
46-
47-
public void setWhenExhaustedAction(byte whenExhaustedAction) {
48-
this.whenExhaustedAction = whenExhaustedAction;
49-
}
50-
51-
public void setTestOnBorrow(boolean testOnBorrow) {
52-
this.testOnBorrow = testOnBorrow;
53-
}
54-
55-
public void setTestOnReturn(boolean testOnReturn) {
56-
this.testOnReturn = testOnReturn;
57-
}
58-
59-
public void setTestWhileIdle(boolean testWhileIdle) {
60-
this.testWhileIdle = testWhileIdle;
61-
}
62-
63-
public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
64-
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
65-
}
66-
67-
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
68-
this.numTestsPerEvictionRun = numTestsPerEvictionRun;
69-
}
70-
71-
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
72-
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
73-
}
74-
75-
public void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis) {
76-
this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
34+
setMaxTotal(maxActive);
7735
}
7836
}

src/main/java/org/springframework/data/redis/connection/jredis/JredisPool.java

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,9 +15,11 @@
1515
*/
1616
package org.springframework.data.redis.connection.jredis;
1717

18-
import org.apache.commons.pool.BasePoolableObjectFactory;
19-
import org.apache.commons.pool.impl.GenericObjectPool;
20-
import org.apache.commons.pool.impl.GenericObjectPool.Config;
18+
import org.apache.commons.pool2.BasePooledObjectFactory;
19+
import org.apache.commons.pool2.PooledObject;
20+
import org.apache.commons.pool2.impl.DefaultPooledObject;
21+
import org.apache.commons.pool2.impl.GenericObjectPool;
22+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
2123
import org.jredis.JRedis;
2224
import org.jredis.connector.Connection;
2325
import org.jredis.connector.Connection.Socket.Property;
@@ -32,10 +34,11 @@
3234
* JRedis implementation of {@link Pool}
3335
*
3436
* @author Jennifer Hickey
37+
* @author Christoph Strobl
3538
*/
3639
public class JredisPool implements Pool<JRedis> {
3740

38-
private final GenericObjectPool internalPool;
41+
private final GenericObjectPool<JRedis> internalPool;
3942

4043
/**
4144
* Uses the {@link Config} and {@link ConnectionSpec} defaults for configuring the connection pool
@@ -44,7 +47,7 @@ public class JredisPool implements Pool<JRedis> {
4447
* @param port The Redis port
4548
*/
4649
public JredisPool(String hostName, int port) {
47-
this(hostName, port, 0, null, 0, new Config());
50+
this(hostName, port, 0, null, 0, new GenericObjectPoolConfig());
4851
}
4952

5053
/**
@@ -54,7 +57,7 @@ public JredisPool(String hostName, int port) {
5457
* @param port The Redis port
5558
* @param poolConfig The pool {@link Config}
5659
*/
57-
public JredisPool(String hostName, int port, Config poolConfig) {
60+
public JredisPool(String hostName, int port, GenericObjectPoolConfig poolConfig) {
5861
this(hostName, port, 0, null, 0, poolConfig);
5962
}
6063

@@ -64,15 +67,15 @@ public JredisPool(String hostName, int port, Config poolConfig) {
6467
* @param connectionSpec The {@link ConnectionSpec} for connecting to Redis
6568
*/
6669
public JredisPool(ConnectionSpec connectionSpec) {
67-
this.internalPool = new GenericObjectPool(new JredisFactory(connectionSpec), new Config());
70+
this.internalPool = new GenericObjectPool<JRedis>(new JredisFactory(connectionSpec), new GenericObjectPoolConfig());
6871
}
6972

7073
/**
7174
* @param connectionSpec The {@link ConnectionSpec} for connecting to Redis
7275
* @param poolConfig The pool {@link Config}
7376
*/
74-
public JredisPool(ConnectionSpec connectionSpec, Config poolConfig) {
75-
this.internalPool = new GenericObjectPool(new JredisFactory(connectionSpec), poolConfig);
77+
public JredisPool(ConnectionSpec connectionSpec, GenericObjectPoolConfig poolConfig) {
78+
this.internalPool = new GenericObjectPool<JRedis>(new JredisFactory(connectionSpec), poolConfig);
7679
}
7780

7881
/**
@@ -87,7 +90,7 @@ public JredisPool(ConnectionSpec connectionSpec, Config poolConfig) {
8790
* @param timeout The socket timeout or 0 to use the default socket timeout
8891
*/
8992
public JredisPool(String hostName, int port, int dbIndex, String password, int timeout) {
90-
this(hostName, port, dbIndex, password, timeout, new Config());
93+
this(hostName, port, dbIndex, password, timeout, new GenericObjectPoolConfig());
9194
}
9295

9396
/**
@@ -98,7 +101,8 @@ public JredisPool(String hostName, int port, int dbIndex, String password, int t
98101
* @param timeout The socket timeout or 0 to use the default socket timeout
99102
* @param poolConfig The pool {@link Config}
100103
*/
101-
public JredisPool(String hostName, int port, int dbIndex, String password, int timeout, Config poolConfig) {
104+
public JredisPool(String hostName, int port, int dbIndex, String password, int timeout,
105+
GenericObjectPoolConfig poolConfig) {
102106
ConnectionSpec connectionSpec = DefaultConnectionSpec.newSpec(hostName, port, dbIndex, null);
103107
connectionSpec.setConnectionFlag(Connection.Flag.RELIABLE, false);
104108
if (StringUtils.hasLength(password)) {
@@ -107,12 +111,12 @@ public JredisPool(String hostName, int port, int dbIndex, String password, int t
107111
if (timeout > 0) {
108112
connectionSpec.setSocketProperty(Property.SO_TIMEOUT, timeout);
109113
}
110-
this.internalPool = new GenericObjectPool(new JredisFactory(connectionSpec), poolConfig);
114+
this.internalPool = new GenericObjectPool<JRedis>(new JredisFactory(connectionSpec), poolConfig);
111115
}
112116

113117
public JRedis getResource() {
114118
try {
115-
return (JRedis) internalPool.borrowObject();
119+
return internalPool.borrowObject();
116120
} catch (Exception e) {
117121
throw new PoolException("Could not get a resource from the pool", e);
118122
}
@@ -142,7 +146,7 @@ public void destroy() {
142146
}
143147
}
144148

145-
private static class JredisFactory extends BasePoolableObjectFactory {
149+
private static class JredisFactory extends BasePooledObjectFactory<JRedis> {
146150

147151
private final ConnectionSpec connectionSpec;
148152

@@ -151,32 +155,34 @@ public JredisFactory(ConnectionSpec connectionSpec) {
151155
this.connectionSpec = connectionSpec;
152156
}
153157

154-
public Object makeObject() throws Exception {
155-
return new JRedisClient(connectionSpec);
156-
}
157-
158-
public void destroyObject(final Object obj) throws Exception {
159-
if (obj instanceof JRedis) {
160-
try {
161-
((JRedis) obj).quit();
162-
} catch (Exception e) {
163-
// Errors may happen if returning a broken resource
164-
}
158+
@Override
159+
public void destroyObject(final PooledObject<JRedis> obj) throws Exception {
160+
try {
161+
obj.getObject().quit();
162+
} catch (Exception e) {
163+
// Errors may happen if returning a broken resource
165164
}
166165
}
167166

168-
public boolean validateObject(final Object obj) {
169-
if (obj instanceof JRedis) {
170-
try {
171-
((JRedis) obj).ping();
172-
return true;
173-
} catch (Exception e) {
174-
return false;
175-
}
176-
} else {
167+
@Override
168+
public boolean validateObject(final PooledObject<JRedis> obj) {
169+
try {
170+
obj.getObject().ping();
171+
return true;
172+
} catch (Exception e) {
177173
return false;
178174
}
179175
}
176+
177+
@Override
178+
public JRedis create() throws Exception {
179+
return new JRedisClient(connectionSpec);
180+
}
181+
182+
@Override
183+
public PooledObject<JRedis> wrap(JRedis obj) {
184+
return new DefaultPooledObject<JRedis>(obj);
185+
}
180186
}
181187

182188
}

0 commit comments

Comments
 (0)