1
1
/*
2
- * Copyright 2011-2013 the original author or authors.
2
+ * Copyright 2011-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .springframework .data .redis .connection .jredis ;
17
17
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 ;
21
23
import org .jredis .JRedis ;
22
24
import org .jredis .connector .Connection ;
23
25
import org .jredis .connector .Connection .Socket .Property ;
32
34
* JRedis implementation of {@link Pool}
33
35
*
34
36
* @author Jennifer Hickey
37
+ * @author Christoph Strobl
35
38
*/
36
39
public class JredisPool implements Pool <JRedis > {
37
40
38
- private final GenericObjectPool internalPool ;
41
+ private final GenericObjectPool < JRedis > internalPool ;
39
42
40
43
/**
41
44
* Uses the {@link Config} and {@link ConnectionSpec} defaults for configuring the connection pool
@@ -44,7 +47,7 @@ public class JredisPool implements Pool<JRedis> {
44
47
* @param port The Redis port
45
48
*/
46
49
public JredisPool (String hostName , int port ) {
47
- this (hostName , port , 0 , null , 0 , new Config ());
50
+ this (hostName , port , 0 , null , 0 , new GenericObjectPoolConfig ());
48
51
}
49
52
50
53
/**
@@ -54,7 +57,7 @@ public JredisPool(String hostName, int port) {
54
57
* @param port The Redis port
55
58
* @param poolConfig The pool {@link Config}
56
59
*/
57
- public JredisPool (String hostName , int port , Config poolConfig ) {
60
+ public JredisPool (String hostName , int port , GenericObjectPoolConfig poolConfig ) {
58
61
this (hostName , port , 0 , null , 0 , poolConfig );
59
62
}
60
63
@@ -64,15 +67,15 @@ public JredisPool(String hostName, int port, Config poolConfig) {
64
67
* @param connectionSpec The {@link ConnectionSpec} for connecting to Redis
65
68
*/
66
69
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 ());
68
71
}
69
72
70
73
/**
71
74
* @param connectionSpec The {@link ConnectionSpec} for connecting to Redis
72
75
* @param poolConfig The pool {@link Config}
73
76
*/
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 );
76
79
}
77
80
78
81
/**
@@ -87,7 +90,7 @@ public JredisPool(ConnectionSpec connectionSpec, Config poolConfig) {
87
90
* @param timeout The socket timeout or 0 to use the default socket timeout
88
91
*/
89
92
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 ());
91
94
}
92
95
93
96
/**
@@ -98,7 +101,8 @@ public JredisPool(String hostName, int port, int dbIndex, String password, int t
98
101
* @param timeout The socket timeout or 0 to use the default socket timeout
99
102
* @param poolConfig The pool {@link Config}
100
103
*/
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 ) {
102
106
ConnectionSpec connectionSpec = DefaultConnectionSpec .newSpec (hostName , port , dbIndex , null );
103
107
connectionSpec .setConnectionFlag (Connection .Flag .RELIABLE , false );
104
108
if (StringUtils .hasLength (password )) {
@@ -107,12 +111,12 @@ public JredisPool(String hostName, int port, int dbIndex, String password, int t
107
111
if (timeout > 0 ) {
108
112
connectionSpec .setSocketProperty (Property .SO_TIMEOUT , timeout );
109
113
}
110
- this .internalPool = new GenericObjectPool (new JredisFactory (connectionSpec ), poolConfig );
114
+ this .internalPool = new GenericObjectPool < JRedis > (new JredisFactory (connectionSpec ), poolConfig );
111
115
}
112
116
113
117
public JRedis getResource () {
114
118
try {
115
- return ( JRedis ) internalPool .borrowObject ();
119
+ return internalPool .borrowObject ();
116
120
} catch (Exception e ) {
117
121
throw new PoolException ("Could not get a resource from the pool" , e );
118
122
}
@@ -142,7 +146,7 @@ public void destroy() {
142
146
}
143
147
}
144
148
145
- private static class JredisFactory extends BasePoolableObjectFactory {
149
+ private static class JredisFactory extends BasePooledObjectFactory < JRedis > {
146
150
147
151
private final ConnectionSpec connectionSpec ;
148
152
@@ -151,32 +155,34 @@ public JredisFactory(ConnectionSpec connectionSpec) {
151
155
this .connectionSpec = connectionSpec ;
152
156
}
153
157
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
165
164
}
166
165
}
167
166
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 ) {
177
173
return false ;
178
174
}
179
175
}
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
+ }
180
186
}
181
187
182
188
}
0 commit comments