package com.googlecode.garbagecan.commons.pool.sample1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyConnection {
private static Logger logger = LoggerFactory.getLogger(MyConnection.class);
private String name;
private boolean connected;
public MyConnection(String name) {
this.name = name;
}
public void connect() {
this.connected = true;
logger.info(name + ": " + connected);
}
public void close() {
this.connected = false;
logger.info(name + ": " + connected);
}
public boolean isConnected() {
return this.connected;
}
public String getName() {
return this.name;
}
public void print() {
logger.info(this.name);
}
}
package com.googlecode.garbagecan.commons.pool.sample1;
import org.apache.commons.pool.PoolableObjectFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyConnectionPoolableObjectFactory implements PoolableObjectFactory {
private static Logger logger = LoggerFactory.getLogger(MyConnectionPoolableObjectFactory.class);
private static int count = 0;
public Object makeObject() throws Exception {
MyConnection myConn = new MyConnection("conn_" + (++count));
myConn.connect();
logger.info(myConn.getName());
return myConn;
}
public void activateObject(Object obj) throws Exception {
MyConnection myConn = (MyConnection)obj;
logger.info(myConn.getName());
}
public void passivateObject(Object obj) throws Exception {
MyConnection myConn = (MyConnection)obj;
logger.info(myConn.getName());
}
public boolean validateObject(Object obj) {
MyConnection myConn = (MyConnection)obj;
logger.info(myConn.getName());
return myConn.isConnected();
}
public void destroyObject(Object obj) throws Exception {
MyConnection myConn = (MyConnection)obj;
logger.info(myConn.getName());
myConn.close();
}
}
package com.googlecode.garbagecan.commons.pool.sample1;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.StackObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Test {
private static Logger logger = LoggerFactory.getLogger(Test.class);
public static void main(String[] args) throws Exception {
PoolableObjectFactory factory = new MyConnectionPoolableObjectFactory();
ObjectPool pool = new StackObjectPool(factory);
try {
logger.info("================================================");
for (int i = 0; i < 10; i++) {
MyConnection myConn = (MyConnection)pool.borrowObject();
try {
myConn.print();
} catch(Exception ex) {
pool.invalidateObject(myConn);
} finally {
pool.returnObject(myConn);
}
}
logger.info("================================================");
for (int i = 0; i < 10; i++) {
MyConnection myConn1 = (MyConnection)pool.borrowObject();
MyConnection myConn2 = (MyConnection)pool.borrowObject();
myConn1.print();
myConn2.print();
pool.returnObject(myConn1);
pool.returnObject(myConn2);
}
} finally {
try {
pool.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
创建一次对象调用方法过程
使用相同对象调用方法,并不需要调用makeObject
归还对象到对象池调用方法过程