0
votes

I have a usecase where I would like to publish a non-state-chaninging event as a trigger.

In the vast majority of cases, the Aggregates will publish events by applying them. However, occasionally, it is necessary to publish an event (possibly from within another component), directly to the Event Bus. To publish an event, simply wrap the payload describing the event in an EventMessage. The GenericEventMessage.asEventMessage(Object) method allows you to wrap any object into an EventMessage ...

The event is published from inside a Saga.

When I use asEventMessageand look at the events table I'm a little confused. The event has an aggregate identifier that does not exist in the rest of the system and the type entry is null (when reading the docs, for a moment it sounds like the expected behavior of asEventMessage is equal to applying events from within aggregates).

Since I consider the event I'm talking about conceptionally part of the aggregate it should be referring to it, right?

So I craft a GenericDomainMessage myself and set its aggregate identifier, sequence number and type manually:

@SagaEventHandler
public void on (AnotherEvent event, @SequenceNumber long sequenceNr) {

    // ...

    GenericDomainEventMessage myEvent = new GenericDomainEventMessage(
            MyAggregate.class.getSimpleName(),
            identifier.toString(),
            sequenceNr + 1, 
            payload);
    eventStore.publish(myEvent);

}

This event does not introduce (data) state change to its underlying aggregate. I treat it as a flag/trigger that has a strong meaning in the domain.

I could also publish the event from within the aggregate, in a command handler but some of the operations that need to be performed are outside of the aggregate's scope. That's why a Saga seems to be more suitable.

So my questions are:

Is publishing a GenericDomainEventMessage equal to the behavior of AggrgateLifeCycle#apply?

Should there be a no-op handler in the aggregate or will axon handle this correctly?

1

1 Answers

1
votes

In Axon 3, publishing the event to the EventBus is the same as apply()ing them from within the Aggregate, with one difference: apply() will invoke any available handlers as well. If no handler is available, there is no difference.

EventBus.publish is meant to publish Events that should not be directly tied to the Aggregate. In the Event Store table, it does get an Aggregate identifier (equal to the message identifier), but that's a technical detail.

In your case, the recommended solution would be to apply() the Event. The fact that the event doesn't trigger a state change doesn't matter at that point. You're not obliged to define an @EventSourcingHandler for it.