自定义连接池

  1. pom.xml
1
2
3
4
5
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>
  1. factory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cn.idea360.i18n.pool;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

import java.util.concurrent.atomic.AtomicInteger;

/**
* @author cuishiying
* @date 2024-02-29
*/
public class StringPoolFactory extends BasePooledObjectFactory<String> {

private final AtomicInteger sum = new AtomicInteger(0);

public StringPoolFactory() {
super();
}

@Override
public String create() throws Exception {
return "str-val-" + sum.incrementAndGet();
}

@Override
public PooledObject<String> wrap(String s) {
System.out.println("包装: " + s);
return new DefaultPooledObject<>(s);
}

@Override
public void destroyObject(PooledObject<String> p) throws Exception {
System.out.println("销毁: " + p.getObject());
super.destroyObject(p);
}

@Override
public boolean validateObject(PooledObject<String> p) {
System.out.println("校验: " + p.getObject());
return super.validateObject(p);
}

@Override
public void activateObject(PooledObject<String> p) throws Exception {
System.out.println("获取: " + p.getObject());
super.activateObject(p);
}

@Override
public void passivateObject(PooledObject<String> p) throws Exception {
System.out.println("归还: " + p.getObject());
super.passivateObject(p);
}

}
  1. pool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package cn.idea360.i18n.pool;

import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.AbandonedConfig;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

/**
* @author cuishiying
* @date 2024-02-29
*/
public class StringPool extends GenericObjectPool<String> {

public StringPool(PooledObjectFactory<String> factory) {
super(factory);
}

public StringPool(PooledObjectFactory<String> factory, GenericObjectPoolConfig<String> config) {
super(factory, config);
}

public StringPool(PooledObjectFactory<String> factory, GenericObjectPoolConfig<String> config, AbandonedConfig abandonedConfig) {
super(factory, config, abandonedConfig);
}
}
  1. test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cn.idea360.i18n.pool;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import java.time.Duration;
import java.util.concurrent.CountDownLatch;

/**
* @author cuishiying
* @date 2024-02-29
*/
public class PoolTest {

public static void main(String[] args) throws Exception {
StringPoolFactory factory = new StringPoolFactory();
GenericObjectPoolConfig<String> config = new GenericObjectPoolConfig<>();
config.setMaxTotal(3);
config.setMinIdle(1);
config.setMaxWait(Duration.ofMillis(1000));
StringPool pool = new StringPool(factory, config);

int count = 10;
CountDownLatch c = new CountDownLatch(count);
for (int i = 0; i < count; i++) {
new Thread(() -> {
test(pool);
c.countDown();
}).start();

}
c.await();
System.out.println("活跃数: " + pool.getNumActive());
System.out.println("空闲数: " + pool.getNumIdle());
}

private static void test(StringPool pool) {
String s = null;
try {
s = pool.borrowObject();
System.out.println("输出: " + s);
} catch (Exception e) {
e.printStackTrace();
if (null != s) {
pool.returnObject(s);
}
} finally {
if (null != s) {
pool.returnObject(s);
}
}
}
}