package com.chinaztt.mes.common.ftp; import org.apache.commons.pool.PoolableObjectFactory; import org.apache.commons.pool.impl.GenericObjectPool; /** * @Author: zhangxy * @Date: 2019/12/10 8:39 */ public abstract class AbstractPool { private final GenericObjectPool internalPool; public AbstractPool(GenericObjectPool.Config poolConfig, PoolableObjectFactory factory) { this.internalPool = new GenericObjectPool(factory, poolConfig); this.internalPool.setTestOnBorrow(true); this.internalPool.setTestWhileIdle(true); this.internalPool.setMaxWait(5000); } public T getResource() { try { return this.internalPool.borrowObject(); } catch (Exception e) { e.printStackTrace(); return null; } } public void returnResource(T resource) { try { this.internalPool.returnObject(resource); } catch (Exception e) { e.printStackTrace(); } } public void invalidateResource(T resource) { try { this.internalPool.invalidateObject(resource); } catch (Exception e) { e.printStackTrace(); } } public void destroy() { try { this.internalPool.close(); } catch (Exception e) { e.printStackTrace(); } } public int inPoolSize() { try { return this.internalPool.getNumIdle(); } catch (Exception e) { e.printStackTrace(); return 0; } } public int borrowSize() { try { return this.internalPool.getNumActive(); } catch (Exception e) { e.printStackTrace(); return 0; } } }