A persistent actor is an event sourced actor. It receives a (non-persistent) command which is first validated.
If validation succeeds, events are generated from the command, representing the effect of the command. These events are then persisted and used to change the actor’s state.
When the event sourced actor needs to be recovered, only the persisted events are replayed as they can be successfully applied to recreate the state of the actor.
Event sourced actors may also process commands that do not change application state (such as query commands).
This application showcases the usage of a typed persistent actor that fetches top, latest or best news from hacker news website using its API.
As shown in the diagram below, the application consists of two actors; namely,
NewsApplication
that responds toFetchNews
message. This actor is the entry point and guardian actor to the actor system. Upon creation, it also creates its child actorEventSourcedActor
and sends 5FetchNews
messages to itself.EventSourcedActor
that responds to commands (FetchTopStories
,FetchNewStories
andFetchBestStories
) and generates events (TopStoriesFetched
,NewStoriesFetched
andBestStoriesFetched
) that gather the news from hacker news, provided the state of the persistent actor does not contain the category of news for that day.