I am trying to find the best way to execute async side effect (or some action) after an event has been persisted.
Here is what I am doing right now, and would appreciate if someone could tell me if there is a better way.
Example 1:
Actor A does something, persists events, and then I use thenRun
with tell
pattern to call Actor B. Actor B has restart with backoff supervisor, so it will try to retry a few times if it fails.
var sideEffectActor = this.context.spawn(SideEffectBehaviour.create(), "sideEffectBehavior");
return Effect()
.persist(persistableEvents)
.thenRun(() -> sideEffectActor.tell(new Message()))
//...
Example 2:
A more complicated case is when Actor B needs to call Actor A. In some cases I need to record additional events that are tied to Actor A behaviour, but I still want it to be async.
I do the same thing as I did in first example, Actor A uses thenRun
with tell
pattern, but the message that is going to Actor B contains an ActorRef. Actor B uses ask
pattern to execute the logic in sync on that ActorRef, and if something fails, retry it.
So is there a better way to do these? Can anything go wrong with the way I do it?