调用saga - Axon中的下一个事件



我有两个事件OrderCreatedEvent和ProductReservedEvent。Saga事件处理程序如下:

@StartSaga
@SagaEventHandler(associationProperty = "orderId")
public void handle(OrderCreatedEvent event){
LOGGER.info("Saga Started - OrderCreatedEvent");
ReserveProductCommand reserveProductCommand = ReserveProductCommand.builder()
.orderId(event.getOrderId())
.productId(event.getProductId())
.orderId(event.getOrderId())
.quantity(event.getQuantity())
.userId(event.getUserId())
.build();
LOGGER.info("Created Reserve Product Command");
//        SagaLifecycle.associateWith("userId", event.getUserId()); //not working
LOGGER.info("Will call next event in saga");
commandGateway.send(reserveProductCommand);
}
@SagaEventHandler(associationProperty = "orderId")
public void handle(ProductReservedEvent productReservedEvent){
LOGGER.info("Saga Step - ProductReservedEvent");
//Process under payment
String userId = productReservedEvent.getUserId();
FetchUserPaymentDetailsQuery fetchUserDetails = new FetchUserPaymentDetailsQuery(userId);
User userPaymentDetails = null;
try {
LOGGER.info("Querying User Service with id: {}",userId);
userPaymentDetails = queryGateway.query(fetchUserDetails, ResponseTypes.instanceOf(User.class)).join();
}catch (Exception exception){
LOGGER.error(exception.getLocalizedMessage());
//start compensating transaction
return;
}
if(userPaymentDetails == null){
//start compensating transaction
return;
}
LOGGER.error("Fetched user details for user {}",userPaymentDetails.getUserId());
}

问题在于"ProductReservedEvent"的handle()方法。没有从"handle(OrderCreatedEvent event)"方法。

我认为问题是@Eventhandler在AggregateLifecycle.apply()和commandGateway.send(reserveProductCommand)之后被调用;将触发一个带@Command注释的方法。

所以在预期的流中有一个注释混淆了。

最新更新