zoukankan      html  css  js  c++  java
  • CQRS With Axon

    CQRS With Axon

     
     

    CQRS implementation with Axon[edit]

    RTENOTITLE

    According to the diagram above, creating commands, passing them to command bus and then creating events and placing them on event bus is not CQRS yet.

    We have to remember about changing the state of write repository and reading current state from the read database. This is the crucial point of the CQRS pattern.

    Configuring this flow should be easy as well. While passing the command to the command gateway, Spring searches methods annotated with @CommandHandler with command type as argument.

    RTENOTITLE

     
    @Value
    class SubmitApplicationCommand {
    private String appId;
    private String category;
    }
     
    @AllArgsConstructor
    public class ApplicationService {
    private final CommandGateway commandGateway;
     
    public CompletableFuture<Void> createForm(String appId) {
    return CompletableFuture.supplyAsync(() -new SubmitExpertsFormCommand(appId, "Android"))
    .thenCompose(commandGateway::send);
    }
    }

    Command handler is responsible, among other things, for sending the created event to the event bus.

    It places an event object to statically imported apply() method from AggregateLifecycle. The event is later dispatched to find expected handlers and thanks to configured event store, all events are saved in DB automatically.

    RTENOTITLE

     
    @Value
    class ApplicationSubmittedEvent {
    private String appId;
    private String category;
    }
     
    @Aggregate
    @NoArgsConstructor
    public class ApplicationAggregate {
    @AggregateIdentifier
    private String id;
     
    @CommandHandler
    public ApplicationAggregate(SubmitApplicationCommand command) {
    //some validation
    this.id = command.getAppId;
    apply(new ApplicationSubmittedEvent(command.getAppId(), command.getCategory()));
    }
    }

    To change the state of the write DB, we need to provide a method annotated with @EventHandler.

    The application can contain multiple event handlers. Each of them should perform one specific task like sending emails, logging or saving in database.

    RTENOTITLE

     
    @RequiredArgsConstructor
    @Order(1)
    public class ProjectingEventHandler {
    private final IApplicationSubmittedProjection projection;
     
    @EventHandler
    public CompletableFuture<Void> onApplicationSubmitted(ExpertsFormSubmittedEvent event) {
    return projection.submitApplication(event.getApplicationId(), event.getCategory());
    }
    }

    If we want to determine the processing order of all event handlers,

    we can annotate a class with @Order and set a sequence number. submitApplication() method is responsible for making all the necessary changes and saving new data in write DB.

    These are all vital points to make our app event sourced with CQRS pattern principles. Of course, these principles can be applied only in some parts of our application depending on business needs.

    Event sourcing is not suitable for every application or module we are building. It is also worth to be cautious while implementing this pattern because a more complex application can be hard to maintain.

    Conclusion[edit]

    Implementation of CQRS and Event Sourcing is straightforward with Axon framework. More details about advanced configuration can be found on Axon’s website https://docs.axonframework.org/.

     

     

  • 相关阅读:
    Ubuntu 15.04 下apt-get安装JDK
    Ubuntu下apt-get安装Java,Tomcat
    虚拟化技术比较 PV HVM
    Java8 Lambda表达式教程
    SpringMVC实现上传和下载
    web.xml中的url-pattern映射规则
    java文件读写操作大全
    Java创建文件
    JAVA文件中获取路径及WEB应用程序获取路径方法
    session.flush()与session.clear()的区别及使用环境
  • 原文地址:https://www.cnblogs.com/lshan/p/11425706.html
Copyright © 2011-2022 走看看