Created
November 12, 2014 11:19
-
-
Save strobe/452c709c043c71d9e753 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
class RouterActor extends Actor with ActorLogging { | |
// Our worker Actor handles the work of the request. | |
var router = Router(RoundRobinRoutingLogic(), Vector.empty) | |
def receive = { | |
// router related handlers | |
case AddRoutee1(actor: ActorRef) => | |
val a: ActorRefRoutee = ActorRefRoutee(actor) | |
router = router.addRoutee(a) | |
case RemoveRoutee1(actor: ActorRef) => | |
router.removeRoutee(ActorRefRoutee(actor)) | |
case TestSimpleResponse => { | |
router.route(TestSimpleResponse, sender()) | |
} | |
} | |
class TestServerActor extends Actor with ChunksTestService with ActorLogging { | |
def actorRefFactory = context | |
def receive = { | |
runRoute(apiRoute) | |
} | |
} | |
trait TestService extends HttpService { | |
implicit def executionContext = actorRefFactory.dispatcher | |
implicit val timeout = Timeout(5.seconds) | |
val router = actorRefFactory.actorOf(Props[RouterActor], "routerActor") | |
val apiRoute = | |
path("ask_test") { | |
detach() { | |
get { | |
doTestAskResponse() | |
} | |
} | |
} | |
def doTestAskResponse() = { | |
val response: Future[String] = (router ? TestSimpleResponse) | |
.mapTo[String] // map response to specific type | |
.recover { case e => s"error: ${e}" } | |
complete(response) | |
} | |
} |
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
class BackendActor extends Actor with ActorLogging { | |
val cluster = Cluster(context.system) | |
// subscribe to cluster changes, MemberUp | |
// re-subscribe when restart | |
override def preStart(): Unit = cluster.subscribe(self, classOf[MemberUp]) | |
override def postStop(): Unit = cluster.unsubscribe(self) | |
// we use the enclosing ActorContext's or ActorSystem's dispatcher | |
// for our Futures and Scheduler | |
implicit def executionContext: ExecutionContextExecutor = context.dispatcher | |
def receive = { | |
// this looking for new members of cluster and trying to register on it | |
case state: CurrentClusterState => | |
state.members.filter(_.status == MemberStatus.Up) foreach register | |
case MemberUp(m) => | |
register(m) | |
// | |
case TestSimpleResponse => { | |
sender ! "TestSimpleResponse" | |
} | |
} | |
// method for send registration message to frontend actor | |
def register(member: Member): Unit = | |
if (member.hasRole("frontend")) | |
context.actorSelection(RootActorPath(member.address) / "user" / "frontend") ! | |
BackendRegistration | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment