Commons-pool practices: poolableobjectfactory and objectpool

Source: Internet
Author: User
The Apache commons-pool toolkit is designed to reduce the creation and initialization of system resource objects. The commons-pool package mainly includes three important interfaces: Objectpool is used to manage the lending and return of objects to be pooled; Objectpoolfactory is used to generate a large number of objectpools of the same type and settings.
Take a look at the example below A connection class, it can be imagined as a remote connection, such as a database connection. This includes creating a connection, closing the connection, and a print method.
Package COM. googlecode. garbagecan. commons. pool. sample1; </P> <p> Import Org. slf4j. logger; <br/> Import Org. slf4j. loggerfactory; </P> <p> public class myconnection {</P> <p> Private Static logger = loggerfactory. getlogger (myconnection. class); </P> <p> private string name; <br/> private Boolean connected; </P> <p> Public myconnection (string name) {<br/> This. name = Name; <br/>}</P> <p> Public void connect () {<br/> This. connected = true; <br/> logger.info (name + ":" + connected); <br/>}</P> <p> Public void close () {<br/> This. connected = false; <br/> logger.info (name + ":" + connected); <br/>}</P> <p> Public Boolean isconnected () {<br/> return this. connected; <br/>}</P> <p> Public String getname () {<br/> return this. name; <br/>}</P> <p> Public void print () {<br/> logger.info (this. name); <br/>}< br/> one poolableobjectfactory interface implementation class, which provides makeobject, activateobject, passivateobject, validateobject, destroy Object method. Package COM. googlecode. garbagecan. commons. pool. sample1; </P> <p> Import Org. apache. commons. pool. poolableobjectfactory; <br/> Import Org. slf4j. logger; <br/> Import Org. slf4j. loggerfactory; </P> <p> public class myconnectionpoolableobjectfactory implements poolableobjectfactory {</P> <p> Private Static logger = loggerfactory. getlogger (myconnectionpoolableobjectfactory. class); </P> <p> Private Static int COUNT = 0; </P> <p> Public object makeobject () throws exception {<br/> myconnection myconn = new myconnection ("conn _" + (++ count); <br/> myconn. connect (); <br/> logger.info (myconn. getname (); <br/> return myconn; <br/>}</P> <p> Public void activateobject (Object OBJ) throws exception {<br/> myconnection myconn = (myconnection) OBJ; <br/> logger.info (myconn. getname (); <br/>}</P> <p> Public void passivateobject (Object OBJ) throws exception {<br/> myconnection myconn = (myconnection) OBJ; <br/> logger.info (myconn. getname (); <br/>}</P> <p> Public Boolean validateobject (Object OBJ) {<br/> myconnection myconn = (myconnection) OBJ; <br/> logger.info (myconn. getname (); <br/> return myconn. isconnected (); <br/>}</P> <p> Public void destroyobject (Object OBJ) throws exception {<br/> myconnection myconn = (myconnection) OBJ; <br/> logger.info (myconn. getname (); <br/> myconn. close (); <br/>}< br/> The last is a test class.
Package COM. googlecode. garbagecan. commons. pool. sample1; </P> <p> Import Org. apache. commons. pool. objectpool; <br/> Import Org. apache. commons. pool. poolableobjectfactory; <br/> Import Org. apache. commons. pool. impl. stackobjectpool; <br/> Import Org. slf4j. logger; <br/> Import Org. slf4j. loggerfactory; </P> <p> public class test {<br/> Private Static logger = loggerfactory. getlogger (test. class); </P> <p> Public static void main (string [] ARGs) throws exception {<br/> poolableobjectfactory factory = new myconnectionpoolableobjectfactory (); <br/> objectpool pool = new stackobjectpool (factory ); <br/> try {<br/> logger.info ("========================== ============================== "); <br/> for (INT I = 0; I <10; I ++) {<br/> myconnection myconn = (myconnection) pool. borrowobject (); <br/> try {<br/> myconn. print (); <br/>}catch (exception ex) {<br/> pool. invalidateobject (myconn); <br/>} finally {<br/> pool. returnobject (myconn ); <br/>}</P> <p> logger.info ("================== ============================= "); <br/> for (INT I = 0; I <10; I ++) {<br/> myconnection myconn1 = (myconnection) pool. borrowobject (); <br/> myconnection myconn2 = (myconnection) pool. borrowobject (); <br/> myconn1.print (); <br/> myconn2.print (); <br/> pool. returnobject (myconn1); <br/> pool. returnobject (myconn2); <br/>}< br/>} finally {<br/> try {<br/> pool. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> Run the test class. It can be seen that, although the first loop contains 10 loops, a total of 10 myconnection objects are required, but each return is a myconnection object instance named "conn_1, the log shows that the makeobject method is called only once. Therefore, except for the first time, every subsequent application is obtained from the pool. In the second loop, two myconnection object instances are applied each time. From the log, we can see that only one makeobject method is called in the second loop, the conn_2 object instance is created because the conn_1 object has been created in the first loop and is used directly. For better testing, no exception handling is performed in the second loop. In actual situations, it should be likeCodeClass: When an exception occurs between borrowobject and objects in the pool, you must call the invalidateobject method and return the objects in the pool.




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.