0

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?

kunicmarko20
  • 2,095
  • 2
  • 15
  • 25
  • What kind of side effect do you want to perform? Ask because I am not sure an additional actor B is needed, would be great to have more context around the side effect. – yiksanchan Aug 11 '20 at 15:22
  • @YikSanChan In some cases it is sending an email, or executing some additional logic, in other cases it is recording additional events, if that helps? – kunicmarko20 Aug 11 '20 at 15:58
  • Can these side effects be performed by simply triggering a Future? Or do they have to go through an actor B because the actor contains some data needed to perform the side effect? – yiksanchan Aug 11 '20 at 16:00
  • @YikSanChan Actor B was just introduced to achieve async dispatch of messages, how would I trigger a Future instead? – kunicmarko20 Aug 11 '20 at 16:13
  • @YikSanChan in some cases I would need additional data, so I guess in those cases I need Actor B? – kunicmarko20 Aug 11 '20 at 16:16
  • Assume you have `def sendEmail(content: String): Future[Unit]`, then you can simply `.thenRun(sendEmail("XXX"))` rather than relying on an actor, if you don't need to maintain internal state in that actor – yiksanchan Aug 11 '20 at 16:36
  • @YikSanChan I assume this just means I would return a future to `thenRun` from a method inside of Actor A? – kunicmarko20 Aug 12 '20 at 08:03
  • Yes, that's what I mean – yiksanchan Aug 12 '20 at 15:12

0 Answers0