Akka ask doesn't seem to send a message to the actor

109 Views Asked by At

I am trying to create a simple simulation using Akka Cluster + Akka Http. The idea is that the frontend is going to process incoming http get and post requests. The server is launched after the frontend actor is spawned. At some point the http requests were processed in a right way, but after I added some logic, there areonly timeouts. It seems the ask message is not even being sent, at least logs indicating that are not printed out in console. Rolling back changes didn't help. The simplified code is below. This is the main app -> a lot has been taken from akka-samples

object App
{
object RootBehavior {
  sealed trait Command
  final case class Done(str: String) extends Command

  var cluster: Cluster = _
  var frontend: ActorRef[FrontendMessage] = _


  def apply(): Behavior[Command] = Behaviors.setup {
  context =>
  cluster = Cluster(context.system)
  
  if (cluster.selfMember.hasRole("frontend")) {
     frontend = context.spawn(Frontend(), "Frontend")
     val route: Route = path("update") {
     get {
         {
        implicit val timeout: Timeout = 5.seconds
        implicit val scheduler: Scheduler = context.system.scheduler
        val response: Future[RootBehavior.Done] = frontend.ask(_ => Frontend.GetRequest(context.self))
        onComplete(response) {
        case scala.util.Success(value) => complete(value.str)
        case scala.util.Failure(exception) => complete(exception.getMessage)
        }
         }
          } ~ post {
            parameters("value")  {
              implicit val timeout: Timeout = 5.seconds
              implicit val scheduler: Scheduler = context.system.scheduler
              value =>
              val response: Future[Done] = frontend.ask(_ => Frontend.PostRequest(value, context.self))
              onComplete(response) {
              case scala.util.Success(value) => complete(value.str)
              case scala.util.Failure(exception) => complete(exception.getMessage)}
            }
             }
              }
        implicit val system: ActorSystem[_] = ActorSystem(Behaviors.empty, "Example")
          val bindingFuture = Http().newServerAt("localhost", 7001).bind(route)
          println(s"Server online at http://localhost:7001/\nPress RETURN to stop...")
          StdIn.readLine()
          implicit val executionContext: ExecutionContextExecutor = context.executionContext
          bindingFuture
            .flatMap(_.unbind())
            .onComplete(_ => system.terminate())
        }
        Behaviors.empty
    }
  }

  def main(args: Array[String]):
  Unit = {
    startup(25252, "frontend")
  }

  def startup(port: Int, role: String): Unit = {
    val config = ConfigFactory.parseString(
      s"""
    akka.remote.artery.canonical.port=$port
     akka.cluster.roles = [$role]
    """).withFallback(ConfigFactory.load("clustering.conf"))

    val system =  ActorSystem[Command](RootBehavior(), "ClusterSystem", config)

}

}

This is the frontend code

object Frontend
{
  trait FrontendMessage

  final case class PostRequest(value: String, sender: ActorRef[RootBehavior.Command]) extends FrontendMessage
  final case class GetRequest(sender: ActorRef[RootBehavior.Command]) extends FrontendMessage

  def apply(): Behavior[FrontendMessage] = {
    Behaviors.setup { context =>

      context.log.debug("Frontend started")
      val databaseValue: String = "basic"
      Behaviors.receiveMessage[FrontendMessage] {
        case PostRequest(value, sender) =>
          context.log.debug("Received a post request")
          sender ! RootBehavior.Done("DONE")
          Behaviors.same
        case GetRequest(sender) =>
          context.log.debug("Received a get request")
          sender ! RootBehavior.Done(databaseValue)
          Behaviors.same
      }}}}

The logs look pretty good. The cluster is created, no errors or warnings.

INFO akka.cluster.Cluster - Cluster Node [akka://[email protected]:25252] - Registered cluster JMX MBean [akka:type=Cluster,port=25252]
INFO akka.cluster.Cluster - Cluster Node [akka://[email protected]:25252] - Started up successfully

I tried various ways to send an ask request and adding more logs, but still the message doesn't seem to reach the Frontend actor.

Added: From there the the breakpoints can't be reached, no logs are being printed. From there the the breakpoints can't be reached, no logs are being printed.

0

There are 0 best solutions below