SagaEventHandler executed on different instance so the state stored in saga is not in effect

Saga is instantiated every time events handled using @SagaEventHandler annotation.
Saga initialized as expected using @StartSaga + @SagaEventHandler and it has set the state Set<String> friends
but when event RemovedFriendshipWithMeEvent is handled, it instantiates new Saga and the state persisted by last event is not available.
I am using playerSagaId as associationProperty and passing this variable in all relevant Event objects
I added SagaLifecycle.associateWith("playerSagaId", playerSagaId); after initiating saga, still no luck.
I am not sure whether it is caused by jackson serializer, but this was working fine before jackson serializer migration
code:

public class PlayerAnonymizingSaga {
    @Autowired
    private transient CommandGateway commandGateway;

    private Set<String> friends;

    @StartSaga
    @SagaEventHandler(associationProperty = "playerSagaId")
    public void handle(final AnonymizedPlayerEvent event) throws JsonProcessingException {
        String playerId = event.getPlayerId();
        String playerSagaId = event.getPlayerId();
        friends = event.getFriends();
        SagaLifecycle.associateWith("playerSagaId", playerSagaId);
        friends.forEach(friend -> {
            RemoveFriendshipWithMeCommand removeFriendshipWithMeCommand =
                    RemoveFriendshipWithMeCommand.builder()
                            .playerId(friend)
                            .playerSagaId(playerSagaId)
                            .friendId(playerId)
                            .build();
            commandGateway.send(removeFriendshipWithMeCommand);
        });
    }

    @SagaEventHandler(associationProperty = "playerSagaId")
    public void handle(final RemovedFriendshipWithMeEvent event) {
        String playerId = event.getPlayerId();
        friends.remove(playerId);
        if (friends.isEmpty()) {
            SagaLifecycle.end();
        }
    }
}

public class AnonymizedPlayerEvent {
    private final String playerId;
    private final String playerSagaId;
    private final Set<String> friends;

    @JsonCreator
    public AnonymizedPlayerEvent(
            @JsonProperty("playerId") final String playerId,
            @JsonProperty("playerSagaId") final String playerSagaId,
            @JsonProperty("friends") final Set<String> friends) {
        this.playerId = playerId;
        this.playerSagaId = playerSagaId;
        this.friends = friends;
    }
}

public class RemovedFriendshipWithMeEvent {
    private final String playerId;
    private final String playerSagaId;
    private final String friendId;

    @JsonCreator
    public RemovedFriendshipWithMeEvent(
            @JsonProperty("playerId") final String playerId,
            @JsonProperty("playerSagaId") final String playerSagaId,
            @JsonProperty("friendId") final String friendId) {
        this.playerId = playerId;
        this.playerSagaId = playerSagaId;
        this.friendId = friendId;
    }

I am not sure what I’m missing

You probably need a public getter for the friends field. Since it’s private, and doesn’t have a getter it’s not getting serialized with Jackson.

1 Like

Thanks for your quick response :pray: and its a timely help. It works like charm :grinning:

1 Like