简体   繁体   English

是否可以在 Play Framework 2.5 上使用 Java Akka 访问 actor 中的 Http 上下文

[英]Does is it possible access Http context in actor using Java Akka on Play Framework 2.5

I'm using JavaAkka to execute some tasks when the Actor receive any message.当 Actor 收到任何消息时,我正在使用 JavaAkka 来执行一些任务。 When my actor receive a message, I'd like to execute some functions from one of the my controllers, however there are inside of these functions uses of http context as:当我的演员收到一条消息时,我想从我的控制器之一执行一些函数,但是在这些函数内部使用 http 上下文:

ctx().messages().at("sensor_alert");

or或者

session().get("id");

And I get the error:我得到错误:

[error] 2017-02-02 13:06:54 -0300 a.a.OneForOneStrategy - There is no HTTP Context available from here.
java.lang.RuntimeException: There is no HTTP Context available from here.
    at play.mvc.Http$Context.current(Http.java:62)
    at play.mvc.Controller.ctx(Controller.java:27)
    at controllers.IpmiMonitorController.gerarAlertas(IpmiMonitorController.java:370)
    at actor.IpmiActor.onReceive(IpmiActor.java:27)
    at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:165)
    at akka.actor.Actor$class.aroundReceive(Actor.scala:484)
    at akka.actor.UntypedActor.aroundReceive(UntypedActor.scala:95)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
    at akka.actor.ActorCell.invoke(ActorCell.scala:495)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)

Is there any solution?有什么解决办法吗?

Your HTTP context should go no further than your controller layer, otherwise you'll find yourself with inappropriate coupling.您的 HTTP 上下文不应超出控制器层,否则您会发现自己处于不适当的耦合中。

Additionally, why would you invoke a method on a controller from an actor?此外,为什么要从参与者调用控制器上的方法? Controllers are meant to handle incoming HTTP requests.控制器旨在处理传入的 HTTP 请求。 Is it possible this method doesn't belong in a controller?这个方法可能不属于控制器吗?

If you're generating alerts, where are the alerts going to?如果您要生成警报,警报会去哪里? If it's the database, inject a DAO into the actor.如果是数据库,则将 DAO 注入到 actor 中。 If it's going into a HTTP request, inject a web service client or similar into the actor.如果它进入一个 HTTP 请求,将一个 web 服务客户端或类似的东西注入到 actor 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM