Answer the question
In order to leave comments, you need to log in
What is the correct way to apply the Eviction Policy in org.apache.commons.pool2?
package com.mycompany.simplepool;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.EvictionConfig;
import org.apache.commons.pool2.impl.EvictionPolicy;
public class MyEvictionPolicy<T> implements EvictionPolicy<T> {
@Override
public boolean evict(EvictionConfig config, PooledObject<T> underTest, int idleCount) {
System.out.println("MyEvictPolicy");
if (Integer.parseInt(underTest.getObject().toString()) % 2 == 0) {
System.out.println("Delete because % 2 == 0 ---- " + underTest.getObject());
return true;
}
if ((config.getIdleSoftEvictTime() < underTest.getIdleTimeMillis() &&
config.getMinIdle() < idleCount) ||
config.getIdleEvictTime() < underTest.getIdleTimeMillis()) {
return true;
}
return false;
}
}
BasePooledObjectFactory<String> poolFactory = new MyBasePooledObjectFactory();
GenericObjectPoolConfig configPool = new GenericObjectPoolConfig();
configPool.setTimeBetweenEvictionRunsMillis(10);
configPool.setMaxIdle(2);
GenericObjectPool<String> pool = new GenericObjectPool<>(poolFactory);
pool.setEvictionPolicyClassName(MyEvictionPolicy.class.getName()); // - задаю Eviction Policy
pool.setConfig(configPool);
Answer the question
In order to leave comments, you need to log in
Poking around in the sources , I came to the conclusion that you can specify the EvictionPolicy in the config and pass it to the overloaded constructor.
BasePooledObjectFactory<String> poolFactory = new MyBasePooledObjectFactory();
GenericObjectPoolConfig configPool = new GenericObjectPoolConfig();
configPool.setTimeBetweenEvictionRunsMillis(100);
configPool.setMaxIdle(2);
configPool.setEvictionPolicyClassName(MyEvictionPolicy.class.getName());
GenericObjectPool<String> pool = new GenericObjectPool<>(poolFactory, configPool);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question