简体   繁体   English

如何在轴心框架的Java中不使用Spring的情况下配置处理命令和调度事件的聚合类?

[英]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. 我想在不使用轴突框架的java中使用spring的情况下设置处理命令并调度事件的聚合类。 I have performed it with spring boot using annotations like @Aggregate, @CommandHandler but unable to do without spring. 我已经使用诸如@ Aggregate,@ CommandHandler之类的批注在spring boot中执行了它,但是没有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. 我知道在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");
    }
}

You don't have to use Spring at all. 您根本不需要使用Spring。

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提供了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 在此处查看完整的example / demo项目: https//github.com/idugalic/axon-vanilla-java-demo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM