我正在尝试学习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
我该如何避免?在施工期间将其作为论据传递?
答案 0 :(得分:5)
每次创建新的ActorSystem
都是错误的!每个角色ActorSystem
都有context
:
context.system