Automatic propagation of MetaData from Command to Event - is it possible?

I want to use MetaData to store the origin of an event. Use Case, we have 2+ apps publishing the same commands/events and I need to filter that each app only handles its own (triggered by it) events.

I managed to register a commondDispatch Interceptor that adds the spring.application.name to MetaData. But I only can access the value if I manually do

@CommandHandler
fun handle(cmd: MyCommand, meta: MetaData) {
  AggregateLifecycle.apply(myEvent, meta)
}

if I do not manually transport the metaData from handle-fun to apply-call, it is lost.

I was under the impression, that meta data is propagated automatically …

(note: although my use case will be spring based, I currently am working with an AggregateTestFixture and the registerCommandDispatcher feature).

Any hints how I can achieve what I need and how I can test this using fixtures?

Thanks

Hi Jan,

have you checked the Correlation Data Provider option from here? It seems that utilizing message correlation could be beneficial in your situation.

Hope this helps.

Kind regards,
Stefan

1 Like

Thanks a lot, Stefan. Yes, this is exactly what I was looking for.

I also enhanced our axon-jgiven test lib so CorrelationDataProviders can be registered in fixture based tests:

    // adds "foo" metaData
    private fun addFooMetaToEveryCommand(value: String) = MessageDispatchInterceptor<CommandMessage<*>> {
      BiFunction { _, cmd -> cmd.andMetaData(mapOf("foo" to value)) }
    }

    @ProvidedScenarioState
    val fixture = AxonJGiven.aggregateTestFixtureBuilder<DummyAggregate>()
      .registerCommandDispatchInterceptor(addFooMetaToEveryCommand("bar"))
      // correlates "foo"
      .registerCorrelationDataProvider(SimpleCorrelationDataProvider("foo"))
      .build()

    @Test
    fun `metaData foo=bar is propagated to event`() {
      GIVEN.noPriorActivity()

      WHEN.command(CreateDummyAggregate("1"))

      // asserts that "foo"="bar" is part of the DomainEventMessage
      THEN
        .expectEventWithMetaData(DummyAggregateCreated("1"), "foo" to "bar")
    }
2 Likes