李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.chinaztt.mes.common.ftp;
 
 
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
 
/**
 * @Author: zhangxy
 * @Date: 2019/12/10 8:39
 */
public abstract class AbstractPool<T> {
 
    private final GenericObjectPool<T> internalPool;
 
    public AbstractPool(GenericObjectPool.Config poolConfig, PoolableObjectFactory<T> factory) {
        this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
        this.internalPool.setTestOnBorrow(true);
        this.internalPool.setTestWhileIdle(true);
        this.internalPool.setMaxWait(5000);
    }
 
    public T getResource() {
        try {
            return this.internalPool.borrowObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public void returnResource(T resource) {
        try {
            this.internalPool.returnObject(resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void invalidateResource(T resource) {
        try {
            this.internalPool.invalidateObject(resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    public void destroy() {
        try {
            this.internalPool.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public int inPoolSize() {
        try {
            return this.internalPool.getNumIdle();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
 
    public int borrowSize() {
        try {
            return this.internalPool.getNumActive();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}