28
28
import org .springframework .util .Assert ;
29
29
import org .springframework .util .StringUtils ;
30
30
31
- import com .lambdaworks .redis .RedisAsyncConnection ;
32
31
import com .lambdaworks .redis .RedisClient ;
33
32
import com .lambdaworks .redis .RedisURI ;
33
+ import com .lambdaworks .redis .api .StatefulConnection ;
34
+ import com .lambdaworks .redis .api .StatefulRedisConnection ;
34
35
import com .lambdaworks .redis .resource .ClientResources ;
35
36
36
37
/**
43
44
public class DefaultLettucePool implements LettucePool , InitializingBean {
44
45
45
46
@ SuppressWarnings ("rawtypes" ) //
46
- private GenericObjectPool <RedisAsyncConnection > internalPool ;
47
+ private GenericObjectPool <StatefulConnection < byte [], byte []> > internalPool ;
47
48
private RedisClient client ;
48
49
private int dbIndex = 0 ;
49
50
private GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig ();
@@ -112,7 +113,8 @@ public void afterPropertiesSet() {
112
113
}
113
114
114
115
client .setDefaultTimeout (timeout , TimeUnit .MILLISECONDS );
115
- this .internalPool = new GenericObjectPool <RedisAsyncConnection >(new LettuceFactory (client , dbIndex ), poolConfig );
116
+ this .internalPool = new GenericObjectPool <StatefulConnection <byte [], byte []>>(new LettuceFactory (client , dbIndex ),
117
+ poolConfig );
116
118
}
117
119
118
120
/**
@@ -135,23 +137,23 @@ private RedisURI createSimpleHostRedisURI() {
135
137
}
136
138
137
139
@ SuppressWarnings ("unchecked" )
138
- public RedisAsyncConnection <byte [], byte []> getResource () {
140
+ public StatefulConnection <byte [], byte []> getResource () {
139
141
try {
140
142
return internalPool .borrowObject ();
141
143
} catch (Exception e ) {
142
144
throw new PoolException ("Could not get a resource from the pool" , e );
143
145
}
144
146
}
145
147
146
- public void returnBrokenResource (final RedisAsyncConnection <byte [], byte []> resource ) {
148
+ public void returnBrokenResource (final StatefulConnection <byte [], byte []> resource ) {
147
149
try {
148
150
internalPool .invalidateObject (resource );
149
151
} catch (Exception e ) {
150
152
throw new PoolException ("Could not invalidate the broken resource" , e );
151
153
}
152
154
}
153
155
154
- public void returnResource (final RedisAsyncConnection <byte [], byte []> resource ) {
156
+ public void returnResource (final StatefulConnection <byte [], byte []> resource ) {
155
157
try {
156
158
internalPool .returnObject (resource );
157
159
} catch (Exception e ) {
@@ -302,7 +304,7 @@ public void setClientResources(ClientResources clientResources) {
302
304
}
303
305
304
306
@ SuppressWarnings ("rawtypes" )
305
- private static class LettuceFactory extends BasePooledObjectFactory <RedisAsyncConnection > {
307
+ private static class LettuceFactory extends BasePooledObjectFactory <StatefulConnection < byte [], byte []> > {
306
308
307
309
private final RedisClient client ;
308
310
@@ -315,36 +317,40 @@ public LettuceFactory(RedisClient client, int dbIndex) {
315
317
}
316
318
317
319
@ Override
318
- public void activateObject (PooledObject <RedisAsyncConnection > pooledObject ) throws Exception {
319
- pooledObject .getObject ().select (dbIndex );
320
+ public void activateObject (PooledObject <StatefulConnection <byte [], byte []>> pooledObject ) throws Exception {
321
+
322
+ if (pooledObject .getObject () instanceof StatefulRedisConnection ) {
323
+ ((StatefulRedisConnection ) pooledObject .getObject ()).sync ().select (dbIndex );
324
+ }
320
325
}
321
326
322
- public void destroyObject (final PooledObject <RedisAsyncConnection > obj ) throws Exception {
327
+ public void destroyObject (final PooledObject <StatefulConnection < byte [], byte []> > obj ) throws Exception {
323
328
try {
324
329
obj .getObject ().close ();
325
330
} catch (Exception e ) {
326
331
// Errors may happen if returning a broken resource
327
332
}
328
333
}
329
334
330
- public boolean validateObject (final PooledObject <RedisAsyncConnection > obj ) {
335
+ public boolean validateObject (final PooledObject <StatefulConnection < byte [], byte []> > obj ) {
331
336
try {
332
- obj .getObject ().ping ();
337
+ if (obj .getObject () instanceof StatefulRedisConnection ) {
338
+ ((StatefulRedisConnection ) obj .getObject ()).sync ().ping ();
339
+ }
333
340
return true ;
334
341
} catch (Exception e ) {
335
342
return false ;
336
343
}
337
344
}
338
345
339
346
@ Override
340
- public RedisAsyncConnection create () throws Exception {
341
- return client .connectAsync (LettuceConnection .CODEC );
347
+ public StatefulConnection < byte [], byte []> create () throws Exception {
348
+ return client .connect (LettuceConnection .CODEC );
342
349
}
343
350
344
351
@ Override
345
- public PooledObject <RedisAsyncConnection > wrap (RedisAsyncConnection obj ) {
346
- return new DefaultPooledObject <RedisAsyncConnection >(obj );
352
+ public PooledObject <StatefulConnection < byte [], byte []>> wrap (StatefulConnection < byte [], byte []> obj ) {
353
+ return new DefaultPooledObject <StatefulConnection < byte [], byte []> >(obj );
347
354
}
348
-
349
355
}
350
356
}
0 commit comments