首页 > 其他 > 详细

CQRS With Axon

时间:2019-08-28 20:16:02      阅读:133      评论:0      收藏:0      [点我收藏+]

CQRS With Axon

 
 

CQRS implementation with Axon[edit]

技术分享图片

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.

技术分享图片

 
@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.

技术分享图片

 
@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.

技术分享图片

 
@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/.

 

 

CQRS With Axon

原文:https://www.cnblogs.com/lshan/p/11425706.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!