Move the withCurator method into CuratorPool to use outside of KafkaService

This commit is contained in:
R. Tyler Croy 2014-11-24 21:23:31 -08:00
parent 2677018943
commit 69ed3f3cad
2 changed files with 13 additions and 13 deletions

View File

@ -15,7 +15,7 @@ class KafkaService {
def brokers = new ArrayList()
def parser = new JsonSlurper()
withCurator { c ->
CuratorPool.withCurator { c ->
c.getChildren().forPath(BROKERS_PATH).each { String id ->
// Pulling this into a String buffer since parse(byte[]) is
// throwing a stackoverflow error
@ -29,20 +29,9 @@ class KafkaService {
public static ArrayList fetchTopics() {
ArrayList brokers = null
withCurator { c ->
CuratorPool.withCurator { c ->
brokers = c.getChildren().forPath(TOPICS_PATH)
}
return brokers
}
private static void withCurator(Closure closure) {
def curator = null
try {
curator = CuratorPool.instance.borrowObject()
closure.call(curator.client)
}
finally {
CuratorPool.instance.returnObject(curator)
}
}
}

View File

@ -20,4 +20,15 @@ class CuratorPool extends GenericObjectPool<CuratorClient>{
super(new CuratorClientObjectFactory(zookeepers))
println "CREATING WITH ${zookeepers}"
}
public static void withCurator(Closure closure) {
def curator = null
try {
curator = CuratorPool.instance.borrowObject()
closure.call(curator.client)
}
finally {
CuratorPool.instance.returnObject(curator)
}
}
}