本文整理汇总了Java中org.apache.commons.pool2.impl.SoftReferenceObjectPool类的典型用法代码示例。如果您正苦于以下问题:Java SoftReferenceObjectPool类的具体用法?Java SoftReferenceObjectPool怎么用?Java SoftReferenceObjectPool使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SoftReferenceObjectPool类属于org.apache.commons.pool2.impl包,在下文中一共展示了SoftReferenceObjectPool类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.commons.pool2.impl.SoftReferenceObjectPool; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
String file = "C:\\Windows\\System32\\drivers\\etc\\hosts";
ObjectPool<StringBuilder> objectPool = new SoftReferenceObjectPool(new StringBuilderFactory());
ReaderUtil readerUtil = new ReaderUtil(objectPool);
Reader reader = new FileReader(new File(file));
readerUtil.readToString(new FileReader(new File(file)));
readerUtil.readToString(new FileReader(new File(file)));
readerUtil.readToString(new FileReader(new File(file)));
readerUtil.readToString(new FileReader(new File(file)));
readerUtil.readToString(new FileReader(new File(file)));
readerUtil.readToString(new FileReader(new File(file)));
String content = readerUtil.readToString(new FileReader(new File(file)));
System.out.println(content);
}
示例2: tryWithResourcesReturnsSoftRefConnectionToPool
import org.apache.commons.pool2.impl.SoftReferenceObjectPool; //导入依赖的package包/类
@Test
public void tryWithResourcesReturnsSoftRefConnectionToPool() throws Exception {
SoftReferenceObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
.createSoftReferenceObjectPool(() -> client.connect());
StatefulRedisConnection<String, String> usedConnection = null;
try (StatefulRedisConnection<String, String> connection = pool.borrowObject()) {
RedisCommands<String, String> sync = connection.sync();
sync.ping();
usedConnection = connection;
}
try {
usedConnection.isMulti();
fail("Missing RedisException");
} catch (RedisException e) {
assertThat(e).hasMessageContaining("deallocated");
}
pool.close();
}
示例3: getPool
import org.apache.commons.pool2.impl.SoftReferenceObjectPool; //导入依赖的package包/类
/**
* Gets the pool of script engines for a specific language
*
* @param language Language of the script engine pool
*/
synchronized SoftReferenceObjectPool<ScriptEngine> getPool(String language) {
SoftReferenceObjectPool<ScriptEngine> pool = pools.get(language);
if (pool == null) {
pool = new SoftReferenceObjectPool<ScriptEngine>(new ScriptEngineInstanceFactory(language, engineManager));
pools.put(language, pool);
}
return pool;
}
示例4: setupObjectPool
import org.apache.commons.pool2.impl.SoftReferenceObjectPool; //导入依赖的package包/类
@Override
public void setupObjectPool() {
objectPool = new SoftReferenceObjectPool<>(new BasePooledObjectFactory<TestObject>() {
@Override
public TestObject create() throws Exception {
return new TestObject(true);
}
@Override
public PooledObject<TestObject> wrap(TestObject testObject) {
return new DefaultPooledObject<>(testObject);
}
});
}
开发者ID:chrishantha,项目名称:microbenchmarks,代码行数:15,代码来源:CommonsPool2SoftReferenceObjectPoolBenchmark.java
示例5: ChunkedFile
import org.apache.commons.pool2.impl.SoftReferenceObjectPool; //导入依赖的package包/类
public ChunkedFile(Path path, int chunkSize, int chunkOverlap) {
if (chunkOverlap > 0.5 * chunkSize)
throw new IllegalArgumentException(String.format(
"Chunk overlap is not allowed to be more than 0.5 of chunk size. " +
"You tried to set overlap %d when chunk size was %d", chunkOverlap, chunkSize));
this.path = path;
this.chunkSize = chunkSize;
this.chunkOverlap = chunkOverlap;
factory = new ByteArrayHolderFactory();
factory.setDefaultSize(chunkSize);
pool = new SoftReferenceObjectPool<>(factory);
}
示例6: createSoftReferenceObjectPool
import org.apache.commons.pool2.impl.SoftReferenceObjectPool; //导入依赖的package包/类
/**
* Creates a new {@link SoftReferenceObjectPool} using the {@link Supplier}.
*
* @param connectionSupplier must not be {@literal null}.
* @param wrapConnections {@literal false} to return direct connections that need to be returned to the pool using
* {@link ObjectPool#returnObject(Object)}. {@literal true} to return wrapped connection that are returned to the
* pool when invoking {@link StatefulConnection#close()}.
* @param <T> connection type.
* @return the connection pool.
*/
@SuppressWarnings("unchecked")
public static <T extends StatefulConnection<?, ?>> SoftReferenceObjectPool<T> createSoftReferenceObjectPool(
Supplier<T> connectionSupplier, boolean wrapConnections) {
LettuceAssert.notNull(connectionSupplier, "Connection supplier must not be null");
AtomicReference<ObjectPool<T>> poolRef = new AtomicReference<>();
SoftReferenceObjectPool<T> pool = new SoftReferenceObjectPool<T>(new RedisPooledObjectFactory<>(connectionSupplier)) {
@Override
public T borrowObject() throws Exception {
return wrapConnections ? wrapConnection(super.borrowObject(), this) : super.borrowObject();
}
@Override
public void returnObject(T obj) throws Exception {
if (wrapConnections && obj instanceof HasTargetConnection) {
super.returnObject((T) ((HasTargetConnection) obj).getTargetConnection());
return;
}
super.returnObject(obj);
}
};
poolRef.set(pool);
return pool;
}
示例7: softReferencePoolShouldWorkWithPlainConnections
import org.apache.commons.pool2.impl.SoftReferenceObjectPool; //导入依赖的package包/类
@Test
public void softReferencePoolShouldWorkWithPlainConnections() throws Exception {
SoftReferenceObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
.createSoftReferenceObjectPool(() -> client.connect(), false);
borrowAndReturn(pool);
StatefulRedisConnection<String, String> connection = pool.borrowObject();
assertThat(Proxy.isProxyClass(connection.getClass())).isFalse();
pool.returnObject(connection);
pool.close();
}
示例8: instantiateReaderPool
import org.apache.commons.pool2.impl.SoftReferenceObjectPool; //导入依赖的package包/类
private ObjectPool<XMLStreamReaderImpl> instantiateReaderPool() {
return new SoftReferenceObjectPool<>(new XMLStreamReaderFactory());
}
示例9: getPool
import org.apache.commons.pool2.impl.SoftReferenceObjectPool; //导入依赖的package包/类
public SoftReferenceObjectPool<ByteArrayHolder> getPool() {
return pool;
}
示例10: softRefPoolShouldWorkWithWrappedConnections
import org.apache.commons.pool2.impl.SoftReferenceObjectPool; //导入依赖的package包/类
@Test
public void softRefPoolShouldWorkWithWrappedConnections() throws Exception {
SoftReferenceObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
.createSoftReferenceObjectPool(() -> client.connect());
StatefulRedisConnection<String, String> connection = pool.borrowObject();
assertThat(channels).hasSize(1);
RedisCommands<String, String> sync = connection.sync();
sync.ping();
sync.close();
pool.close();
Wait.untilTrue(channels::isEmpty).waitOrTimeout();
assertThat(channels).isEmpty();
}
示例11: testPOFReturnObjectUsages
import org.apache.commons.pool2.impl.SoftReferenceObjectPool; //导入依赖的package包/类
@Test
public void testPOFReturnObjectUsages() throws Exception {
final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
final ObjectPool<Object> pool;
try {
pool = makeEmptyPool(factory);
} catch (final UnsupportedOperationException uoe) {
return; // test not supported
}
final List<MethodCall> expectedMethods = new ArrayList<>();
Object obj;
/// Test correct behavior code paths
obj = pool.borrowObject();
clear(factory, expectedMethods);
// returned object should be passivated
pool.returnObject(obj);
// StackObjectPool, SoftReferenceObjectPool also validate on return
if (pool instanceof SoftReferenceObjectPool) {
expectedMethods.add(new MethodCall(
"validateObject", obj).returned(Boolean.TRUE));
}
expectedMethods.add(new MethodCall("passivateObject", obj));
assertEquals(expectedMethods, factory.getMethodCalls());
//// Test exception handling of returnObject
reset(pool, factory, expectedMethods);
pool.addObject();
pool.addObject();
pool.addObject();
assertEquals(3, pool.getNumIdle());
// passivateObject should swallow exceptions and not add the object to the pool
obj = pool.borrowObject();
pool.borrowObject();
assertEquals(1, pool.getNumIdle());
assertEquals(2, pool.getNumActive());
clear(factory, expectedMethods);
factory.setPassivateObjectFail(true);
pool.returnObject(obj);
// StackObjectPool, SoftReferenceObjectPool also validate on return
if (pool instanceof SoftReferenceObjectPool) {
expectedMethods.add(new MethodCall(
"validateObject", obj).returned(Boolean.TRUE));
}
expectedMethods.add(new MethodCall("passivateObject", obj));
removeDestroyObjectCall(factory.getMethodCalls()); // The exact timing of destroyObject is flexible here.
assertEquals(expectedMethods, factory.getMethodCalls());
assertEquals(1, pool.getNumIdle()); // Not returned
assertEquals(1, pool.getNumActive()); // But not in active count
// destroyObject should swallow exceptions too
reset(pool, factory, expectedMethods);
obj = pool.borrowObject();
clear(factory, expectedMethods);
factory.setPassivateObjectFail(true);
factory.setDestroyObjectFail(true);
pool.returnObject(obj);
pool.close();
}