[英]Axon FrameWork: How to rollback in the domain_event_entry table
You can see my sample classes below. 您可以在下面看到我的示例类。
Basically, I want to use Axon's domain_event_entry
table to store the events, and my own entity table to store the entities. 基本上,我想使用Axon的domain_event_entry
表来存储事件,并使用我自己的实体表来存储实体。 I know that If I fire a CreateTemplateCommand
which is handled in the Aggregate an Event will be published, and that after that it will go to the @EventSourcingHandler
where Axon will persist the event in his domain_event_entry
table. 我知道如果我触发在Aggregate中处理的CreateTemplateCommand
将发布一个事件,那之后它将转到@EventSourcingHandler
,Axon将在其domain_event_entry
表中保存该事件。
After this part, it will go to my external @EventHandler
where I want to persist my entity from the event. 在这部分之后,它将转到我的外部@EventHandler
,我希望在该事件中保留我的实体。 As you can see, I throw a RunTimeException
, because I want to simulate a rollback. 如您所见,我抛出RunTimeException
,因为我想模拟回滚。 It will rollback in my own table, but in the Axon's domain_event_entry
table there will be the event. 它会在我自己的表中回滚,但是在Axon的domain_event_entry
表中会有事件。 So I will get my entity table empty, and my Axon's table with the TemplateCreatedEvent
in it, but I want to rollback the domain_event_entry
too. 所以我将我的实体表空了,我的Axon的表中包含了TemplateCreatedEvent
,但我也要回滚domain_event_entry
。
How can I rollback the domain_event_entry
table or should I use compensating events for that? 如何回滚domain_event_entry
表或者我应该使用补偿事件?
@Aggregate
@Getter
@Setter
@NoArgsConstructor
public class TemplateAggregate {
private static final transient Logger logger = LoggerFactory.getLogger(TemplateAggregate.class);
@AggregateIdentifier
private String templateId;
private LocalDate createdAt;
private String createdBy;
private String description;
private LocalDate modifiedAt;
private String modifiedBy;
private String name;
private LocalDate validFrom;
private LocalDate validTo;
private File file;
private String fileName;
private long fileSize;
private LocalDate fileDate;
private String fileUploader;
@CommandHandler
public TemplateAggregate(CreateTemplateCommand cmd) {
AggregateLifecycle.apply(new TemplateCreatedEvent(cmd));
}
@CommandHandler
public void handle(ModifyTemplateCommand cmd) {
AggregateLifecycle.apply(new TemplateModifiedEvent(cmd));
}
@EventSourcingHandler
public void on(TemplateCreatedEvent event) {
this.templateId = event.getTemplateId();
this.createdAt = event.getCreatedAt();
this.createdBy = event.getCreatedBy();
this.description = event.getDescription();
this.name = event.getName();
this.validFrom = event.getValidFrom();
this.validTo = event.getValidTo();
this.file = event.getFile();
this.fileName = event.getFileName();
this.fileSize = event.getFileSize();
this.fileDate = event.getFileDate();
this.fileUploader = event.getFileUploader();
logger.info("TemplateAggregate - TemplateCreatedEvent");
}
@EventSourcingHandler
public void on(TemplateModifiedEvent event) {
this.templateId = event.getTemplateId();
this.createdAt = event.getCreatedAt();
this.createdBy = event.getCreatedBy();
this.description = event.getDescription();
this.name = event.getName();
this.validFrom = event.getValidFrom();
this.validTo = event.getValidTo();
this.file = event.getFile();
this.fileName = event.getFileName();
this.fileSize = event.getFileSize();
this.fileDate = event.getFileDate();
this.fileUploader = event.getFileUploader();
this.modifiedAt = event.getModifiedAt();
this.modifiedBy = event.getModifiedBy();
logger.info("TemplateAggregate - TemplateModifiedEvent");
}
}
My External @EventHandler class: 我的外部@EventHandler类:
@Service
public class TemplateCreatedEventHandler {
private static final transient Logger logger =
LoggerFactory.getLogger(TemplateCreatedEventHandler.class);
@Autowired
private TemplateRepository templateRepository;
@Transactional
@EventHandler
public void on(TemplateCreatedEvent event) {
templateRepository.save(new TemplateQueryEntity(event));
logger.info("EventHandler - TemplateCreatedEvent");
throw new RuntimeException();
}
}
This is correct and expected behavior of a tracking event processor. 这是跟踪事件处理器的正确和预期行为。 The event store is the source of truth, and once your TemplateCreatedEvent has been persisted in the event store, this means 'a template created event has occurred'. 事件存储是事实的来源,一旦您的TemplateCreatedEvent持久存储在事件存储中,这意味着“模板创建的事件已经发生”。
Your event processor down the line is unable to handle the event, and throws and exception. 你的事件处理器无法处理事件,抛出和异常。 This means the transaction in your TemplateCreatedEventHandler is rolled back. 这意味着回滚了TemplateCreatedEventHandler中的事务。 This does not mean history has suddenly been changed; 这并不意味着历史突然发生变化; your event handler down the line does not get to decide that a TemplateCreatedEvent has not occurred. 你的事件处理程序不会决定是否没有发生TemplateCreatedEvent。
When you later roll out a fix for the TemplateCreatedEventHandler so it is able to handle that event (remove the RuntimeException), the handler will handle the event and persist the entity. 当您稍后为TemplateCreatedEventHandler推出修复程序以便它能够处理该事件(删除RuntimeException)时,处理程序将处理该事件并持久保存该实体。
If this behavior is not what you want, you can choose to use a subscribing event processor. 如果此行为不是您想要的,您可以选择使用订阅事件处理器。
The SubscribingEventProcessor will have the exception bubble up to the publishing component of the Event, allowing it to deal with it, accordingly. SubscribingEventProcessor将异常冒泡到事件的发布组件,允许它相应地处理它。
https://docs.axoniq.io/reference-guide/configuring-infrastructure-components/event-processing/event-processors#exceptions-during-processing https://docs.axoniq.io/reference-guide/configuring-infrastructure-components/event-processing/event-processors#exceptions-during-processing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.