apache-common pool的使用

转载 2016年06月01日 13:41:29
Apache Commons Pool小试一把
 
相比众多牛人的自己连接池,Apache的Pool就是牛中的大牛了,这里小试一把。
 
环境:
commons-pool-1.5.3-bin.zip
jdk1.5
 
 
一、基本String对象测试
 
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;

/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 2009-10-27 17:46:18
*/

public class Test {
        public static void main(String[] args) throws Exception {
                //创建一个对象池
                GenericKeyedObjectPool pool = new GenericKeyedObjectPool(new BaseKeyedPoolableObjectFactory() {
                        @Override
                        public Object makeObject(Object o) throws Exception {
                                return o;
                        }
                });

                //添加对象到池,重复的不会重复入池
                pool.addObject("a");
                pool.addObject("a");
                pool.addObject("b");
                pool.addObject("x");

                //清除最早的对象
                pool.clearOldest();

                //获取并输出对象
                System.out.println(pool.borrowObject("a"));
                System.out.println(pool.borrowObject("b"));
                System.out.println(pool.borrowObject("c"));
                System.out.println(pool.borrowObject("c"));
                System.out.println(pool.borrowObject("a"));

                //输出池状态
                System.out.println(pool.getMaxIdle());
                System.out.println(pool.getMaxActive());

        }
}
 
输出结果:
a
b
c
c
a
8
8

Process finished with exit code 0
 
当运行到清除最早对象一行,内存状态如下:
 
 
初略看看,Apache Commons pool功能还很强大,DBCP数据库连接池就用了它,还可以对其进行参数配置。
 
二、自定义复杂对象测试
 
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;

/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 2009-10-27 17:46:18
*/

public class Test {
        public static void main(String[] args) throws Exception {
                //创建一个对象池
                GenericKeyedObjectPool pool = new GenericKeyedObjectPool(new BaseKeyedPoolableObjectFactory() {
                        @Override
                        public Object makeObject(Object o) throws Exception {
                                if (o != null && o instanceof User)
                                        return o;
                                else
                                        return null;
                        }
                });

                //添加对象到池,重复的不会重复入池
                pool.addObject("a");
                pool.addObject("b");
                pool.addObject("x");
                pool.addObject(null);
                pool.addObject(null);
                pool.addObject(null);
                pool.addObject(new User("zhangsan", "123"));
                pool.addObject(new User("lisi", "112"));
                pool.addObject(new User("lisi", "112", 32));
                pool.addObject(new User("lisi", "112", 32, "一个烂人!"));


                //清除最早的对象
                pool.clearOldest();

                //获取并输出对象
                User u1 = new User("lisi", "112", 32, "一个烂人!");
                System.out.println(pool.borrowObject(u1));
                pool.returnObject(u1,u1);

                //获取并输出对象
                User u2 = new User("lisi", "112", 32, "一个烂人!");
                System.out.println(pool.borrowObject(u2));
                pool.returnObject(u2,u2);

                //获取并输出对象
                User u3 = new User("lisi", "112", 32);
                System.out.println(pool.borrowObject(u3));
                pool.returnObject(u3,u3);

                //获取并输出对象
                User u4 = new User("lisi", "112");
                System.out.println(pool.borrowObject(u4));
                pool.returnObject(u4,u4);

                System.out.println(pool.borrowObject(u4));
                pool.returnObject(u4,u4);
                System.out.println(pool.borrowObject(u4));
                pool.returnObject(u4,u4);
                System.out.println(pool.borrowObject(u4));
                pool.returnObject(u4,u4);
                System.out.println(pool.borrowObject(u4));
                pool.returnObject(u4,u4);
                System.out.println(pool.borrowObject(u4));
                pool.returnObject(u4,u4);
                System.out.println(pool.borrowObject(u4));
                pool.returnObject(u4,u4);
                System.out.println(pool.borrowObject(u4));
                pool.returnObject(u4,u4);





                System.out.println(pool.borrowObject(new User("lisi", "112")));
//                System.out.println(pool.borrowObject(new User("lisi", "112")));

                //输出池状态
                System.out.println(pool.getMaxIdle());
                System.out.println(pool.getMaxActive());
                pool.clearOldest();
                pool.close();

        }
}

class User {
        private String name;
        private String pswd;
        private int age;
        private String reamark;

        User() {
        }

        User(String name, String pswd) {
                this.name = name;
                this.pswd = pswd;
        }

        User(String name, String pswd, int age) {
                this.name = name;
                this.pswd = pswd;
                this.age = age;
        }

        User(String name, String pswd, int age, String reamark) {
                this.name = name;
                this.pswd = pswd;
                this.age = age;
                this.reamark = reamark;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getPswd() {
                return pswd;
        }

        public void setPswd(String pswd) {
                this.pswd = pswd;
        }

        public int getAge() {
                return age;
        }

        public void setAge(int age) {
                this.age = age;
        }

        public String getReamark() {
                return reamark;
        }

        public void setReamark(String reamark) {
                this.reamark = reamark;
        }

        @Override
        public boolean equals(Object o) {
                if (this == o) return true;
                if (o == null || getClass() != o.getClass()) return false;

                User user = (User) o;

                if (!name.equals(user.name)) return false;
                if (!pswd.equals(user.pswd)) return false;

                return true;
        }

        @Override
        public int hashCode() {
                int result = name.hashCode();
                result = 31 * result + pswd.hashCode();
                return result;
        }

        @Override
        public String toString() {
                return "User{" +
                                "name='" + name + '\'' +
                                ", pswd='" + pswd + '\'' +
                                ", age=" + age +
                                ", reamark='" + reamark + '\'' +
                                '}';
        }
}
 
测试结果:
User{name='lisi', pswd='112', age=32, reamark='null'}
User{name='lisi', pswd='112', age=32, reamark='一个烂人!'}
User{name='lisi', pswd='112', age=32, reamark='一个烂人!'}
User{name='lisi', pswd='112', age=32, reamark='null'}
User{name='lisi', pswd='112', age=0, reamark='null'}
User{name='lisi', pswd='112', age=0, reamark='null'}
User{name='lisi', pswd='112', age=0, reamark='null'}
User{name='lisi', pswd='112', age=0, reamark='null'}
User{name='lisi', pswd='112', age=0, reamark='null'}
User{name='lisi', pswd='112', age=0, reamark='null'}
User{name='lisi', pswd='112', age=0, reamark='null'}
User{name='lisi', pswd='112', age=0, reamark='null'}
8
8

Process finished with exit code 0
 
内存状态:
 
 
这次测试得出一些有用的结论:
1、复杂对象应该实现equals() 、hashCode()方法,以便不重复入池。
2、对于非入池的目标对象,比如a、b、x字符串,目前没有有效的办法过滤掉,不让其入池,虽然在工厂方法里做了努力。
3、试图将null入池,不会成功的,但也不抛异常。
4、对于池对象,使用应该遵循“接而有还,再借不难”-----从池中获取,不用时候归池,否则当池中没有空闲可用的对象时候,程序会处于等待状态,如果没有显式的调用归还方法,则造成程序死锁。
 
三、使用GenericObjectPool
 
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.BasePoolableObjectFactory;

import java.util.Random;

/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 2009-10-28 10:51:11
*/

public class Test2 {
        public static void main(String[] args) throws Exception {
                GenericObjectPool pool = new GenericObjectPool(new BasePoolableObjectFactory() {
                        public Object makeObject() throws Exception {
                                return new Random().nextFloat();
                        }
                });

                pool.addObject();
                pool.addObject();
                pool.addObject();
                pool.addObject();
                pool.addObject();
                pool.addObject();
                pool.addObject();
                pool.addObject();
                pool.addObject();
                pool.addObject();
                pool.addObject();
                pool.addObject();

                Object o = pool.borrowObject();
                System.out.println(o);
                pool.returnObject(o);

                Object o1 = pool.borrowObject();
                System.out.println(o1);

                Object o2 = pool.borrowObject();
                System.out.println(o2);

                Object o3 = pool.borrowObject();
                System.out.println(o3);

                Object o4 = pool.borrowObject();
                System.out.println(o4);


                Object o5 = pool.borrowObject();
                System.out.println(o5);


                Object o6 = pool.borrowObject();
                System.out.println(o6);

                Object o7 = pool.borrowObject();
                System.out.println(o7);

                Object o8 = pool.borrowObject();
                System.out.println(o8);

                pool.returnObject(o1);
                pool.returnObject(o2);
                pool.returnObject(o3);
                pool.returnObject(o4);
                pool.returnObject(o5);
                pool.returnObject(o6);
                pool.returnObject(o7);
                pool.returnObject(o8);

                Object o9 = pool.borrowObject();
                System.out.println(o9);
                pool.returnObject(o9);

                Object o10 = pool.borrowObject();
                System.out.println(o10);
                pool.returnObject(o10);

                Object o11 = pool.borrowObject();
                System.out.println(o11);
                pool.returnObject(o11);

                Object o12 = pool.borrowObject();
                System.out.println(o12);
                pool.returnObject(o12);

                Object o13 = pool.borrowObject();
                System.out.println(o13);
                pool.returnObject(o13);

        }
}
 
输出:
0.7358789
0.7358789
0.15324193
0.7465391
0.7198403
0.7477861
0.37906575
0.049714446
0.9650687
0.9650687
0.9650687
0.9650687
0.9650687
0.9650687

Process finished with exit code 0
 
从内存状态看到,这种对象池,没法将指定的对象入池,只能通过工厂生成对象入池,而且无法给工厂传递参数。
这种池适合数据库连接池等,所有的池对象拿出来都一样用。
 
经过上面三个例子综合测试得知,池的大小是初始化配置的,在没有显式配置指定的情况下,池大小默认为8.可以通过GenericKeyedObjectPool.Config来配置。
 

本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/217547

apache-common pool 的使用

Apache Commons-pool2(整理) apache-common pool的使用
  • hotdust
  • hotdust
  • 2017年11月24日 08:26
  • 111

apache-common pool的使用

Apache commons-pool本质上是"对象池",即通过一定的规则来维护对象集合的容器;commos-pool在很多场景中,用来实现"连接池"/"任务worker池"等,大家常用的dbcp数据...
  • zgc_sitech
  • zgc_sitech
  • 2014年07月18日 18:09
  • 583

对象池技术apache-common pool的使用

Apache commons-pool本质上是"对象池",即通过一定的规则来维护对象集合的容器;commos-pool在很多场景中,用来实现"连接池"/"任务worker池"等,大家常用的dbcp数据...
  • dudefu011
  • dudefu011
  • 2015年11月19日 16:17
  • 592

apache commons-pool的配置参数

转自:http://www.thinksaas.cn/group/topic/96620/ apache commons-pool的配置参数 write by yinmin...
  • wjacketcn
  • wjacketcn
  • 2016年03月02日 13:34
  • 1169

apache-common-pool2(配置参数详解,以及资源回收,从池中获取资源,将资源返还给池 逻辑解析)

apache-common-pool2(资源池配置参数详解,以及资源回收,从池中获取资源,将资源返还给池 逻辑解析)...
  • liang_love_java
  • liang_love_java
  • 2016年01月26日 17:29
  • 3179

apache-common

common-lang (2.1) ...
  • TIYILON
  • TIYILON
  • 2006年03月06日 10:41
  • 1570

boost及其子库pool内存池的使用

1.什么是boost库?boost库是一个可移植并提供源代码的C++库,是标准可的后备库,是C++标准化开发进程之一。 boost库是为C++语言标准库提供扩展的程序库总称。 2.boost程序库的下...
  • a200800170331
  • a200800170331
  • 2015年09月17日 19:15
  • 1823

Jedis Pool使用原理及源码

1,JedisPool的使用 //WHEN_EXHAUSTED_FAIL ...
  • xiangxizhishi
  • xiangxizhishi
  • 2017年08月27日 18:20
  • 157

sqlalchemy Connection Pool

sqlalchemy 默認的pool_size=5 pool裡存放的是在跟數據庫的的閒置連接, 使用c1 = engine.connect() 或 session = scoped_sessio...
  • liukeforever
  • liukeforever
  • 2011年12月05日 14:56
  • 3755

脚本之查询Shared Pool空闲率

以下语句可以查询Shared Pool空闲率: SQL> select to_number(v$parameter.value) value, v$sgastat.BYTES,   2      ...
  • haiross
  • haiross
  • 2014年11月19日 16:40
  • 471
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:apache-common pool的使用
举报原因:
原因补充:

(最多只允许输入30个字)