Skip to content

Instantly share code, notes, and snippets.

@bleis-tift
Forked from karno/AgentRuntime.scala
Last active December 18, 2015 01:39
Show Gist options
  • Save bleis-tift/5705211 to your computer and use it in GitHub Desktop.
Save bleis-tift/5705211 to your computer and use it in GitHub Desktop.
仕事の片手間に単純に書き換えただけなのでアレだけど。Scalaってシャドウイングできたっけ・・・レベル
class AgentRuntime(profile: Properties, holder: ServiceBindingHolder[MicroRuntimeServiceBinder]) {
def startAgent(name: String, args: Array[AnyRef]): Future[AgentController] =
for {
binder <- holder.getServiceBinder()
binder <- createContainer(binder)
binder <- startAgentCore(name, args, binder)
} yield MicroRuntime.getAgent(name)
private def startAgentCore[T <: Agent](name: String, args: Array[AnyRef],
serviceBinder: MicroRuntimeServiceBinder): Future[MicroRuntimeServiceBinder] = {
val p = Promise[MicroRuntimeServiceBinder]
serviceBinder.startAgent(name, classOf[T].getCanonicalName, args, new RuntimeCallback[Void] {
def onFailure(t: Throwable) {
p.failure(t)
}
def onSuccess(_null: Void) {
p.success(serviceBinder)
}
})
p.future
}
private def createContainer(serviceBinder: MicroRuntimeServiceBinder): Future[MicroRuntimeServiceBinder] = {
MicroRuntime.isRunning match {
case true => future(serviceBinder)
case false => {
val p = Promise[MicroRuntimeServiceBinder]()
serviceBinder.startAgentContainer(profile, new RuntimeCallback[Void] {
def onFailure(t: Throwable) {
p.failure(t)
}
def onSuccess(_null: Void) {
p.success(serviceBinder)
}
})
p.future
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment