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

 

 

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