Added simple router example

parent 8eefb568
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
package no.nsd.akkapres
import akka.actor.{Props, ActorSystem}
import akka.actor.{ActorSystem, Props}
import no.nsd.akkapres.counter.CounterActor
import no.nsd.akkapres.greeting.GreeterResponseActor
import scala.concurrent.duration._
/**
* Where you would start an actual application.
*
......@@ -15,5 +13,5 @@ import scala.concurrent.duration._
object AkkaPres extends App {
implicit val system = ActorSystem("akkapres")
system.actorOf(Props(classOf[GreeterResponseActor]), "greeter")
system.actorOf(Props(classOf[CounterActor], 1 second), "counter")
system.actorOf(Props(classOf[CounterActor]), "counter")
}
package no.nsd.akkapres.router
import akka.actor.{ActorLogging, Actor}
case class Message(content: String)
class MessageActor extends Actor with ActorLogging {
def receive = {
case m: Message =>
log.info(m.content)
sender() ! m.content
case _ =>
log.info("Unable to handle it.")
}
}
\ No newline at end of file
package no.nsd.akkapres.router
import akka.actor._
import akka.routing.{ActorRefRoutee, RoundRobinRoutingLogic, Router}
/**
* @author Dag Østgulen Heradstveit
*/
class MessageRouterActor(routeeSize: Int) extends Actor with ActorLogging {
var router = {
val routees = Vector.fill(routeeSize) {
val routee = context.actorOf(Props[MessageActor])
context watch routee
ActorRefRoutee(routee)
}
Router(RoundRobinRoutingLogic(), routees)
}
def receive = {
case m: Message =>
router.route(m, sender())
case _ =>
log.info("Empty message")
case Terminated(actor) =>
router = router.removeRoutee(actor)
val routee = context.actorOf(Props[MessageActor])
context watch routee
router = router.addRoutee(routee)
log.info("Terminated")
}
}
object MessageRouter extends App {
val routeeSize = 5
implicit val system = ActorSystem("akkapres")
val a = system.actorOf(Props(classOf[MessageRouterActor], routeeSize), "message-router")
for(x <- 1 to routeeSize * routeeSize * 100000){
a ! Message("This is our message to any actor in the router.")
}
}
package no.nsd.akkapres.counter
import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, TestKit}
import org.scalatest.{BeforeAndAfter, Matchers, FlatSpecLike}
class CounterRouterActorTest(_system: ActorSystem) extends TestKit(_system)
with ImplicitSender with FlatSpecLike with Matchers with BeforeAndAfter {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment