Is it possible to add metadata that's automatically added to every subsequent command or event?

Hello,

Is it possible to add metadata to a command and to automatically let it add to every subsequent command or event?
For instance, we want to add information about the user who’s doing the request and retrieve that metadata when there’s a need to do that in events or commands following the original command.
I know we could retrieve the metadata and place it again, but is there a magic way to do it automatically?

Sincerely,
Marc

Hi Marc,

yes, that’s possible. No magic required :wink:

If you’re on Spring, you can simply define a bean of type CorrelationDataProvider. This interface describes a single method, which returns a Map meta-data entries for a given Message. The return value represents the Meta Data that you would like to assign to all messages that are generated as the result of handling the message passed to the method. Hope that makes sense.

Note that Axon-Spring autoconfiguration will give you the “MessageOriginProvider” by default. If you’d like to keep that and provide additional CorrelationDataProviders, you also need to define a Bean that returns a MessageOriginProvider.

If you don’t use Spring, you can use the Configurer.configureCorrelationDataProviders(…) method to register the CorrelationDataProvider instances that you’d like to use.

Hope this helps.
Cheers,