Created
September 9, 2013 23:35
-
-
Save nefilim/6502975 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import akka.actor._ | |
import akka.routing.{Broadcast, RoundRobinRouter} | |
import scala.Some | |
// CONNECTION | |
case class Connection(production: Boolean = false, id: Int) { | |
def doStuff(stuff: String) = println(s"stuff [${stuff}] is being done") | |
} | |
trait ConnectionProvider { | |
def getConnection: Connection | |
} | |
trait ProductionConnectionProvider extends ConnectionProvider { | |
override def getConnection: Connection = Connection(true, 1) | |
} | |
trait TestConnectionProvider extends ConnectionProvider { | |
override def getConnection: Connection = Connection(false, 2) | |
} | |
// STORE | |
class ACStore extends Actor { this:ConnectionProvider => | |
def receive = { | |
case "init" => println(s"hi! ${self.path.name}") | |
case _ => println(s"${self.path.name} store got a message"); getConnection doStuff "storing yo" | |
} | |
} | |
class ProductionACStore extends ACStore with ProductionConnectionProvider | |
class TestACStore extends ACStore with TestConnectionProvider | |
trait StoreProvider { | |
val numberOfRoutees = 1 | |
def getACStoreProps: Props | |
} | |
trait ProductionStoreProvider extends StoreProvider { | |
override val numberOfRoutees = 3 | |
override def getACStoreProps = Props[ProductionACStore] | |
} | |
trait TestStoreProvider extends StoreProvider { | |
override val numberOfRoutees = 1 | |
override def getACStoreProps = Props[TestACStore] | |
} | |
// SERVICE | |
class ACService extends Actor { this:StoreProvider => | |
var router: Option[ActorRef] = None | |
override def preStart() { | |
var routees = List[ActorRef]() | |
for (i <- 0 until numberOfRoutees) | |
routees = routees :+ context.actorOf(getACStoreProps) | |
router = Some(context.actorOf(Props.empty.withRouter(RoundRobinRouter(routees = routees)))) | |
} | |
def init = { | |
println("initializing") | |
} | |
def receive = { | |
case "init" => init; router.get ! Broadcast("init") | |
case "go" => println("service got message, forwarding to router"); router.get ! "go" | |
} | |
} | |
class ProductionACService extends ACService with ProductionStoreProvider | |
class TestACService extends ACService with TestStoreProvider | |
object MyApp extends App { | |
implicit val system = ActorSystem() | |
sys.addShutdownHook(system.shutdown()) | |
val service = system.actorOf(Props[ProductionACService], "ACServiceActor") | |
service ! "init" | |
service ! "go" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment