本文整理汇总了Java中org.apache.commons.pool2.PooledObject类的典型用法代码示例。如果您正苦于以下问题:Java PooledObject类的具体用法?Java PooledObject怎么用?Java PooledObject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PooledObject类属于org.apache.commons.pool2包,在下文中一共展示了PooledObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: makeObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
public PooledObject<Socket> makeObject() throws Exception {
// 简单策略随机取服务器,没有考虑权重
List<ServerAddress> temps = new ArrayList<ServerAddress>();
temps.addAll(serverGroup.getServers());
for (int i = temps.size() - 1; i >= 0; i--) {
int index = new Random(System.currentTimeMillis()).nextInt(temps.size());
ServerAddress server = temps.get(index);
try {
PooledObject<Socket> po = new DefaultPooledObject<Socket>(
new Socket(server.getIp(), server.getPort()));
if (logger.isDebugEnabled()) {
logger.debug("tcpPool makeObject[" + server.getIp() + ":" + server.getPort() + "] success!");
}
return po;
} catch (Exception e) {
temps.remove(i);
logger.error("tcpPool makeObject[" + server.getIp() + ":" + server.getPort() + "] failed! "
+ e.getMessage());
}
}
// 没有可用服务器时,请求一下registry更新服务器信息
this.updateServerGroup();
throw new Exception("No server available!");
}
示例2: makeObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@Override
public PooledObject<Jedis> makeObject() throws Exception {
final HostAndPort hostAndPort = this.hostAndPort.get();
final Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout,
soTimeout);
jedis.connect();
if (null != this.password) {
jedis.auth(this.password);
}
if (database != 0) {
jedis.select(database);
}
if (clientName != null) {
jedis.clientSetname(clientName);
}
return new DefaultPooledObject<Jedis>(jedis);
}
示例3: validateObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@Override
public boolean validateObject(PooledObject<Jedis> pooledJedis) {
final BinaryJedis jedis = pooledJedis.getObject();
try {
HostAndPort hostAndPort = this.hostAndPort.get();
String connectionHost = jedis.getClient().getHost();
int connectionPort = jedis.getClient().getPort();
return hostAndPort.getHost().equals(connectionHost)
&& hostAndPort.getPort() == connectionPort && jedis.isConnected()
&& jedis.ping().equals("PONG");
} catch (final Exception e) {
return false;
}
}
示例4: passivateObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@Override
public void passivateObject(Event.Type key, PooledObject<Event> pooledObject) throws Exception {
final Event event = pooledObject.getObject();
switch (event.type) {
case BEFORE:
unsafe.putObject(event, targetFieldInBeforeEventOffset, null);
unsafe.putObject(event, argumentArrayFieldInBeforeEventOffset, null);
break;
case IMMEDIATELY_THROWS:
case THROWS:
unsafe.putObject(event, throwableFieldInThrowsEventOffset, null);
break;
case IMMEDIATELY_RETURN:
case RETURN:
unsafe.putObject(event, objectFieldInReturnEventOffset, null);
}
}
示例5: validateObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@Override
public boolean validateObject(ThriftServer thriftServer, PooledObject<TTransport> pooledObject) {
boolean isValidate;
try {
if (failoverChecker == null) {
isValidate = pooledObject.getObject().isOpen();
} else {
ConnectionValidator validator = failoverChecker.getConnectionValidator();
isValidate = pooledObject.getObject().isOpen() && (validator == null || validator.isValid(pooledObject.getObject()));
}
} catch (Throwable e) {
logger.warn("Fail to validate tsocket: {}:{}", new Object[]{thriftServer.getHost(), thriftServer.getPort(), e});
isValidate = false;
}
if (failoverChecker != null && !isValidate) {
failoverChecker.getFailoverStrategy().fail(thriftServer);
}
logger.info("ValidateObject isValidate:{}", isValidate);
return isValidate;
}
示例6: setupObjectPool
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@Override
public void setupObjectPool() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(poolSize);
objectPool = new GenericObjectPool<>(new BasePooledObjectFactory<TestObject>() {
@Override
public TestObject create() throws Exception {
return new TestObject(true);
}
@Override
public PooledObject<TestObject> wrap(TestObject testObject) {
return new DefaultPooledObject<>(testObject);
}
}, config);
}
示例7: getPool
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
private synchronized GenericKeyedObjectPool<String, Script> getPool() {
if (pool == null) {
GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
poolConfig.setMaxTotalPerKey(-1);
poolConfig.setMaxIdlePerKey(globalConfig.getGroovyEvaluationPoolMaxIdle());
pool = new GenericKeyedObjectPool<>(
new BaseKeyedPooledObjectFactory<String, Script>() {
@Override
public Script create(String key) throws Exception {
return createScript(key);
}
@Override
public PooledObject<Script> wrap(Script value) {
return new DefaultPooledObject<>(value);
}
},
poolConfig
);
}
return pool;
}
示例8: makeObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@Override
public PooledObject<Jedis> makeObject() throws Exception {
final Jedis jedis = new Jedis(this.host, this.port, this.timeout);
jedis.connect();
if (null != this.password) {
jedis.auth(this.password);
}
if (database != 0) {
jedis.select(database);
}
if (clientName != null) {
jedis.clientSetname(clientName);
}
return new DefaultPooledObject<Jedis>(jedis);
}
示例9: makeObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public PooledObject makeObject() throws Exception {
//logger.debug("makeObject...........");
try {
String host = this.host;
int port = this.port;
int timeout = this.timeout;
TFramedTransport transport = new TFramedTransport(new TSocket(host, port, timeout));
TBinaryProtocol protocol = new TBinaryProtocol(transport);
FrcService.Client client = new FrcService.Client(protocol, protocol);
transport.open();
RpcClient<Client> rpcClient = new RpcClient(client, transport, 1);
return this.wrap(rpcClient);
} catch (Exception e) {
logger.error("exception", e);
return null;
}
}
示例10: makeObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@Override
public PooledObject<Jedis> makeObject() throws Exception {
final HostAndPort hostAndPort = this.hostAndPort.get();
final Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout);
jedis.connect();
if (null != this.password) {
jedis.auth(this.password);
}
if (database != 0) {
jedis.select(database);
}
if (clientName != null) {
jedis.clientSetname(clientName);
}
return new DefaultPooledObject<Jedis>(jedis);
}
示例11: main
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
public static void main(String[] args) {
log("PCFMessageAgent Test");
final Properties p = new Properties();
p.setProperty(HOST_KEY, "localhost");
p.setProperty(PORT_KEY, "1430");
p.setProperty(CHANNEL_KEY, "JBOSS.SVRCONN");
MQPCFPoolBuilder poolBuilder = new MQPCFPoolBuilder(p);
log("Created");
log("PCF_CTOR: [" + PCF_CTOR + "]");
Object o = null;
PooledObject<Object> pooledObject = null;
try {
o = poolBuilder.create();
log("Created Object: [" + o + "]");
pooledObject = new DefaultPooledObject<Object>(o);
log("Validating Object....");
poolBuilder.validateObject(pooledObject);
//SystemClock.sleep(100000);
} finally {
if(pooledObject!=null) try { poolBuilder.destroyObject(pooledObject); } catch (Exception x) {}
}
}
示例12: makeObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@Override
public PooledObject<Jedis> makeObject() throws Exception {
final HostAndPort hostAndPort = this.hostAndPort.get();
JaRedis.Builder builder = new JaRedis.Builder();
builder
.host(hostAndPort.getHost())
.port(hostAndPort.getPort())
.connectionTimeout(connectionTimeout)
.soTimeout(soTimeout);
Jedis jedis = builder.build();
try {
jedis.connect();
if (null != this.password) {
jedis.auth(this.password);
}
if (database != 0) {
jedis.select(database);
}
if (clientName != null) {
jedis.clientSetname(clientName);
}
} catch (JedisException je) {
jedis.close();
throw je;
}
return new DefaultPooledObject<>(jedis);
}
示例13: validateObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@Override
public boolean validateObject(PooledObject<Jedis> pooledJedis) {
final BinaryJedis jedis = pooledJedis.getObject();
try {
HostAndPort hostAndPort = this.hostAndPort.get();
String connectionHost = jedis.getClient().getHost();
int connectionPort = jedis.getClient().getPort();
return hostAndPort.getHost().equals(connectionHost)
&& hostAndPort.getPort() == connectionPort && jedis.isConnected()
&& jedis.ping().equals("PONG");
} catch (final Exception e) {
return false;
}
}
示例14: activateObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@Override
public void activateObject(PooledObject<Jedis> pooledJedis) throws Exception {
final BinaryJedis jedis = pooledJedis.getObject();
if (jedis.getDB() != database) {
jedis.select(database);
}
}
示例15: activateObject
import org.apache.commons.pool2.PooledObject; //导入依赖的package包/类
@Override
public void activateObject(PooledObject<Jedis> pooledJedis) throws Exception {
final BinaryJedis jedis = pooledJedis.getObject();
if (jedis.getDB() != database) {
jedis.select(database);
}
}