forked from spirom/spark-streaming-with-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbeddedZookeeper.scala
54 lines (48 loc) · 1.51 KB
/
EmbeddedZookeeper.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package util
import java.io.{IOException, File}
import java.net.InetSocketAddress
import com.typesafe.scalalogging.Logger
import org.apache.zookeeper.server.{NIOServerCnxnFactory, ZooKeeperServer, ServerCnxnFactory}
/**
* Start/stop a single Zookeeper instance for use by EmbeddedKafkaServer. Do not create one of these directly.
* @param port
*/
private[util] class EmbeddedZookeeper(port: Int, tempDirs: TemporaryDirectories) {
private val LOGGER = Logger[EmbeddedZookeeper]
private var serverConnectionFactory: Option[ServerCnxnFactory] = None
/**
* Start a single instance.
*/
def start() {
LOGGER.info(s"starting Zookeeper on $port")
try {
val zkMaxConnections = 32
val zkTickTime = 2000
val zkServer = new ZooKeeperServer(tempDirs.zkSnapshotDir, tempDirs.zkLogDir, zkTickTime)
serverConnectionFactory = Some(new NIOServerCnxnFactory())
serverConnectionFactory.get.configure(new InetSocketAddress("localhost", port), zkMaxConnections)
serverConnectionFactory.get.startup(zkServer)
}
catch {
case e: InterruptedException => {
Thread.currentThread.interrupt()
}
case e: IOException => {
throw new RuntimeException("Unable to start ZooKeeper", e)
}
}
}
/**
* Stop the instance if running.
*/
def stop() {
LOGGER.info(s"shutting down Zookeeper on $port")
serverConnectionFactory match {
case Some(f) => {
f.shutdown
serverConnectionFactory = None
}
case None =>
}
}
}