Last active
August 29, 2015 14:03
-
-
Save isterin/645a230b4a930be2ef7c to your computer and use it in GitHub Desktop.
VoltDB Client Provider for Guice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VoltDbClientProvider @Inject()(settings: AppSettings) extends Provider[Client] { | |
val cf = settings.getSection("voltdb") | |
val nodes = cf.getConfigList("nodes").toList | |
var client: Option[Client] = None | |
private val executor = Executors.newCachedThreadPool(new ThreadFactory() { | |
def newThread(runnable: Runnable): Thread = { | |
val thread = new Thread(runnable, "Retry Connection") | |
thread.setDaemon(true) | |
thread | |
} | |
}) | |
val clientConfig = new ClientConfig(cf.getString("username"), cf.getString("password"), new ClientStatusListenerExt { | |
override def connectionLost(hostname: String, port: Int, connectionsLeft: Int, cause: DisconnectCause) = { | |
client match { | |
case Some(c) => | |
executor.execute(new Runnable { | |
override def run() = connectToHosts(c, (hostname, port)) | |
}) | |
} | |
} | |
}) | |
def connectToHosts(client: Client, hosts: (String, Int)*) { | |
hosts.foreach { | |
case (h: String, p: Int) => | |
client.createConnection(h, p) | |
} | |
} | |
def get() = { | |
client = Some(ClientFactory.createClient(clientConfig)) | |
val hosts = nodes.map(n => (n.getString("host"), n.getInt("port"))) | |
connectToHosts(client.get, hosts: _*) | |
client.get | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment