Hi
I am new to akka + playframework. I have been using direct slick run method in play async action method. run(. … ) method returns Future[T]. Now my question is - will it be okay to use actor ask method within the play async action in controller to execute slick run method?
package controllers
import akka.actor.{ActorRef, ActorSystem, Props}
import akka.pattern.ask
import akka.util.Timeout
import javax.inject.{Inject, Singleton}
import play.api.mvc._
import services.actorServices.TestActor
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
@Singleton
class ActorTestController @Inject()(cc: ControllerComponents, actorSystem: ActorSystem)(implicit assetsFinder: AssetsFinder, executionContext: ExecutionContext)
extends AbstractController(cc) {
val testActor: ActorRef = actorSystem.actorOf(Props[TestActor])
implicit val timeout: Timeout = Timeout.apply(2 second)
def test: Action[AnyContent] =Action.async{
(testActor ? "test").mapTo[String].map(string => Ok(string))
}
}
package services.actorServices
import akka.actor.Actor
import services.dbServices.HomePageDAO
import scala.concurrent.ExecutionContext.Implicits.global
class TestActor extends Actor {
override def receive: Receive = {
case "test" =>
HomePageDAO.getSlogan.map { seqOfString =>
sender() ! seqOfString.head
}
}
}
Actually ask does not work with a method which compute in Future :(