Skip to content

Instantly share code, notes, and snippets.

@sjednac
Last active September 25, 2015 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjednac/86687b735f29bda4cf81 to your computer and use it in GitHub Desktop.
Save sjednac/86687b735f29bda4cf81 to your computer and use it in GitHub Desktop.
A minimal property reader for Apache ZooKeeper
name := "zookeeper-reader"
version := "0.1-SNAPSHOT"
scalaVersion := "2.11.7"
resolvers ++= Seq(
"Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/",
"Sonatype Releases" at "http://oss.sonatype.org/content/repositories/releases",
"Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
)
libraryDependencies ++= Seq(
"org.apache.curator" % "curator-framework" % "2.9.0",
"org.slf4j" % "slf4j-simple" % "1.7.12"
)
import java.time._
import org.apache.curator.framework._
import org.apache.curator.retry._
import org.apache.zookeeper.CreateMode._
object ZooKeeperReader extends App {
val connectionString = "192.168.59.201:2181,192.168.59.202:2181,192.168.59.203:2181,192.168.59.204:2181,192.168.59.205:2181"
val retryPolicy = new RetryOneTime(1000)
val client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy)
val startTime = ZonedDateTime.now(ZoneOffset.UTC)
val path = "/test-app/start-time"
try {
client.start()
client.create().creatingParentsIfNeeded().withMode(EPHEMERAL).forPath(path, startTime.toString.getBytes)
for (i <- 1 to 60) {
val bytes = client.getData.forPath(path)
val result = new String(bytes)
println("#"*50)
println(s" Application started at: ${result}")
println("#"*50)
Thread.sleep(1000)
}
} finally {
client.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment