|
1 | 1 | package redis.clients.jedis; |
2 | 2 |
|
3 | 3 | import java.net.URI; |
| 4 | +import java.util.concurrent.atomic.AtomicReference; |
4 | 5 |
|
5 | 6 | import javax.net.ssl.HostnameVerifier; |
6 | 7 | import javax.net.ssl.SSLParameters; |
|
18 | 19 | * PoolableObjectFactory custom impl. |
19 | 20 | */ |
20 | 21 | class JedisFactory implements PooledObjectFactory<Jedis> { |
21 | | - private final JedisSocketFactory jedisSocketFactory; |
| 22 | + private final AtomicReference<JedisSocketFactory> jedisSocketFactory; |
22 | 23 | private final String user; |
23 | 24 | private final String password; |
24 | 25 | private final int database; |
25 | 26 | private final String clientName; |
26 | 27 |
|
27 | 28 | JedisFactory(final JedisSocketFactory jedisSocketFactory, final String user, final String password, final int database, final String clientName) { |
28 | | - this.jedisSocketFactory = jedisSocketFactory; |
| 29 | + this.jedisSocketFactory = new AtomicReference<>(jedisSocketFactory); |
29 | 30 | this.user = user; |
30 | 31 | this.password = password; |
31 | 32 | this.database = database; |
@@ -73,20 +74,22 @@ class JedisFactory implements PooledObjectFactory<Jedis> { |
73 | 74 | "Cannot open Redis connection due invalid URI. %s", uri.toString())); |
74 | 75 | } |
75 | 76 |
|
76 | | - this.jedisSocketFactory = new DefaultJedisSocketFactory( |
77 | | - uri.getHost(), uri.getPort(), connectionTimeout, soTimeout, |
78 | | - JedisURIHelper.isRedisSSLScheme(uri), sslSocketFactory, |
79 | | - sslParameters, hostnameVerifier |
80 | | - ); |
| 77 | + this.jedisSocketFactory = new AtomicReference<JedisSocketFactory>(new DefaultJedisSocketFactory( |
| 78 | + uri.getHost(), uri.getPort(), connectionTimeout, soTimeout, |
| 79 | + JedisURIHelper.isRedisSSLScheme(uri), sslSocketFactory, |
| 80 | + sslParameters, hostnameVerifier |
| 81 | + )); |
81 | 82 | this.user = JedisURIHelper.getUser(uri); |
82 | 83 | this.password = JedisURIHelper.getPassword(uri); |
83 | 84 | this.database = JedisURIHelper.getDBIndex(uri); |
84 | 85 | this.clientName = clientName; |
85 | 86 | } |
86 | 87 |
|
87 | 88 | public void setHostAndPort(final HostAndPort hostAndPort) { |
88 | | - jedisSocketFactory.setHost(hostAndPort.getHost()); |
89 | | - jedisSocketFactory.setPort(hostAndPort.getPort()); |
| 89 | + JedisSocketFactory newFactory = jedisSocketFactory.get().copy(); |
| 90 | + newFactory.setHost(hostAndPort.getHost()); |
| 91 | + newFactory.setPort(hostAndPort.getPort()); |
| 92 | + jedisSocketFactory.set(newFactory); |
90 | 93 | } |
91 | 94 |
|
92 | 95 | @Override |
@@ -114,7 +117,7 @@ public void destroyObject(PooledObject<Jedis> pooledJedis) throws Exception { |
114 | 117 |
|
115 | 118 | @Override |
116 | 119 | public PooledObject<Jedis> makeObject() throws Exception { |
117 | | - final Jedis jedis = new Jedis(jedisSocketFactory); |
| 120 | + final Jedis jedis = new Jedis(jedisSocketFactory.get()); |
118 | 121 | try { |
119 | 122 | jedis.connect(); |
120 | 123 | if (user != null) { |
@@ -145,8 +148,9 @@ public void passivateObject(PooledObject<Jedis> pooledJedis) throws Exception { |
145 | 148 | public boolean validateObject(PooledObject<Jedis> pooledJedis) { |
146 | 149 | final BinaryJedis jedis = pooledJedis.getObject(); |
147 | 150 | try { |
148 | | - String host = jedisSocketFactory.getHost(); |
149 | | - int port = jedisSocketFactory.getPort(); |
| 151 | + JedisSocketFactory factory = this.jedisSocketFactory.get(); |
| 152 | + String host = factory.getHost(); |
| 153 | + int port = factory.getPort(); |
150 | 154 |
|
151 | 155 | String connectionHost = jedis.getClient().getHost(); |
152 | 156 | int connectionPort = jedis.getClient().getPort(); |
|
0 commit comments