简体   繁体   中英

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. 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:

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.

Additionally, why would you invoke a method on a controller from an actor? Controllers are meant to handle incoming HTTP requests. 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. If it's going into a HTTP request, inject a web service client or similar into the actor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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