11package redis .clients .jedis ;
22
33import java .net .URI ;
4- import java .util .concurrent .atomic .AtomicReference ;
54
65import javax .net .ssl .HostnameVerifier ;
76import javax .net .ssl .SSLParameters ;
@@ -24,7 +23,7 @@ public class JedisFactory implements PooledObjectFactory<Jedis> {
2423
2524 private static final Logger logger = LoggerFactory .getLogger (JedisFactory .class );
2625
27- private final AtomicReference < HostAndPort > hostAndPort = new AtomicReference <>() ;
26+ private final JedisSocketFactory jedisSocketFactory ;
2827
2928 private final JedisClientConfig clientConfig ;
3029
@@ -66,20 +65,25 @@ protected JedisFactory(final String host, final int port, final int connectionTi
6665 }
6766
6867 protected JedisFactory (final HostAndPort hostAndPort , final JedisClientConfig clientConfig ) {
69- this .hostAndPort .set (hostAndPort );
7068 this .clientConfig = DefaultJedisClientConfig .copyConfig (clientConfig );
69+ this .jedisSocketFactory = new DefaultJedisSocketFactory (hostAndPort , this .clientConfig );
7170 }
7271
7372 protected JedisFactory (final String host , final int port , final int connectionTimeout , final int soTimeout ,
7473 final int infiniteSoTimeout , final String user , final String password , final int database ,
7574 final String clientName , final boolean ssl , final SSLSocketFactory sslSocketFactory ,
7675 final SSLParameters sslParameters , final HostnameVerifier hostnameVerifier ) {
77- this .hostAndPort .set (new HostAndPort (host , port ));
7876 this .clientConfig = DefaultJedisClientConfig .builder ().connectionTimeoutMillis (connectionTimeout )
7977 .socketTimeoutMillis (soTimeout ).blockingSocketTimeoutMillis (infiniteSoTimeout ).user (user )
8078 .password (password ).database (database ).clientName (clientName )
8179 .ssl (ssl ).sslSocketFactory (sslSocketFactory )
8280 .sslParameters (sslParameters ).hostnameVerifier (hostnameVerifier ).build ();
81+ this .jedisSocketFactory = new DefaultJedisSocketFactory (new HostAndPort (host , port ), this .clientConfig );
82+ }
83+
84+ protected JedisFactory (final JedisSocketFactory jedisSocketFactory , final JedisClientConfig clientConfig ) {
85+ this .clientConfig = DefaultJedisClientConfig .copyConfig (clientConfig );
86+ this .jedisSocketFactory = jedisSocketFactory ;
8387 }
8488
8589 /**
@@ -100,6 +104,7 @@ protected JedisFactory(final int connectionTimeout, final int soTimeout, final i
100104 */
101105 protected JedisFactory (final JedisClientConfig clientConfig ) {
102106 this .clientConfig = clientConfig ;
107+ this .jedisSocketFactory = new DefaultJedisSocketFactory (clientConfig );
103108 }
104109
105110 protected JedisFactory (final URI uri , final int connectionTimeout , final int soTimeout ,
@@ -120,17 +125,17 @@ protected JedisFactory(final URI uri, final int connectionTimeout, final int soT
120125 throw new InvalidURIException (String .format (
121126 "Cannot open Redis connection due invalid URI. %s" , uri .toString ()));
122127 }
123- this .hostAndPort .set (new HostAndPort (uri .getHost (), uri .getPort ()));
124128 this .clientConfig = DefaultJedisClientConfig .builder ().connectionTimeoutMillis (connectionTimeout )
125129 .socketTimeoutMillis (soTimeout ).blockingSocketTimeoutMillis (infiniteSoTimeout )
126130 .user (JedisURIHelper .getUser (uri )).password (JedisURIHelper .getPassword (uri ))
127131 .database (JedisURIHelper .getDBIndex (uri )).clientName (clientName )
128132 .ssl (JedisURIHelper .isRedisSSLScheme (uri )).sslSocketFactory (sslSocketFactory )
129133 .sslParameters (sslParameters ).hostnameVerifier (hostnameVerifier ).build ();
134+ this .jedisSocketFactory = new DefaultJedisSocketFactory (new HostAndPort (uri .getHost (), uri .getPort ()), this .clientConfig );
130135 }
131136
132137 public void setHostAndPort (final HostAndPort hostAndPort ) {
133- this . hostAndPort . set (hostAndPort );
138+ jedisSocketFactory . updateHostAndPort (hostAndPort );
134139 }
135140
136141 public void setPassword (final String password ) {
@@ -167,10 +172,9 @@ public void destroyObject(PooledObject<Jedis> pooledJedis) throws Exception {
167172
168173 @ Override
169174 public PooledObject <Jedis > makeObject () throws Exception {
170- final HostAndPort hostPort = this .hostAndPort .get ();
171175 Jedis jedis = null ;
172176 try {
173- jedis = new Jedis (hostPort , clientConfig );
177+ jedis = new Jedis (jedisSocketFactory , clientConfig );
174178 jedis .connect ();
175179 return new DefaultPooledObject <>(jedis );
176180 } catch (JedisException je ) {
@@ -199,13 +203,14 @@ public void passivateObject(PooledObject<Jedis> pooledJedis) throws Exception {
199203 public boolean validateObject (PooledObject <Jedis > pooledJedis ) {
200204 final BinaryJedis jedis = pooledJedis .getObject ();
201205 try {
202- HostAndPort hostAndPort = this .hostAndPort .get ();
206+ String host = jedisSocketFactory .getHost ();
207+ int port = jedisSocketFactory .getPort ();
203208
204209 String connectionHost = jedis .getClient ().getHost ();
205210 int connectionPort = jedis .getClient ().getPort ();
206211
207- return hostAndPort . getHost () .equals (connectionHost )
208- && hostAndPort . getPort () == connectionPort && jedis .isConnected ()
212+ return host .equals (connectionHost )
213+ && port == connectionPort && jedis .isConnected ()
209214 && jedis .ping ().equals ("PONG" );
210215 } catch (final Exception e ) {
211216 return false ;
0 commit comments