com.baidu.jprotobuf.pbrpc.utils.Pool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jprotobuf-rpc-core Show documentation
Show all versions of jprotobuf-rpc-core Show documentation
The core module of jprotobuf rpc socket implementation
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baidu.jprotobuf.pbrpc.utils;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
/**
* Base pool class
*
* @author xiemalin
* @since 2.27
*/
public abstract class Pool {
private final GenericObjectPool internalPool;
public Pool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory factory) {
this.internalPool = new GenericObjectPool(factory, poolConfig);
}
@SuppressWarnings("unchecked")
public T getResource() {
try {
return (T) internalPool.borrowObject();
} catch (Exception e) {
throw new RuntimeException("Could not get a resource from the pool", e);
}
}
public void returnResource(final T resource) {
try {
internalPool.returnObject(resource);
} catch (Exception e) {
throw new RuntimeException("Could not return the resource to the pool", e);
}
}
public void returnBrokenResource(final T resource) {
try {
internalPool.invalidateObject(resource);
} catch (Exception e) {
throw new RuntimeException("Could not return the resource to the pool", e);
}
}
public void destroy() {
try {
internalPool.close();
} catch (Exception e) {
throw new RuntimeException("Could not destroy the pool", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy