@@ -69,18 +69,52 @@ public abstract class ConnectionPoolSupport {
69
69
private ConnectionPoolSupport () {
70
70
}
71
71
72
+ /**
73
+ * Creates a new {@link GenericObjectPool} using the {@link Supplier}. Allocated instances are wrapped and must not be
74
+ * returned with {@link ObjectPool#returnObject(Object)}. By default, connections are validated by checking their
75
+ * {@link StatefulConnection#isOpen()} method.
76
+ *
77
+ * @param connectionSupplier must not be {@code null}.
78
+ * @param config must not be {@code null}.
79
+ * @param <T> connection type.
80
+ * @return the connection pool.
81
+ */
82
+ public static <T extends StatefulConnection <?, ?>> GenericObjectPool <T > createGenericObjectPool (
83
+ Supplier <T > connectionSupplier , GenericObjectPoolConfig <T > config ) {
84
+ return createGenericObjectPool (connectionSupplier , config , true , (c ) -> c .isOpen ());
85
+ }
86
+
72
87
/**
73
88
* Creates a new {@link GenericObjectPool} using the {@link Supplier}. Allocated instances are wrapped and must not be
74
89
* returned with {@link ObjectPool#returnObject(Object)}.
75
90
*
76
91
* @param connectionSupplier must not be {@code null}.
77
92
* @param config must not be {@code null}.
93
+ * @param validationPredicate a {@link Predicate} to help validate connections
78
94
* @param <T> connection type.
79
95
* @return the connection pool.
80
96
*/
81
97
public static <T extends StatefulConnection <?, ?>> GenericObjectPool <T > createGenericObjectPool (
82
- Supplier <T > connectionSupplier , GenericObjectPoolConfig <T > config , Predicate <T > connectionValidator ) {
83
- return createGenericObjectPool (connectionSupplier , config , true , connectionValidator );
98
+ Supplier <T > connectionSupplier , GenericObjectPoolConfig <T > config , Predicate <T > validationPredicate ) {
99
+ return createGenericObjectPool (connectionSupplier , config , true , validationPredicate );
100
+ }
101
+
102
+ /**
103
+ * Creates a new {@link GenericObjectPool} using the {@link Supplier}. By default, connections are validated by checking
104
+ * their {@link StatefulConnection#isOpen()} method.
105
+ *
106
+ * @param connectionSupplier must not be {@code null}.
107
+ * @param config must not be {@code null}.
108
+ * @param wrapConnections {@code false} to return direct connections that need to be returned to the pool using
109
+ * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connections that are returned to the pool
110
+ * when invoking {@link StatefulConnection#close()}.
111
+ * @param <T> connection type.
112
+ * @return the connection pool.
113
+ */
114
+ @ SuppressWarnings ("unchecked" )
115
+ public static <T extends StatefulConnection <?, ?>> GenericObjectPool <T > createGenericObjectPool (
116
+ Supplier <T > connectionSupplier , GenericObjectPoolConfig <T > config , boolean wrapConnections ) {
117
+ return createGenericObjectPool (connectionSupplier , config , wrapConnections , (c ) -> c .isOpen ());
84
118
}
85
119
86
120
/**
@@ -89,24 +123,25 @@ private ConnectionPoolSupport() {
89
123
* @param connectionSupplier must not be {@code null}.
90
124
* @param config must not be {@code null}.
91
125
* @param wrapConnections {@code false} to return direct connections that need to be returned to the pool using
92
- * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connection that are returned to the pool
126
+ * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connections that are returned to the pool
93
127
* when invoking {@link StatefulConnection#close()}.
128
+ * @param validationPredicate a {@link Predicate} to help validate connections
94
129
* @param <T> connection type.
95
130
* @return the connection pool.
96
131
*/
97
132
@ SuppressWarnings ("unchecked" )
98
133
public static <T extends StatefulConnection <?, ?>> GenericObjectPool <T > createGenericObjectPool (
99
134
Supplier <T > connectionSupplier , GenericObjectPoolConfig <T > config , boolean wrapConnections ,
100
- Predicate <T > connectionValidator ) {
135
+ Predicate <T > validationPredicate ) {
101
136
102
137
LettuceAssert .notNull (connectionSupplier , "Connection supplier must not be null" );
103
138
LettuceAssert .notNull (config , "GenericObjectPoolConfig must not be null" );
104
- LettuceAssert .notNull (connectionValidator , "Connection validator must not be null" );
139
+ LettuceAssert .notNull (validationPredicate , "Connection validator must not be null" );
105
140
106
141
AtomicReference <Origin <T >> poolRef = new AtomicReference <>();
107
142
108
143
GenericObjectPool <T > pool = new GenericObjectPool <T >(
109
- new EnhancedRedisPooledObjectFactory < T >(connectionSupplier , connectionValidator ), config ) {
144
+ new RedisPooledObjectFactory < >(connectionSupplier , validationPredicate ), config ) {
110
145
111
146
@ Override
112
147
public T borrowObject () throws Exception {
@@ -149,20 +184,38 @@ public void returnObject(T obj) {
149
184
*
150
185
* @param connectionSupplier must not be {@code null}.
151
186
* @param wrapConnections {@code false} to return direct connections that need to be returned to the pool using
152
- * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connection that are returned to the pool
187
+ * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connections that are returned to the pool
153
188
* when invoking {@link StatefulConnection#close()}.
154
189
* @param <T> connection type.
155
190
* @return the connection pool.
156
191
*/
157
192
@ SuppressWarnings ("unchecked" )
158
193
public static <T extends StatefulConnection <?, ?>> SoftReferenceObjectPool <T > createSoftReferenceObjectPool (
159
194
Supplier <T > connectionSupplier , boolean wrapConnections ) {
195
+ return createSoftReferenceObjectPool (connectionSupplier , wrapConnections , (c ) -> c .isOpen ());
196
+ }
197
+
198
+ /**
199
+ * Creates a new {@link SoftReferenceObjectPool} using the {@link Supplier}.
200
+ *
201
+ * @param connectionSupplier must not be {@code null}.
202
+ * @param wrapConnections {@code false} to return direct connections that need to be returned to the pool using
203
+ * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connections that are returned to the pool
204
+ * when invoking {@link StatefulConnection#close()}.
205
+ * @param validationPredicate a {@link Predicate} to help validate connections
206
+ * @param <T> connection type.
207
+ * @return the connection pool.
208
+ */
209
+ @ SuppressWarnings ("unchecked" )
210
+ public static <T extends StatefulConnection <?, ?>> SoftReferenceObjectPool <T > createSoftReferenceObjectPool (
211
+ Supplier <T > connectionSupplier , boolean wrapConnections , Predicate <T > validationPredicate ) {
160
212
161
213
LettuceAssert .notNull (connectionSupplier , "Connection supplier must not be null" );
162
214
163
215
AtomicReference <Origin <T >> poolRef = new AtomicReference <>();
164
216
165
- SoftReferenceObjectPool <T > pool = new SoftReferenceObjectPool <T >(new RedisPooledObjectFactory <>(connectionSupplier )) {
217
+ SoftReferenceObjectPool <T > pool = new SoftReferenceObjectPool <T >(
218
+ new RedisPooledObjectFactory <>(connectionSupplier , validationPredicate )) {
166
219
167
220
private final Lock lock = new ReentrantLock ();
168
221
@@ -205,8 +258,11 @@ private static class RedisPooledObjectFactory<T extends StatefulConnection<?, ?>
205
258
206
259
private final Supplier <T > connectionSupplier ;
207
260
208
- RedisPooledObjectFactory (Supplier <T > connectionSupplier ) {
261
+ private final Predicate <T > validationPredicate ;
262
+
263
+ RedisPooledObjectFactory (Supplier <T > connectionSupplier , Predicate <T > validationPredicate ) {
209
264
this .connectionSupplier = connectionSupplier ;
265
+ this .validationPredicate = validationPredicate ;
210
266
}
211
267
212
268
@ Override
@@ -226,7 +282,7 @@ public PooledObject<T> wrap(T obj) {
226
282
227
283
@ Override
228
284
public boolean validateObject (PooledObject <T > p ) {
229
- return p .getObject (). isOpen ( );
285
+ return this . validationPredicate . test ( p .getObject ());
230
286
}
231
287
232
288
}
@@ -254,43 +310,4 @@ public CompletableFuture<Void> returnObjectAsync(T o) throws Exception {
254
310
255
311
}
256
312
257
- private static class EnhancedRedisPooledObjectFactory <T extends StatefulConnection <?, ?>>
258
- extends BasePooledObjectFactory <T > {
259
-
260
- private final Supplier <T > connectionSupplier ;
261
-
262
- private final Predicate <T > connectionValidator ;
263
-
264
- EnhancedRedisPooledObjectFactory (Supplier <T > connectionSupplier , Predicate <T > connectionValidator ) {
265
- this .connectionSupplier = connectionSupplier ;
266
- this .connectionValidator = connectionValidator ;
267
- }
268
-
269
- @ Override
270
- public T create () throws Exception {
271
- return connectionSupplier .get ();
272
- }
273
-
274
- @ Override
275
- public PooledObject <T > wrap (T obj ) {
276
- return new DefaultPooledObject <>(obj );
277
- }
278
-
279
- @ Override
280
- public boolean validateObject (PooledObject <T > p ) {
281
- T connection = p .getObject ();
282
- return connection .isOpen () && connectionValidator .test (connection );
283
- }
284
-
285
- @ Override
286
- public void destroyObject (PooledObject <T > p ) throws Exception {
287
- try {
288
- p .getObject ().close ();
289
- } catch (Exception e ) {
290
- e .printStackTrace ();
291
- }
292
- }
293
-
294
- }
295
-
296
313
}
0 commit comments