【问题标题】:Persistent actor using jdbc dead letters使用 jdbc 死信的持久演员
【发布时间】:2021-03-02 09:46:20
【问题描述】:

在使用持久性参与者并使用 jdbc 作为日志时。给我的持久演员的所有消息都是死信。但是,我看不到原因,因为我将它们直接发送给持久演员。

持久性演员代码:

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)
  }
}

测试代码(发送给持久化actor的所有消息都是死信):

  "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")
    }
  }

【问题讨论】:

    标签: akka akka-persistence


    【解决方案1】:

    当没有正确运行的actor实例时会发生死信。无论消息被发送到的actor是否是持久actor,传递消息的过程都是相同的。

    所以,我想当你向它发送消息时,你的持久性actor实际上并没有运行。这可能是因为没有正确配置持久性设置。

    我使用 In-Memory 持久性运行了您的代码(将 Cmd 和 Evt 更改为 String 类型)并且它有效。

    【讨论】:

      【解决方案2】:

      感谢您的回复!您是否熟悉 jdbc 插件?我配置了日志/快照和与数据库的平滑连接,它似乎符合插件的文档。您是否可以看到任何错误/遗漏的东西? 应用程序.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
       }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-13
        • 2017-11-30
        • 1970-01-01
        • 2023-03-03
        • 2017-09-09
        • 1970-01-01
        • 2015-08-03
        • 1970-01-01
        相关资源
        最近更新 更多