Skip to content

Commit 3f30725

Browse files
authored
Merge pull request #211 from masahitojp/feat/use-apache-commons-pool-2.x
using apache pool to v2(latest)
2 parents 5dd0079 + 5f95dae commit 3f30725

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

build.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ lazy val commonSettings: Seq[Setting[_]] = Seq(
1818
lazy val coreSettings = commonSettings ++ Seq(
1919
name := "RedisClient",
2020
libraryDependencies ++= Seq(
21-
"commons-pool" % "commons-pool" % "1.6",
21+
"commons-pool" % "commons-pool" % "2.6.0",
2222
"org.slf4j" % "slf4j-api" % "1.7.25",
2323
"org.slf4j" % "slf4j-log4j12" % "1.7.25" % "provided",
2424
"log4j" % "log4j" % "1.2.17" % "provided",

src/main/scala/com/redis/Pool.scala

+30-21
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,54 @@
11
package com.redis
22

3-
import org.apache.commons.pool._
4-
import org.apache.commons.pool.impl._
3+
import java.util.concurrent.TimeUnit
4+
5+
import org.apache.commons.pool2._
6+
import org.apache.commons.pool2.impl._
57
import com.redis.cluster.ClusterNode
68

79
private [redis] class RedisClientFactory(val host: String, val port: Int, val database: Int = 0, val secret: Option[Any] = None, val timeout : Int = 0)
8-
extends PoolableObjectFactory[RedisClient] {
10+
extends PooledObjectFactory[RedisClient] {
911

1012
// when we make an object it's already connected
11-
def makeObject: RedisClient = {
12-
new RedisClient(host, port, database, secret, timeout)
13+
override def makeObject: PooledObject[RedisClient] = {
14+
new DefaultPooledObject[RedisClient](new RedisClient(host, port, database, secret, timeout))
1315
}
1416

1517
// quit & disconnect
16-
def destroyObject(rc: RedisClient): Unit = {
18+
override def destroyObject(p: PooledObject[RedisClient]): Unit = {
19+
val rc = p.getObject
1720
rc.quit // need to quit for closing the connection
1821
rc.disconnect // need to disconnect for releasing sockets
1922
}
2023

2124
// noop: we want to have it connected
22-
def passivateObject(rc: RedisClient): Unit = {}
23-
def validateObject(rc: RedisClient): Boolean = rc.connected
25+
override def passivateObject(p: PooledObject[RedisClient]): Unit = {}
26+
override def validateObject(p: PooledObject[RedisClient]): Boolean = p.getObject.connected
2427

2528
// noop: it should be connected already
26-
def activateObject(rc: RedisClient): Unit = {}
29+
override def activateObject(p: PooledObject[RedisClient]): Unit = {}
2730
}
2831

2932
object RedisClientPool {
30-
val UNLIMITED_CONNECTIONS = -1
31-
32-
val WHEN_EXHAUSTED_BLOCK = GenericObjectPool.WHEN_EXHAUSTED_BLOCK
33-
val WHEN_EXHAUSTED_FAIL = GenericObjectPool.WHEN_EXHAUSTED_FAIL
34-
val WHEN_EXHAUSTED_GROW = GenericObjectPool.WHEN_EXHAUSTED_GROW
33+
val UNLIMITED_CONNECTIONS: Int = -1
3534
}
3635

37-
class RedisClientPool(val host: String, val port: Int, val maxIdle: Int = 8, val database: Int = 0, val secret: Option[Any] = None, val timeout : Int = 0,
38-
val maxConnections: Int = RedisClientPool.UNLIMITED_CONNECTIONS, val whenExhaustedBehavior: Byte = RedisClientPool.WHEN_EXHAUSTED_BLOCK, val poolWaitTimeout: Long = 3000) {
39-
val pool = new GenericObjectPool(new RedisClientFactory(host, port, database, secret, timeout), maxConnections, whenExhaustedBehavior, poolWaitTimeout, maxIdle, false, true)
36+
class RedisClientPool(val host: String, val port: Int, val maxIdle: Int = 8, val database: Int = 0, val secret: Option[Any] = None, val timeout : Int = 0,
37+
val maxConnections: Int = RedisClientPool.UNLIMITED_CONNECTIONS, val poolWaitTimeout: Long = 3000) {
38+
39+
val objectPoolConfig = new GenericObjectPoolConfig[RedisClient]
40+
objectPoolConfig.setMaxIdle(maxIdle)
41+
objectPoolConfig.setMaxTotal(maxConnections)
42+
objectPoolConfig.setBlockWhenExhausted(true)
43+
objectPoolConfig.setTestOnBorrow(false)
44+
objectPoolConfig.setTestOnReturn(true)
45+
46+
val abandonedConfig = new AbandonedConfig
47+
abandonedConfig.setRemoveAbandonedTimeout(TimeUnit.MILLISECONDS.toSeconds(poolWaitTimeout).toInt)
48+
val pool = new GenericObjectPool(new RedisClientFactory(host, port, database, secret, timeout), objectPoolConfig,abandonedConfig)
4049
override def toString: String = host + ":" + String.valueOf(port)
4150

42-
def withClient[T](body: RedisClient => T) = {
51+
def withClient[T](body: RedisClient => T): T = {
4352
val client = pool.borrowObject
4453
try {
4554
body(client)
@@ -53,9 +62,9 @@ class RedisClientPool(val host: String, val port: Int, val maxIdle: Int = 8, val
5362
}
5463

5564
/**
56-
*
57-
* @param node
58-
*/
65+
*
66+
* @param node
67+
*/
5968
class IdentifiableRedisClientPool(val node: ClusterNode)
6069
extends RedisClientPool (node.host, node.port, node.maxIdle, node.database, node.secret,node.timeout){
6170
override def toString: String = node.nodename

0 commit comments

Comments
 (0)