How create a Aggregate with eventBus?

Hello,

I have a microservice A. When I create my aggregate A, I would like to create another aggregate B in microservice B. It’s possible with the eventBus ?

Microservice A

public class AggregateA {

    @AggregateIdentifier
    private UUID id;
    private UUID bId
    private String titre;
    private String description;
    private Date debut;
    private Date fin;

    @CommandHandler
    public AggregateA(CreateACommand command){
        apply(new ACreatedEvent(
                command.getId(),
                command.getTitre(),
        ));
    }

    @EventSourcingHandler
    public void on(ACreatedEvent event){
        this.id = event.getId();
        this.titre = event.getTitre();
      
    }

    @EventSourcingHandler
    public void on(BCreatedEvent event){
        this.idB= event.getB();
    }

Microservice B

public class BAggregate {

    @AggregateIdentifier
    private UUID id;
    private UUID aId;
    private String titre;
    private Date debut;
    private Date fin;
   
    @EventSourcingHandler
    public void on(ACreatedEvent event) {
        this.operationId = event.getId();
        this.id = UUID.randomUUID();
        this.titre = event.getTitre();
        apply(new BCreatedEvent(operationId, id));
    }


}

Thanks

Hi @Vincent62,

An Event can have multiple handlers each working performing their domain-related updates logic. An EventBus should be able to able to route events.

Hi, thank you for the answer

The example above, don’t work
The ACreatedEvent in the microservice B is never called
Do you have an idea knowing that each microservice must have a reference with the other ?

Hey @Vincent62,

Does this one not work for you?

Maybe what you can do is perform an @EventHandler (not @EventSourcingHandler) and then fire a command based on the desired event. If synchronicity is a concern I would not recommend this approach

Handling the event source directly on BAggregate will not work since they are distinct aggregates.

Hi,

Does this one not work for you?

I think it works when it’s in the same microservice

If i use @EventHandler to fire a command, a new aggregate is created when i restart the microservice. So I have several aggregate B

B service

    @EventHandler
    public void on(ACreatedEvent event) {
        commandGateway.send(new CreateBCommand(
                UUID.randomUUID(),
                event.getId(),
                event.getTitre()
        ));
    }

BAggregate

    @CommandHandler
    public BAggregate(CreateBCommand command) {
        apply(new BCreatedEvent(
                command.getId(),
                command.getOperationId(),
                command.getTitre()
        ));

    }

    @EventSourcingHandler
    public void on( BCreatedEvent event) {
        this.id = event.getId();
        this.AId = event.getAId();
        this.titre = event.getTitre();
        log.info("A is created {}",id);

    }

The synchronicity is not a concern. I would just create an other aggragate and link the two with id

Is this question somehow related to that one?

No. The other question, is to link two Aggregate in the same Microservice

Now, i would like create two aggregates ( in different microservices) and exchange the aggregate id
It’s possible to send two http request, but I thought it’s more attractive using the eventBus when the first aggregate is created

The Aggregate provided by Axon Framework is for a Command Model. The aggregate is essentially a component, to begin with. You can read more about it here.

In an event sourcing model, the @EventSourcingHandler in the aggregate handles the events originating from it. In your use case, add the @EventHandler annotation in a @Component but not an @Aggregate.

Hope this helps!

Hi,
For my use case, I just used the SAGA pattern and it’s work perfectly
Thank you very much for your help

2 Likes