SagaEventHandler vs EventSourcingHandler

Hi All,
Consider below code snippets one in aggregate and other one is in the saga.

 @EventSourcingHandler
    protected void on(OrderCreatedEvent orderCreatedEvent){
        this.orderId = orderCreatedEvent.orderId;
}
  @SagaEventHandler(associationProperty = "orderId")
    public void handle(OrderCreatedEvent orderCreatedEvent){
              System.out.println("Saga invoked");
        //associate Saga
        SagaLifecycle.associateWith("paymentId", paymentId);
       System.out.println("order id" + orderCreatedEvent.orderId);
        //send the commands
        commandGateway.send(new CreateInvoiceCommand(paymentId, orderCreatedEvent.orderId));
    }

@EventHandler

So is it mandatory to have both @EventHandler and @SagaEventHandler methods always ?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 ?

When there are mulitple @EventHandler for same events what is the execution order?

Also when you say commandGateway.send(new CreateInvoiceCommand(paymentId, orderCreatedEvent.orderId)); you presume command running in the same service.

How to invoke commands running in a different remote service which is the most practical scenario?

Does Axon support distributed sagas?With AxonServer or Kafka etc

Please calrify.
Thanks,
ISuru

Hi Isuru!

About the ordering, I don’t think Axon provides an ordering guarantee in this scenario, and, in abstract, you shouldn’t depend on this either as it would make the temporal coupling a nightmare. Can you imagine having to 1) find the handlers, and 2) find out their ordering; to be able to do a code change?. If one of these two operations have a dependency, you’ll need to create a new event and then the saga has to listen to the new event. Or a new command that the saga sends when it receives the event.

I’ll leave the other question for someone else as I have strong views on integration between services.

Hi Augusto,
My question simply boils down to whether axon is supporting distributed sagas where services are running independently and can axon managed a distributed transaction with distributed sagas?Does axon support this at the moment?IF so how?

This is a pervasive use case since most of the services are distributed and might need to participate in a distributed transaction to complete.

It seems distributed command bus is the way to go.I’ll check on that

  • Axon Distributed Command bus

  • Jgroup coommand bus

  • Spring Cloud Connector command bus

What is the most reliable command bus to use?

But still does it support distributed sagas.Please clarify

Thanks
ISuru

Let me go over your questions one by one to simplify the explanation.

So is it mandatory to have both @EventHandler and @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.

The @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 @EventHandler and @SagaEventHandler, respectively.

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.

Axon’s 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.