如何在所需的各个地方使用顶级ActorSystem?

时间:2015-08-04 22:06:33

标签: scala akka akka-http

我正在尝试学习akka-http并正在处理他们的example

这是我的代码看起来像

  val system = ActorSystem.create("enterpriseSystem", ConfigFactory.load("application"))
  val notifier = system.actorOf(Props[Notifier], "notifier")

和Notifier as

class Notifier extends Actor with ActorLogging {

  implicit val system = ActorSystem()
  implicit val materializer = ActorMaterializer()
  import scala.concurrent.ExecutionContext.Implicits.global

  def receive = {
    case CommunicateECFailure =>
      log.info("notifying about EC Failure")
      val responseFuture: Future[HttpResponse] =
        Http().singleRequest(HttpRequest(uri = "http://localhost:8080"))

      responseFuture onComplete {
        case response =>
          log.info("response received {}", response)
          log.info("notified about EC Failure")
      }
  }

正如您所看到的,每创建一次ActorSystem我都会创建新的Actor,那是不是很糟糕?我在akka文档中读到你不应该有很多ActorSystems

我该如何避免?在施工期间将其作为论据传递?

1 个答案:

答案 0 :(得分:5)

每次创建新的ActorSystem都是错误的!每个角色ActorSystem都有context

context.system