Add MetaData to event with andThenApply

Hello,

How can i add the MetaData in the event VictimAdded when i use andThenApply ?

    @CommandHandler
    private void handle(RegisterUser cmd, MetaData metaData) {
        AggregateLifecycle.apply(new UserRegisteredEvent(userId, emailAddress), metaData)
                .andThenApply(() -> new VictimAdded(userId, victimId));
    }

Hi,

in this case, it probably just easiest to do two consecutive calls to apply(). Axon Framework guarantees that the event is applied to the current aggregate before it executes the next line of code.

If you want to keep the .thenApply() approach, the parameter is a messageOrPayload, which means you can wrap your payload and metadata in a Message. Axon will automatically make an EventMessage out of it:

.andThenApply(() -> new GenericMessage(new VictimAdded(userId, victimId), metaData));

fwiw - It would have been nice to have the thenApply provide overloaded methods for passing along metadata as well. Feel free to submit a ticket (or better a PR) on GitHub

1 Like