Commons-pool object pool instance (1)

An example of an object pool is implemented using the commons-pool framework. The example on the official website has been slightly changed.

The version used is:

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
	<version>2.4.2</version>
</dependency>

 

 

Object pool implementation class:

 

package com.my.pool.pooledobject;

import org.apache.commons.pool2.ObjectPool;

public class PooledStringBuffer {
	private ObjectPool<StringBuffer> pool;
	
	public PooledStringBuffer(ObjectPool<StringBuffer> pool) {
		this.pool = pool;
	}
	
	public boolean print(String data) {
		boolean flag = false;
		if(pool!=null) {
			StringBuffer buf = null;
			try{
				buf = pool.borrowObject();
				buf.append(data);
				System.out.println(buf.toString());
				return true;
			}catch(Exception e) {
				e.printStackTrace ();
			}finally {
				try {
					pool.returnObject(buf);
				} catch (Exception e) {
					e.printStackTrace ();
				}
			}
		}
		
		return flag;
	}
	
}

 

The factory for the object pool:

package com.my.pool.pooledobject;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
 * Create first, pack later
 *
 */
public class PooledStringBufferFactory extends BasePooledObjectFactory<StringBuffer>{

	@Override
	public StringBuffer create() throws Exception {
		return new StringBuffer();
	}

	@Override
	/**
	 * Packaged into a dispatchable object
	 */
	public PooledObject<StringBuffer> wrap(StringBuffer obj) {
		return new DefaultPooledObject<StringBuffer>(obj);
	}
	
	@Override
	/**
	 * Method called after return
	 */
	public void passivateObject(PooledObject<StringBuffer> pooledObject) {
		pooledObject.getObject().setLength(0);
	}

}

 

Main class:

package com.my.pool.main;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import com.my.pool.pooledobject.PooledStringBuffer;
import com.my.pool.pooledobject.PooledStringBufferFactory;

public class MainClass {
	public static void main(String[] args) {
		GenericObjectPoolConfig config = new GenericObjectPoolConfig();
		//Maximum waiting time for allocation
		config.setMaxWaitMillis(1000l);
		//Maximum number of objects in the pool
		config.setMaxTotal(100);
		//Maximum number of free in the pool
		config.setMaxIdle(20);
		//Minimum number of free spaces in the pool
		config.setMinIdle(10);
		
		GenericObjectPool<StringBuffer> pool = new GenericObjectPool<StringBuffer>(new PooledStringBufferFactory(), config);
		
		PooledStringBuffer bufUtil = new PooledStringBuffer(pool);
		bufUtil.print("OK");
	}
}

 

    The object pool can be applied to the management of the database connection pool, which can reuse objects and improve performance. It is more convenient to use spring for configuration management in practical applications

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326682375&siteId=291194637