【发布时间】:2020-09-24 11:27:05
【问题描述】:
我遇到了一个要求,我希望轴突等到事件总线中针对特定命令触发的所有事件完成执行。我将简要介绍一下场景:
我有一个 RestController,它会触发以下命令来创建应用程序实体:
@RestController
class myController{
@PostMapping("/create")
@ResponseBody
public String create(
org.axonframework.commandhandling.gateway.CommandGateway.sendAndWait(new CreateApplicationCommand());
System.out.println(“in myController:: after sending CreateApplicationCommand”);
}
}
该命令正在Aggregate 中处理,Aggregate 类用org.axonframework.spring.stereotype.Aggregate 注释:
@Aggregate
class MyAggregate{
@CommandHandler //org.axonframework.commandhandling.CommandHandler
private MyAggregate(CreateApplicationCommand command) {
org.axonframework.modelling.command.AggregateLifecycle.apply(new AppCreatedEvent());
System.out.println(“in MyAggregate:: after firing AppCreatedEvent”);
}
@EventSourcingHandler //org.axonframework.eventsourcing.EventSourcingHandler
private void on(AppCreatedEvent appCreatedEvent) {
// Updates the state of the aggregate
this.id = appCreatedEvent.getId();
this.name = appCreatedEvent.getName();
System.out.println(“in MyAggregate:: after updating state”);
}
}
AppCreatedEvent 在 2 个地方处理:
- 在聚合本身中,如上所示。
- 在投影类中如下:
@EventHandler //org.axonframework.eventhandling.EventHandler
void on(AppCreatedEvent appCreatedEvent){
// persists into database
System.out.println(“in Projection:: after saving into database”);
}
这里的问题是在首先捕获事件之后(即在聚合内部),调用被返回到 myController。 即这里的输出是:
in MyAggregate:: after firing AppCreatedEvent
in MyAggregate:: after updating state
in myController:: after sending CreateApplicationCommand
in Projection:: after saving into database
我想要的输出是:
in MyAggregate:: after firing AppCreatedEvent
in MyAggregate:: after updating state
in Projection:: after saving into database
in myController:: after sending CreateApplicationCommand
简单来说,我希望轴突等到针对特定命令触发的所有事件都完全执行,然后返回触发该命令的类。
在论坛上搜索后,我知道所有 sendAndWait 所做的就是等到命令的处理和事件的发布完成,然后我厌倦了 Reactor Extension 以及使用下面但得到相同的结果:@987654327 @
谁能帮帮我。 提前致谢。
【问题讨论】:
标签: axon