0

While using persistent actors and using jdbc for the journal. All messages to my persistent actor are dead letters. However, I cannot see the reason for this since I send them directly to the persistent actor.

Persistent actor code:

case class ExampleState(events: List[String] = Nil) {
  def updated(evt: Evt): ExampleState = copy(evt.data :: events)
  def size: Int = events.length
  override def toString: String = events.reverse.toString
}

class ExampleActor extends PersistentActor {
  override def persistenceId = "sample-id-1"

   var state = ExampleState()

  def updateState(event: Evt): Unit = {
    state = state.updated(event)
  }

  def numEvents =
    state.size

  override def receiveRecover: Receive = {
    case evt: Evt                                 => updateState(evt)
    case SnapshotOffer(_, snapshot: ExampleState) => state = snapshot
  }

  val snapShotInterval = 1000

  override def receiveCommand: Receive= {
    case Cmd(data) => {
      println("in the command code block")
      persist(Evt(s"${data}-${numEvents}")) { event => {
        updateState(event)
        context.system.eventStream.publish(event)
        if (lastSequenceNr % snapShotInterval == 0 && lastSequenceNr != 0)
          saveSnapshot(state)
      }
      }
    }
    case Shutdown => context.stop(self)
    case "print"=>println(state)
  }
}

Test code (all messages sent to persistent actor are the dead letters):

  "The example persistent actor" should {
    "Test Command" in {
      val persistentActor = system.actorOf(Props[ExampleActor](),"examplePersistentactor")
      Thread.sleep(2000)
      println("before the send")
      persistentActor ! Cmd("foo")
      persistentActor ! Cmd("bar")
      persistentActor ! Cmd("fizz")
      persistentActor ! Cmd("buzz")
      persistentActor ! "print"

      Thread.sleep(10000)
      persistentActor ! Shutdown
      println("after messages should be sent and received")
    }
  }
Dirac
  • 25
  • 5

2 Answers2

0

Dead letters happens when there is no actor instance properly running. The delivery message process is the same no matter whether the actor to which message was sent is a persistent actor or not.

So, I suppose your persistent actor is not actually running when you send a message to it. That could be because of persistence settings are not configured properly.

I ran your code (changing Cmd and Evt for String type) using In-Memory persistence and it worked.

0

Thanks for your reply! Are you by any chance familiar with the jdbc plugin? I configured the journal/snapshot and slick connection to the database and it seems to fit with the documentation of the plugin. Is there anything you can see that is wrong/missing by any chance? application.conf:

akka {
 loglevel = DEBUG
 }

# Copyright 2016 Dennis Vriend
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# general.conf is included only for shared settings used for the akka-persistence-jdbc tests
include "general.conf"

akka {
 persistence {
   journal {
     plugin = "jdbc-journal"
     # Enable the line below to automatically start the journal when the actorsystem is started
     auto-start-journals = ["jdbc-journal"]
   }
   snapshot-store {
     plugin = "jdbc-snapshot-store"
     # Enable the line below to automatically start the snapshot-store when the actorsystem is started
     auto-start-snapshot-stores = ["jdbc-snapshot-store"]
   }
 }
}

jdbc-journal {
 slick = ${slick}
}

# the akka-persistence-snapshot-store in use
jdbc-snapshot-store {
 slick = ${slick}
}

# the akka-persistence-query provider in use
jdbc-read-journal {
 slick = ${slick}
}

slick {
 profile = "slick.jdbc.MySQLProfile$"
 db {
   url = "jdbc:mysql://localhost:3306/squirrel_persistence/mysql?cachePrepStmts=true&cacheCallableStmts=true&cacheServerConfiguration=true&useLocalSessionState=true&elideSetAutoCommits=true&alwaysSendSetIsolation=false&enableQueryTimeouts=false&connectionAttributes=none&verifyServerCertificate=false&useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&useLegacyDatetimeCode=false&serverTimezone=UTC&rewriteBatchedStatements=true"
   user = "root"
   password = ""
   driver = "com.mysql.jdbc.Driver"
   numThreads = 5
   maxConnections = 5
   minConnections = 1
 }
}
Dirac
  • 25
  • 5