Why doesn't the Akka Persisence Query Read Journal retrieve my events?

本小妞迷上赌 提交于 2019-12-08 01:01:34

问题


I have problems understanding Akka Persistence Query, especially the method eventsByTag since it doesn't behave like I expect.

In my main class I call a class that starts listening to any events being persisted with a certain tag.

class CassandraJournal(implicit val system: ActorSystem) {

 def engageStreaming = {
   val readJournal = PersistenceQuery(system).readJournalFor[CassandraReadJournal](CassandraReadJournal.Identifier)
   implicit val mat = ActorMaterializer()

   readJournal.eventsByTag("account", Offset.noOffset)
     .runForeach { event => println(event) }
   }
}

Whenever I start my server and my event store is empty and I persist my first event (by calling a http service, built in Akka HTTP), the event indeed gets printed. However, when I restart the server and there are already events in the event store, the new persisted events won't get printed.

Is there an explanation for this? I have a hard time figuring out why this is happening.

EDIT

The event store I'm using is Cassandra. Here is the PersistentActor (I am not using an Event Adapter to tag the events, just wrap them around a Tagged())

class Account(id: UUID) extends PersistentActor {

  override def receiveRecover: Receive = {
    case createCheckingsAccount: CreateCheckingsAccount =>
      println("Creating checkings account")
  }

  override def receiveCommand: Receive = {
    case createCheckingsAccount: CreateCheckingsAccount =>
      persist(Tagged(CheckingsAccountCreated(id), Set("account"))) { event =>
        val checkingsAccountCreatedEvent = event.payload.asInstanceOf[CheckingsAccountCreated]
        sender ! CreateCheckingsAccountResponse(checkingsAccountCreatedEvent.id.toString)
      }

  }

  def updateState(evt: Event): Unit = {
  }

  override def persistenceId: String = s"account-$id"
}

回答1:


With receiveRecover not doing the necessary state recovery work, persistence wouldn't work properly. I would suggest putting some basic state recovery logic in receiveRecover and have your updateState method cover also tagged event cases.

I used eventsByTag in an app with state recovery logic similar to the following and it worked fine on both fresh start and recovery.

def updateState(e: Any): Unit = e match {
  case evt: Event =>
    state = state.updated(evt)
  case Tagged(evt: Event, _) =>
    state = state.updated(evt)
}

...

override def receiveRecover: Receive = {
  case evt: Event => updateState(evt)
  case taggedEvt: Tagged => updateState(taggedEvt)
}


来源:https://stackoverflow.com/questions/43171143/why-doesnt-the-akka-persisence-query-read-journal-retrieve-my-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!