Apache Commons Pool2 Connection Pool Code

Source: Internet
Author: User

In 2013, Apache Commons Pool 2.0 was released, which is an implementation of a fully overridden object pool. The implementation of the Code and the original difference is very large, some of the original examples can not be used. Follow the example above and rewrite it with the Pool2 class and method.

The Apachecommons Pool 2.0 code is as follows:

Package Test.ffm83.commons.pool;

import Java.text.SimpleDateFormat;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

import org.apache.commons.pool2.BasePooledObjectFactory;

import Org.apache.commons.pool2.PooledObject;

import Org.apache.commons.pool2.impl.DefaultPooledObject;

import Org.apache.commons.pool2.impl.GenericObjectPool;

/**

* Commons Pool is simple and practical, based on 2.x version

* 1. Factory basepooledobjectfactory

* 2. Pool is configured as pool

* 3. Pool instance Resource

 * @author Fan Fangming

*/

Public class Pool2baseusage {

Public static void main (string[] args) {

Final Genericobjectpool<resource> pool =new genericobjectpool<resource> (

New Testpoolableobjectfactory ());

Pool.setmaxtotal (2);

for (int i = 0; i < 6; i++) {

New Thread (new Runnable () {

@Override

Public void run () {

Try {

Resourceobj = Pool.borrowobject (); // Note that if the object pool has no free objects, then this will Block

System. out. println (StringUtils. Rightpad(obj.tostring () +", Get Object ", ","-"));

Thread. Sleep (5000);

Pool.returnobject (obj); // Returning Connection Pool resources

System. out. println (StringUtils. Leftpad(obj.tostring () +", return object ", "()", "-"));

}catch(Exception e) {

E.printstacktrace ();

}

}

}). Start ();

}

}

/**

* Commons Pool is simple and practical, based on 2.x version

* 1.x of the basepoolableobjectfactory class is modified to basepooledobjectfactory

     * @author Fan Fangming

*/

static class Testpoolableobjectfactory extends basepooledobjectfactory<resource>{

@Override

Public Resource Create ()throws Exception {

return new Resource ();

}

@Override

Public Pooledobject<resource> Wrap (Resource Resource) {

return new Defaultpooledobject<resource> (Resource);

}

/**

* When a object is returned to the pool,clear the buffer.

*/

@Override

Public voidpassivateobject (pooledobject<resource> Resource) {

//resource.getobject (). Setrid ( -1);

}

}

static class Resource {

Public static int ID;

private int rid;

Public Resource () {

synchronized (this) {

RID = ID+ +;

}

}

Public int Getrid () {

return rid;

}

Public void setrid (int rid) {

this. rid = RID;

}

@Override

Public String toString () {

SIMPLEDATEFORMATDF = newSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); // Set Date format

Stringstr = Df.format (newDate ()) +","+ "ID:"+rid;

return str;

}

}

}

The results are as follows:

2014-12-19 15:51:58,id:1, gets the object---------------------

2014-12-19 15:51:58,id:0, gets the object---------------------

2014-12-19 15:52:03,id:0, gets the object---------------------

---------------------2014-12-1915:52:03,id:0, return the object

---------------------2014-12-1915:52:03,id:1, return the object

2014-12-19 15:52:03,id:1, gets the object---------------------

---------------------2014-12-1915:52:08,id:0, return the object

2014-12-19 15:52:08,id:0, gets the object---------------------

---------------------2014-12-1915:52:08,id:1, return the object

2014-12-19 15:52:08,id:1, gets the object---------------------

---------------------2014-12-1915:52:13,id:0, return the object

---------------------2014-12-1915:52:13,id:1, return the object

Apache Commons Pool2 Connection Pool Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.