Saga 条目 table/ Sagas 集合仍然为空
Saga entry table/ Sagas collection remains empty
我一直在关注 AxonBank example 以了解 Axon 框架中 Saga 的实现,并有一些类似这样的代码来开始和结束 saga
@Saga
public class MoneyTransferSaga {
@Inject
private transient CommandGateway commandGateway;
private String targetAccount;
private String transferId;
@StartSaga
@SagaEventHandler(associationProperty = "transferId")
public void on(MoneyTransferRequestedEvent event) {
System.out.println("Inside start saga for money transfer event");
targetAccount = event.getTargetAccount();
transferId = event.getTransferId();
SagaLifecycle.associateWith("transactionId", transferId);
System.out.println("## These are the params going into WMC : sourceAccount: " + event.getSourceAccount()
+ " transferID: " + transferId + " event.getAmount: " + event.getAmount());
commandGateway.send(new WithdrawMoneyCommand(event.getSourceAccount(), transferId, event.getAmount()),
new CommandCallback<WithdrawMoneyCommand, Object>() {
@Override
public void onSuccess(CommandMessage<? extends WithdrawMoneyCommand> commandMessage,
Object result) {
}
@Override
public void onFailure(CommandMessage<? extends WithdrawMoneyCommand> commandMessage,
Throwable cause) {
System.out.println("On failure of withdraw money command inside saga ");
System.out.println("###################### Cause of failure = " + cause);
commandGateway.send(new CancelMoneyTransferCommand(event.getTransferId()));
}
});
}
@SagaEventHandler(associationProperty = "transactionId")
public void on(MoneyWithdrawnEvent event) {
System.out.println("Inside saga event handler for monney withdrawnevent");
commandGateway.send(new DepositMoneyCommand(targetAccount, event.getTransactionId(), event.getAmount()),
LoggingCallback.INSTANCE);
}
@SagaEventHandler(associationProperty = "transactionId")
public void on(MoneyDepositedEvent event) {
System.out.println("Inside saga event handler for money deposited event");
commandGateway.send(new CompleteMoneyTransferCommand(transferId), LoggingCallback.INSTANCE);
}
@EndSaga
@SagaEventHandler(associationProperty = "transferId")
public void on(MoneyTransferCompletedEvent event) {
System.out.println("Inside Endsaga for money transfer complete event");
}
@SagaEventHandler(associationProperty = "transferId")
public void on(MoneyTransferCancelledEvent event) {
end();
}
}
通过 REST API 执行汇款后,所有这些代码都被执行,因为我可以看到我的日志被打印到控制台并且所有交易都存储在帐户 table.
domain_event_entry中也存在所有条目,但无论交易成功还是失败,saga_entry和association_value_entrytable都为空。
最初我认为这可能是因为 saga 商店配置错误,所以我用 MongoSagaStore
配置了 saga 商店,但 Saga 的集合仍然是空的。
所以我是不是遗漏了什么,或者 axon 只是在 saga 完成后从这些 table 中删除了数据?
AxonFramework 将在结束时自动从其存储中删除 Saga 条目,包括任何关联。因此,您只会在此处看到活动实例的信息。
在示例应用程序中,所有总线组件都使用 "Simple..." 实现,这基本上意味着所有活动都在同一个线程中执行。因此,一旦您收到 OK 或 NotOK,Saga 的所有活动也将结束。
如果您要用异步或分布式实现替换它们,情况就不再如此。在整个过程完成之前将返回您确定。
我一直在关注 AxonBank example 以了解 Axon 框架中 Saga 的实现,并有一些类似这样的代码来开始和结束 saga
@Saga
public class MoneyTransferSaga {
@Inject
private transient CommandGateway commandGateway;
private String targetAccount;
private String transferId;
@StartSaga
@SagaEventHandler(associationProperty = "transferId")
public void on(MoneyTransferRequestedEvent event) {
System.out.println("Inside start saga for money transfer event");
targetAccount = event.getTargetAccount();
transferId = event.getTransferId();
SagaLifecycle.associateWith("transactionId", transferId);
System.out.println("## These are the params going into WMC : sourceAccount: " + event.getSourceAccount()
+ " transferID: " + transferId + " event.getAmount: " + event.getAmount());
commandGateway.send(new WithdrawMoneyCommand(event.getSourceAccount(), transferId, event.getAmount()),
new CommandCallback<WithdrawMoneyCommand, Object>() {
@Override
public void onSuccess(CommandMessage<? extends WithdrawMoneyCommand> commandMessage,
Object result) {
}
@Override
public void onFailure(CommandMessage<? extends WithdrawMoneyCommand> commandMessage,
Throwable cause) {
System.out.println("On failure of withdraw money command inside saga ");
System.out.println("###################### Cause of failure = " + cause);
commandGateway.send(new CancelMoneyTransferCommand(event.getTransferId()));
}
});
}
@SagaEventHandler(associationProperty = "transactionId")
public void on(MoneyWithdrawnEvent event) {
System.out.println("Inside saga event handler for monney withdrawnevent");
commandGateway.send(new DepositMoneyCommand(targetAccount, event.getTransactionId(), event.getAmount()),
LoggingCallback.INSTANCE);
}
@SagaEventHandler(associationProperty = "transactionId")
public void on(MoneyDepositedEvent event) {
System.out.println("Inside saga event handler for money deposited event");
commandGateway.send(new CompleteMoneyTransferCommand(transferId), LoggingCallback.INSTANCE);
}
@EndSaga
@SagaEventHandler(associationProperty = "transferId")
public void on(MoneyTransferCompletedEvent event) {
System.out.println("Inside Endsaga for money transfer complete event");
}
@SagaEventHandler(associationProperty = "transferId")
public void on(MoneyTransferCancelledEvent event) {
end();
}
}
通过 REST API 执行汇款后,所有这些代码都被执行,因为我可以看到我的日志被打印到控制台并且所有交易都存储在帐户 table.
domain_event_entry中也存在所有条目,但无论交易成功还是失败,saga_entry和association_value_entrytable都为空。
最初我认为这可能是因为 saga 商店配置错误,所以我用 MongoSagaStore
配置了 saga 商店,但 Saga 的集合仍然是空的。
所以我是不是遗漏了什么,或者 axon 只是在 saga 完成后从这些 table 中删除了数据?
AxonFramework 将在结束时自动从其存储中删除 Saga 条目,包括任何关联。因此,您只会在此处看到活动实例的信息。
在示例应用程序中,所有总线组件都使用 "Simple..." 实现,这基本上意味着所有活动都在同一个线程中执行。因此,一旦您收到 OK 或 NotOK,Saga 的所有活动也将结束。 如果您要用异步或分布式实现替换它们,情况就不再如此。在整个过程完成之前将返回您确定。