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.
1515 */
1616package 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 ;
2123import org .jredis .JRedis ;
2224import org .jredis .connector .Connection ;
2325import org .jredis .connector .Connection .Socket .Property ;
3234 * JRedis implementation of {@link Pool}
3335 *
3436 * @author Jennifer Hickey
37+ * @author Christoph Strobl
3538 */
3639public 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 */
4649public 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 ) {
5861this (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 */
6669public 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 */
8992public 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 ) {
102106ConnectionSpec connectionSpec = DefaultConnectionSpec .newSpec (hostName , port , dbIndex , null );
103107connectionSpec .setConnectionFlag (Connection .Flag .RELIABLE , false );
104108if (StringUtils .hasLength (password )) {
@@ -107,12 +111,12 @@ public JredisPool(String hostName, int port, int dbIndex, String password, int t
107111if (timeout > 0 ) {
108112connectionSpec .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
113117public JRedis getResource () {
114118try {
115- return ( JRedis ) internalPool .borrowObject ();
119+ return internalPool .borrowObject ();
116120} catch (Exception e ) {
117121throw 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
147151private final ConnectionSpec connectionSpec ;
148152
@@ -151,32 +155,34 @@ public JredisFactory(ConnectionSpec connectionSpec) {
151155this .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 ) {
177173return 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