Akash Jadhav
Akash Jadhav

Reputation: 41

How to configure aggregate class which handles command and dispatch event without using spring in java with axon framework?

I want to setup aggregate class which handles command and dispatches event without using spring in java using axon framework. I have performed it with spring boot using annotations like @Aggregate, @CommandHandler but unable to do without spring.

I have used default configurer object and command bus object. I am able to dispatch command and handle it in custom handler but I want to handle it in aggregate and dispatch event and handle that event also in aggregate. I know annotations are enabled in spring boot.

@Aggregate
public class PlayerAggregate{

    @AggregateIdentifier
    private String playerId;

    public PlayerAggregate() {
    }

    @CommandHandler
    public PlayerAggregate(CreatePlayerCommand createPlayerCommand){
        AggregateLifecycle.apply(new PlayerCreatedEvent(createPlayerCommand.playerId ));
    }

    @EventSourcingHandler
    protected void on(PlayerCreatedEvent playerCreatedEvent){
        this.playerId = playerCreatedEvent.playerId;
        System.out.println("event completed");
    }
}

Upvotes: 2

Views: 1018

Answers (1)

Ivan Dugalic
Ivan Dugalic

Reputation: 661

You don't have to use Spring at all.

To make the configuration of the infrastructure components and to define their relationship with each of the functional components, Axon provides a Java Configuration API.

 /* Axon configuration */
    Configuration config = DefaultConfigurer.defaultConfiguration()
            .configureAggregate(GiftCard.class)
            // .configureEmbeddedEventStore(c -> new InMemoryEventStorageEngine())
            .eventProcessing(ep -> ep.registerEventHandler(c -> new GiftCardEventHandler(c.queryUpdateEmitter(), querySideDBMap)))
            .registerQueryHandler(c -> new GiftCardQueryHandler(querySideDBMap))
            .start();

Check out the full example/demo project here: https://github.com/idugalic/axon-vanilla-java-demo

Upvotes: 3

Related Questions