/exercise_102_display_cluster_sharding/src/main/scala/com/lightbend/akka_oled/Main.scala

https://github.com/lightbend/Pi-Akka-Cluster · Scala · 91 lines · 54 code · 16 blank · 21 comment · 0 complexity · 7b8010722c65dcdb79565acb29cc9943 MD5 · raw file

  1. /**
  2. * Copyright © 2016-2020 Lightbend, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. *
  14. * NO COMMERCIAL SUPPORT OR ANY OTHER FORM OF SUPPORT IS OFFERED ON
  15. * THIS SOFTWARE BY LIGHTBEND, Inc.
  16. *
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. package com.lightbend.akka_oled
  21. import akka.NotUsed
  22. import akka.actor.typed.scaladsl.Behaviors
  23. import akka.actor.typed.scaladsl.adapter.TypedActorSystemOps
  24. import akka.actor.typed.{ActorSystem, Behavior, Terminated}
  25. import akka.cluster.sharding.typed.scaladsl.{ClusterSharding, Entity}
  26. import akka.http.scaladsl.Http
  27. import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
  28. import akka.management.scaladsl.AkkaManagement
  29. import akka.persistence.typed.PersistenceId
  30. import akka.stream.Materializer
  31. import akkapi.cluster.{ClusterStatusTracker, OledClusterVisualizer, OledDriver, Settings}
  32. import spray.json._
  33. import scala.concurrent.ExecutionContextExecutor
  34. object Main extends SprayJsonSupport with DefaultJsonProtocol {
  35. case class AddPoints(points: Int)
  36. implicit val transactionFormat = jsonFormat1(AddPoints)
  37. def apply(settings: Settings): Behavior[NotUsed] = Behaviors.setup { ctx =>
  38. implicit val system = ctx.system
  39. implicit val untypedSystem: akka.actor.ActorSystem = ctx.system.toClassic
  40. implicit val ec: ExecutionContextExecutor = ctx.system.executionContext
  41. val oledDriver = ctx.spawn(OledDriver(settings), "oled-driver")
  42. oledDriver ! OledDriver.RegisterView("Cluster State", 0)
  43. oledDriver ! OledDriver.RegisterView("Sharding State", 1)
  44. val clusterView = ctx.spawn(OledClusterVisualizer(0, settings, oledDriver), "oled-cluster-view")
  45. val clusterStatusTracker = ctx.spawn(ClusterStatusTracker(settings, None), "cluster-status-tracker")
  46. clusterStatusTracker ! ClusterStatusTracker.SubscribeVisualiser(clusterView)
  47. val shardVisualizer = ctx.spawn(OledShardingVisualizer(1, oledDriver), "oled-sharding-view")
  48. val sharding = ClusterSharding(ctx.system)
  49. sharding.init(Entity(typeKey = ClientEntity.TypeKey) { entityContext =>
  50. ClientEntity(entityContext.entityId,
  51. PersistenceId(entityContext.entityTypeKey.name, entityContext.entityId),
  52. shardVisualizer)
  53. })
  54. val tracker = ctx.spawn(ShardStateTracker(shardVisualizer), "oled-sharding-tracker")
  55. ctx.spawn(ShardStateScheduler(sharding.shardState, tracker), "oled-sharding-scheduler")
  56. val routes = new Routes(sharding)
  57. //materializer
  58. Materializer.createMaterializer(ctx.system.toClassic)
  59. implicit val mat: Materializer = Materializer.createMaterializer(ctx.system.toClassic)
  60. Http()(ctx.system.toClassic).bindAndHandle(routes.route,
  61. settings.config.getString("cluster-node-configuration.external-ip"), 8080)
  62. Behaviors.receiveSignal {
  63. case (_, Terminated(_)) =>
  64. Behaviors.stopped
  65. }
  66. }
  67. }
  68. object DisplayClusterShardingMain {
  69. def main(args: Array[String]): Unit = {
  70. val settings = Settings()
  71. val system = ActorSystem[NotUsed](Main(settings), "akka-oled", settings.config)
  72. // Start Akka HTTP Management extension
  73. AkkaManagement(system).start()
  74. }
  75. }