New to actors and I must be missing something trivial though tried many variations. Here is what I have:
import akka.actor.typed.ActorRef
import akka.actor.typed.ActorSystem
import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.AskPattern._
import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.SupervisorStrategy
import akka.cluster.typed.ClusterSingleton
import akka.cluster.typed.SingletonActor
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import java.util.concurrent.TimeUnit
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Failure
import scala.util.Success
object ClusteredApiRequest {
case class RequestContextCommand(request: String, replyTo: ActorRef[Int])
def apply(): Behavior[RequestContextCommand] =
Behaviors.setup { ctx =>
ctx.log.info(“Setup worked”)
println(“Setup worked over println”) // This is printed
Behaviors.receiveMessage[RequestContextCommand] {
case RequestContextCommand(request, replyTo) =>
println(s"Received a request request") // This is not printed
ctx.log.info( s"Received a request {request}")
replyTo ! 100
Behaviors.same
case _ =>
println(“Help me, unexpected”)
Behaviors.same
}
}
}
// Override the configuration of the port
val config = ConfigFactory.parseString(s"""
akka.remote.artery.canonical.hostname=“127.0.0.1”
akka.remote.artery.canonical.port=2552
akka.cluster.seed-nodes = [“akka://ClusterSystem@127.0.0.1:2552”],
akka.actor.provider=cluster
akka.actor.allow-java-serialization=on
akka.cluster.singleton.singleton-name = “api”
akka.cluster.singleton-proxy.singleton-name = “api”
“”").withFallback(ConfigFactory.load())
// Create an Akka system
val clusterActorSystem = ActorSystem[ClusteredApiRequest.RequestContextCommand](ClusteredApiRequest(), “api”, config)
val singletonManager = ClusterSingleton(clusterActorSystem)
val apiProxyActorRef: ActorRef[ClusteredApiRequest.RequestContextCommand] =
singletonManager.init(
SingletonActor(
Behaviors
.supervise(ClusteredApiRequest())
.onFailureException,
“api”)
)
val timeout = Timeout(FiniteDuration(15, TimeUnit.SECONDS))
val scheduler = clusterActorSystem.scheduler
val result = apiProxyActorRef.ask(ClusteredApiRequest.RequestContextCommand(“Hello”, _))(timeout, scheduler)
result.onComplete{
case Success(result: Int) =>
println(s"Result was $result")
case Failure(exception) =>
println(“Failed”, exception)
}
// Prints: (Failed,java.util.concurrent.TimeoutException: Ask timed out on [Actor[akka://api/system/singletonProxyapi-no-dc#-1785592397]] after [15000 ms]. Message of type [$line18.$read$$iw$$iw$ClusteredApiRequest$RequestContextCommand]. A typical reason for AskTimeoutException
is that the recipient actor didn’t send a reply.)