Modify the code of the previous article to implement KeyedObjectPool.
Object pool implementation class :
package com.my.pool.pooledobject; import org.apache.commons.pool2.KeyedObjectPool; public class KeyedPooledStringBuffer { private KeyedObjectPool<String,StringBuffer> pool; public KeyedPooledStringBuffer(KeyedObjectPool<String,StringBuffer> pool) { this.pool = pool; } public boolean print(String key,String data) { boolean flag = false; if(pool!=null&&key!=null) { StringBuffer buf = null; try{ buf = pool.borrowObject(key); buf.append(data); System.out.println(buf.toString()); return true; }catch(Exception e) { e.printStackTrace (); }finally { try { if(buf != null) { pool.returnObject(key, buf); } } catch (Exception e) { e.printStackTrace (); } } } return flag; } }
Factory class:
package com.my.pool.pooledobject; import org.apache.commons.pool2.BaseKeyedPooledObjectFactory; import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.impl.DefaultPooledObject; /** * Create first, pack later * */ public class KeyedPooledStringBufferFactory extends BaseKeyedPooledObjectFactory<String,StringBuffer>{ @Override public StringBuffer create(String key) throws Exception { StringBuffer buf = new StringBuffer(); // Perform different operations according to the different keys if("hello".equals(key)) { buf.append(key); buf.append("\t"); }else { //Do nothing. } return buf; } @Override public PooledObject<StringBuffer> wrap(StringBuffer obj) { return new DefaultPooledObject<StringBuffer>(obj); } @Override /** * Method called after return */ public void passivateObject(String key,PooledObject<StringBuffer> pooledObject) { StringBuffer buf = pooledObject.getObject(); buf.setLength(0); if("hello".equals(key)) { buf.append(key); buf.append("\t"); }else { //Do nothing. } } }
Main class:
package com.my.pool.main; import org.apache.commons.pool2.impl.GenericKeyedObjectPool; import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig; import com.my.pool.pooledobject.KeyedPooledStringBuffer; import com.my.pool.pooledobject.KeyedPooledStringBufferFactory; public class KeyedMainClass { public static void main(String[] args) { GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig(); //Maximum waiting time for allocation config.setMaxWaitMillis(1000l); //Maximum number of objects in each key pool config.setMaxTotalPerKey(100); //Maximum free number in each key pool config.setMaxIdlePerKey(20); //Minimum number of free spaces in each key pool config.setMinIdlePerKey(10); GenericKeyedObjectPool<String,StringBuffer> pool = new GenericKeyedObjectPool<String,StringBuffer>(new KeyedPooledStringBufferFactory()); KeyedPooledStringBuffer bufUtil = new KeyedPooledStringBuffer(pool); bufUtil.print("hello","OK"); bufUtil.print("else","OK"); } }
The difference between KeyedObjectPool and ObjectPool is that different operations can be performed on the input key, and even different thread pools can be obtained. In this example, some characters are added at the time of creation, and the prefix is written back after recycling. Different usage, you can use your imagination.