private void detectOverdrawn()

in pekko-sample-persistence-dc-java/src/main/java/sample/persistence/res/bank/BankAccount.java [150:163]


    private void detectOverdrawn(BankAccount.State state, ReplicationContext replicationContext, ActorContext<Command> context) {
        if (
                replicationContext.concurrent() // this event happened concurrently with other events already processed
                        && replicationContext.origin().equals(new ReplicaId("eu-central")) // if we only want to do the side effect in a single DC
                        && !replicationContext.recoveryRunning() // probably want to avoid re-execution of side effects during recovery
        ) {
            // there's a chance we may have gone into the overdraft due to concurrent events due to concurrent requests
            // or a network partition
            if (state.balance < 0) {
                // the trigger could happen here, in a projection, or as done here by sending a command back to self so that an event can be stored
                context.getSelf().tell(new AlertOverdrawn(state.balance));
            }
        }
    }