Start fetching information from zookeeper on load of the main page

This commit is contained in:
R. Tyler Croy 2014-11-23 15:49:48 -08:00
parent 9f06b78fa8
commit 3665149fb3
8 changed files with 72 additions and 10 deletions

View File

@ -38,5 +38,11 @@ dependencies {
testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
}
////////////////////////////////////////////////////////////////////////////////
test {
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
}
}

View File

@ -1,14 +1,17 @@
package offtopic
package offtopic.curator
import org.apache.curator.retry.ExponentialBackoffRetry
import org.apache.curator.framework.CuratorFrameworkFactory
class CuratorClient {
private def client = null
def client = null
public CuratorClient(String zookeepers) {
if (zookeepers?.length() <= 0) {
throw new offtopic.errors.ConfigurationError("Cannot pass an empty string to CuratorClient()")
}
def retry = new ExponentialBackoffRetry(1000, 3)
this.client = CuratorFrameworkFactory.newClient(zookeepers, retry)
this.client.start()
}
}

View File

@ -0,0 +1,29 @@
package offtopic.curator
import org.apache.commons.pool2.*
import org.apache.commons.pool2.impl.DefaultPooledObject;
/**
* Object factory class for keeping an object pool of CuratorClient objects
*
* @author R. Tyler Croy
*/
class CuratorClientObjectFactory extends BasePooledObjectFactory<CuratorClient> {
private String zookeepers = null
public CuratorClientObjectFactory(String zks) {
this.zookeepers = zks
}
@Override
CuratorClient create() {
return new CuratorClient(this.zookeepers)
}
@Override
PooledObject<CuratorClient> wrap(CuratorClient client) {
return new DefaultPooledObject<CuratorClient>(client)
}
}

View File

@ -1,11 +1,11 @@
package offtopic
package offtopic.curator
import org.apache.commons.pool2.impl.GenericObjectPool
@Singleton(strict=false)
class CuratorPool {
GenericObjectPool<CuratorClient> pool
class CuratorPool extends GenericObjectPool<CuratorClient>{
private CuratorPool() {
/** XXX: Figure out how to get ZK from settings */
super(new CuratorClientObjectFactory('localhost:2181'))
}
}

View File

@ -6,7 +6,7 @@ import static ratpack.jackson.Jackson.json
import static ratpack.groovy.Groovy.*
import static ratpack.websocket.WebSockets.websocket
import offtopic
import offtopic.curator.CuratorPool
ratpack {
bindings {
@ -15,6 +15,16 @@ ratpack {
}
handlers {
get {
curator = null
try {
curator = CuratorPool.instance.borrowObject()
println curator.client.getChildren().forPath('/')
}
finally {
if (curator != null) {
CuratorPool.instance.returnObject(curator)
}
}
render 'offtopic!'
}

View File

@ -0,0 +1,14 @@
package offtopic.curator
import spock.lang.Specification
class CuratorClientObjectFactorySpec extends Specification {
def "instantiation with zookeepers"() {
when:
def ccof = new CuratorClientObjectFactory('hello')
then:
ccof.zookeepers == 'hello'
}
}

View File

@ -1,4 +1,4 @@
package offtopic
package offtopic.curator
import spock.lang.Specification

View File

@ -1,4 +1,4 @@
package offtopic
package offtopic.curator
import spock.lang.Specification