According to the Akka.Net documentation, PersistentView is deprecated, and PersistenceQuery should be used instead. In an ASP.Net Core 2.0 Web-API application I am using Akka.Net with event sourcing. I am using the SQL Server plugin for persistence, with events and snapshots. For persistent views I want to start using PersistenceQuery. When the application starts, the events are played back to restore the states of the actors.
I have implemented a journal reader, that is receiving events, and uses it to compose a view. Question is, how can I tell that the last played back event has arrived, so that the composed view can be saved (as a sort of snapshot)? I don't want to save the view after every event during the restore phase.
The journal reader is now started when the ActorSystem is initialised (called via Startup.cs). The code looks like this:
private static void InitialiseJournalReader()
{
// Obtain read journal by plugin id.
var readJournal = PersistenceQuery.Get(ActorSystem).ReadJournalFor<SqlReadJournal>("akka.persistence.query.myjournal");
// Materialize stream, consuming events.
var materializer = ActorMaterializer.Create(ActorSystem);
var writer = ActorSystem.ActorOf(CreateViewsActor.GetActorProps(), CreateViewsActor.GetActorName());
// issue query to journal
Source<EventEnvelope, NotUsed> source = readJournal.CurrentEventsByTag("MyEvents");
source.RunForeach(envelope => writer.Ask(envelope.Event), materializer);
}
CreateViewsActor
is an Actor, which uses the messages to create one or more views. It also has to save these views (currently in JSON-format into an SQL Server table).
Unfortunately so far I have not found a working example of creating persistent views through a journal reader. But maybe I have been looking in the wrong places. So far I have the following questions:
- Are there any working examples of creating persistent views through a journal reader?
- How can the CreateViewsActor (or any code responsible for creating and saving the views) know that all recovery messages have been processed?
- What is the best place to initialise the journal readers?