Previous two articles
Article After talking about how to simply use the commons-pool library, we need to consider a problem here: in many cases, our objects in the pool are heavy and most of the scarce resources, such as database connections, in this way, if you keep putting some connections in the pool without returning them, it will occupy a lot of resources and reduce the utilization of these resources, how can we better manage the resources in the pool, the commons-Pool provides a genericobjectpool class. Its appearance can solve the above problem. Similarly, for the genericobjectpool class, there is a corresponding generickeyedobjectpool class.
The following is an example.
A connection class can be considered 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. sample3; </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/>}An Implementation class of the poolableobjectfactory interface that provides makeobject, activateobject, passivateobject, validateobject, and destroyobject methods.
Package COM. googlecode. garbagecan. commons. pool. sample3; </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 (generatename (); <br/> logger.info (myconn. getname (); <br/> myconn. connect (); <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/>}</P> <p> private synchronized string generatename () {<br/> return "conn _" + (++ count ); <br/>}< br/>}One test class
Package COM. googlecode. garbagecan. commons. pool. sample3; </P> <p> Import Org. apache. commons. pool. objectpool; <br/> Import Org. apache. commons. pool. poolableobjectfactory; <br/> Import Org. apache. commons. pool. impl. genericobjectpool; <br/> Import Org. slf4j. logger; <br/> Import Org. slf4j. loggerfactory; </P> <p> public class test {</P> <p> Private Static logger = loggerfactory. getlogger (test. class); </P> <p> Publ IC static void main (string [] ARGs) {<br/> // test1 (); <br/> // Test2 (); <br/> // test3 (); <br/>}</P> <p> Private Static void test1 () {<br/> poolableobjectfactory factory = new myconnectionpoolableobjectfactory (); <br/> genericobjectpool. config = new genericobjectpool. config (); <br/> config. LIFO = false; <br/> config. maxactive = 5; <br/> config. maxidle = 5; <br/> config. minidle = 1; <br/> config. maxwait = 5*1000; </P> <p> objectpool pool = new genericobjectpool (factory, config); <br/> for (INT I = 0; I <10; I ++) {<br/> thread = new thread (New mytask (pool); <br/> thread. start (); <br/>}< br/> // closepool (pool); <br/>}</P> <p> Private Static void Test2 () {<br/> poolableobjectfactory factory = new myconnectionpoolableobjectfactory (); <br/> genericobjectpool. config = new genericobjectpool. con Fig (); <br/> config. LIFO = false; <br/> config. maxactive = 5; <br/> config. maxidle = 5; <br/> config. minidle = 1; <br/> config. maxwait = 20*1000; </P> <p> objectpool pool = new genericobjectpool (factory, config); <br/> for (INT I = 0; I <10; I ++) {<br/> thread = new thread (New mytask (pool); <br/> thread. start (); <br/>}< br/> // closepool (pool); <br/>}</P> <p> Private Static void test3 () {<br/> poolab Leobjectfactory factory = new myconnectionpoolableobjectfactory (); <br/> genericobjectpool. config = new genericobjectpool. config (); <br/> config. LIFO = false; <br/> config. maxactive = 5; <br/> config. maxidle = 0; <br/> config. minidle = 0; <br/> config. maxwait =-1; </P> <p> objectpool pool = new genericobjectpool (factory, config); <br/> thread = new thread (New mytask (pool )); <br/> thread. start (); </P> <p> try {<br/> thread. sleep (60l * 1000l); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> // closepool (pool ); <br/>}</P> <p> Private Static void closepool (objectpool pool) {<br/> try {<br/> pool. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Private Static class mytask implements runnable {<br/> private objectpool; </P> <p> Public mytask (Objectpool pool) {<br/> This. pool = pool; <br/>}</P> <p> Public void run () {<br/> myconnection myconn = NULL; <br/> try {<br/> myconn = (myconnection) pool. borrowobject (); <br/> try {<br/> myconn. print (); <br/>}catch (exception ex) {<br/> pool. invalidateobject (myconn); <br/> myconn = NULL; <br/>}< br/> thread. sleep (10l * 1000l); <br/>} catch (exception ex) {<br/> logger. error ("cannot borrow connection fr Om pool. ", ex); <br/>}finally {<br/> If (myconn! = NULL) {<br/> try {<br/> pool. returnobject (myconn); <br/>} catch (exception ex) {<br/> logger. error ("cannot return connection from pool. ", ex); <br/>}< br/>}Three methods are included and three cases are tested respectively;
- Class contains an internal class that implements the runnable interface. The purpose is to start several threads to simulate the use of the connection class, and to be as realistic as possible, sleep for 10 seconds in the run method;
- First, run the test method test1 (). We can see that the first five threads can obtain the connection class well when applying for a connection class in a loop of 10 threads, but the last five threads cannot obtain the connection, and throw an exception because "config. maxactive = 5; "and" config. maxwait = 5*1000; "is at work, because the maximum number of active connections configured is 5, and the waiting time for no valid connections for subsequent applications is 5 seconds, therefore, the last five threads in the test1 method throw an exception after waiting for five seconds, indicating that the connection cannot be applied.
- Run the Test2 () method and change "config. maxwait = 20*1000;" to 20 seconds in Test2.ProgramEach thread uses connections for 10 seconds, so the next five threads get all the connections after waiting for 10 seconds, so the program will run successfully.
- In the test3 () method, change both maxidle and minidle to 0, that is, the connection is actually returned immediately when the connection is not in use. For database connections, the physical connection is closed, and maxwait is changed to-1, if you do not apply for a connection, you will always wait. Run the test3 () method and observe the log. It can be seen that the program will call myconnectionpoolableobjectfactory after the user connects to the object. destroyobject () and myconnection. close () method to destroy objects. Therefore, if this configuration is used, it is equivalent to a physical connection every time, and the connection is closed after use. Of course, this is an extreme example. In reality, neither maxidle nor minidle is set to 0.
In fact, the genericobjectpool. config class and the generickeyedobjectpool. config class still have many configuration parameters. Here we only list the simplest and most commonly used parameters. For details, refer to the official documentation.