com.quincy.core.zookeeper.PoolableZooKeeperFactory Maven / Gradle / Ivy
The newest version!
package com.quincy.core.zookeeper;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper.States;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class PoolableZooKeeperFactory implements PooledObjectFactory {
private String url;
private int timeout;
private Watcher watcher;
private volatile ObjectPool pool;
public PoolableZooKeeperFactory(String url, int timeout, Watcher watcher) {
this.url = url;
this.timeout = timeout;
this.watcher = watcher;
}
@Override
public PooledObject makeObject() throws Exception {
PoolableZooKeeper pzk = new PoolableZooKeeper(url, timeout, watcher, pool);
return new DefaultPooledObject(pzk);
}
@Override
public void destroyObject(PooledObject p) throws Exception {
log.info("ZK_POOL=======================================destroyObject");
p.getObject().reallyClose();
}
@Override
public boolean validateObject(PooledObject p) {
log.info("ZK_POOL=======================================validateObject");
States states = p.getObject().getState();
try {
log.info("ZK_POOL------------------isAlive: {}===isConnected: {}", states.isAlive(), states.isConnected());
if(states.isAlive()) {
log.info("ZK_POOL------------------OBJ_VALIDATION_TRUE");
return true;
} else {
log.info("ZK_POOL------------------OBJ_VALIDATION_FALSE");
return false;
}
} catch (final Exception e) {
log.error("\r\nOBJECT_VALIDATION_ERR: \r\n", e);
return false;
}
}
@Override
public void activateObject(PooledObject p) throws Exception {
log.info("ZK_POOL=======================================activateObject");
}
@Override
public void passivateObject(PooledObject p) throws Exception {
log.info("ZK_POOL=======================================passivateObject");
}
public ObjectPool getPool() {
return pool;
}
public void setPool(ObjectPool pool) {
this.pool = pool;
}
}