This file contains 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
▶ curl --silent http://127.0.0.1:8083/stats | python -m json.tool | |
{ | |
"rollsCounts": [ | |
{ | |
"rolledNumber": 2, | |
"rollsCount": 5 | |
}, | |
{ | |
"rolledNumber": 1, | |
"rollsCount": 5 |
This file contains 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
curl http://127.0.0.1:8083/stats | |
// Output: | |
{"rollsCounts":[]} |
This file contains 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
docker run -d -p 5672:5672 -p 15672:15672 dockerfile/rabbitmq | |
sbt "project game" run | |
sbt "project webapp" run | |
sbt "project statistics" run |
This file contains 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
case class RollCount(rolledNumber: Int, rollsCount: Int) | |
case class StatsResponseData(rollsCounts: Seq[RollCount]) |
This file contains 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 GetStatsRequestActor( | |
ctx: RequestContext, | |
statsActor: ActorRef)(implicit override val json4sFormats: Formats) | |
extends Actor | |
with Json4sSupport { | |
statsActor ! StatsActor.GetState | |
context.setReceiveTimeout(2.seconds) |
This file contains 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
val statsRoute = (path("stats") & get)(handleGetStats) | |
private def handleGetStats: Route = { ctx => | |
actorRefFactory.actorOf(GetStatsRequestActor.props(ctx, statsActor)) | |
} |
This file contains 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
case class Stats(rollsCounts: Map[Int, Int]) { | |
def incRollsCount(rolledNumber: Int) = { | |
val currentCount = rollsCounts.getOrElse(rolledNumber, 0) | |
copy(rollsCounts = rollsCounts + (rolledNumber -> (currentCount + 1))) | |
} | |
} |
This file contains 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
case class IncRollsCount(rolledNumber: Int) extends Command | |
case class RollsCountIncreased(rolledNumber: Int) extends Event | |
case object GetState extends Command | |
class StatsActor extends PersistentActor { | |
override val persistenceId = "rolls_stats" | |
var state: Stats = Stats(Map.empty) |
This file contains 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 SubscriberActor(statsActor: ActorRef) extends ActorSubscriber { | |
override def receive = { | |
case OnNext(message: Message) => | |
for { | |
eventType <- message.headers.get("type") if eventType == "DiceRolled" | |
content <- parseOpt(message.body.decodeString("UTF-8")) | |
JInt(rolledNumber) <- content \ "rolledNumber" | |
} statsActor ! StatsActor.IncRollsCount(rolledNumber.toInt) | |
case OnComplete => |
This file contains 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
implicit val connection = Connection() | |
val queue = Queue(queueName, durable = false, autoDelete = true) | |
val bindArgs = Map("x-match" -> "all", "type" -> "DiceRolled") | |
val resultFuture = for { | |
_ <- connection.queueDeclare(queue) | |
_ <- connection.queueBind(queueName, exchangeName, "", bindArgs) | |
} yield () | |
resultFuture onComplete { |
NewerOlder