Search code examples
functional-programmingakkaactor

How to access state scoped to current behaviour from postStop


I have an online game, with an actor that represents a user's state. State updates via recursive become calls:

private PartialFunction<Object, BoxedUnit> updatedUser(final User user) {
    return ReceiveBuilder.
        ...
        matchEquals("update", s -> {
            context().become(updatedUser(new User(...)));
        }).build();
}

Now when the user leaves the game (actor stops), I need to save its state to a database. I think the ideal place for this would be sending a message from postStop. But user's state is out of scope.

public void postStop() throws Exception {
    //user state out of scope
    Database.tell(user, self());

}

I don't want to have state as an actor field. What would be the best way to solve this problem?


Solution

  • There's no way to access the user value outside of the updatedUser function.

    Just make your user state an instance variable of the actor.