Skip to content

Instantly share code, notes, and snippets.

@remeniuk
Forked from whiter4bbit/gist:9411873
Last active August 29, 2015 13:57
Show Gist options
  • Save remeniuk/9427490 to your computer and use it in GitHub Desktop.
Save remeniuk/9427490 to your computer and use it in GitHub Desktop.
import com.typesafe.config._
import akka.actor._
import scala.concurrent.Await
import scala.concurrent.duration._
object Storage {
val id = "storage"
}
trait Storage {
def store(id: String)
}
trait StorageActorProvider {
val storage: Storage
}
class StorageActorImpl extends Storage {
def store(id: String) = println(s"Stored: ${id}")
}
trait ProdStorageActorProvider extends StorageActorProvider {
val system = TypedActor.context.system
val storage = TypedActor(TypedActor.context.system).typedActorOf(TypedProps[Storage],
Await.result(TypedActor.context.system.actorSelection(system / Storage.id)
.resolveOne()(5.seconds), 5.seconds)
)
}
object TCPFrontend {
val id = "tcp-frontend"
}
trait TCPFrontend {
def event(id: String)
}
trait TCPFrontendImpl extends TCPFrontend {
self: StorageActorProvider =>
def event(id: String) = storage.store(id)
}
class TCPFrontendProdImpl extends TCPFrontendImpl with ProdStorageActorProvider
object ProdShowCase {
def main(args: Array[String]): Unit = {
val system = ActorSystem("system", ConfigFactory.parseString("akka {}"))
TypedActor(system).typedActorOf(TypedProps[StorageActorImpl], Storage.id)
val ref: TCPFrontend = TypedActor(system).typedActorOf(TypedProps[TCPFrontendProdImpl],
TCPFrontend.id)
ref.event("1")
}
}
object TestShowCase {
def main(args: Array[String]): Unit = {
val ref = new TCPFrontendImpl with StorageActorProvider {
val storage = new StorageActorImpl
}
ref.event("1")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment