Let me go over your questions one by one to simplify the explanation.
So is it mandatory to have both
@SagaEventHandler methods always?
No, you wouldn’t use both annotations in a single class. The
@EventHandler annotation is used for components that react to events to update a model or perform a simple activity. Furthermore, the annotations are placed in separate event handling components, receiving their events from an Event Processor. This makes them part of the Query Side of your application.
@SagaEventHandler is only used for Saga classes. It is required for sagas, as a saga is associated with specific model instances. This requirement is in place to ensure the saga represents a single complex business transaction.
So what is happening here is that
SagaEventHandler will monitor the
EventHandler execution and onsuccess or failure saga state will be updated? Is it? Does
SagaEventHander gets executed after
EventHandler is finished succesfully or failed?
These two annotations are completely separated. So, there is no interplay between functions annotated with
When there are mulitple
@EventHandler for same events what is the execution order?
Within a single Event Handling Component, Axon will pick the most specific event handler for an event it receives. So, if a single class has two (or more) event handlers for the same event, the last registered will prevail. It would thus follow the function ordering within the class in that case.
If you have the requirement of several event handlers for the same event, it is recommended to separate those into distinct classes. This further helps with the entire separation of your application actually, generating a clearer separation of concerns.
How to invoke commands running in a different remote service which is the most practical scenario?
If you dispatch a command, the
CommandBus will decide where to route the command to. Depending on the
CommandBus implementation, this can be local or distributed. If the bus is a local implementation, you are guaranteed the command is handled locally. If the
CommandBus is a distributed application, the command will either be handled locally or on another node.
CommandBus will know whether the command handlers reside on the local instance or somewhere else in a distributed setup. If both instances contain the command handler, Axon uses the consistent hashing algorithm to decide where to send the command to.
Does Axon support distributed sagas?
Yes, it does. Event Handling can only be performed if a
StreamingEventProcessor claims on a
TrackingToken. Through this, you are ensured other instances cannot handle events for that processor. This ensures that only a single application will claim the token required to provide events to a Saga in a distributed application. It is important to state that this does require sharing the
TokenStore's data source among your application.