Search code examples
scalapersistenceakka

PersistentActor doesn't receive command


I have implemented the following PersistedActor

import akka.actor._
import akka.persistence._

object TaskDispatcher {

  /**
   * Create Props for the actor
   */
  def props(): Props = Props(new TaskDispatcher())

  case class AddEndpoint(serverEndpoint: ActorRef, id: String)
}

class TaskDispatcher() extends PersistentActor with ActorLogging {
  import TaskDispatcher._

  override def persistenceId = "task-dispatcher-persistence-ID"

  // Actor State
  var endpoints: Map[String, ActorRef] = Map()


  def receiveRecover: Receive = {
    case AddEndpoint(serverEndpoint, id) =>
      endpoints += (id -> serverEndpoint)

  }

  def receiveCommand: Receive = {

    case AddEndpoint(serverEndpoint, id) =>
      log.info("AddEndpoint received")
      persistAsync(AddEndpoint(serverEndpoint, id)) { command =>
        endpoints += (id -> serverEndpoint)
      }
  }

}

I create an instance of the PersistedActor and I send a message AddEndpoint to it through another actor

val taskDispatcher =
    context.actorOf(Props[TaskDispatcher], "task-dispatcher")
taskDispatcher ! AddEndpoint(self, id)

Before I had a non- persistent version of this actor and everything worked. Now the actor doesn't receive AddEndpoint message. I noticed it because log doesn't print "AddEndpoint received" message. What am I doing wrong?


Solution

  • Maybe just recovery doesn't start. Try place:

    override def preStart() = {
      self ! Recover()
    }