为了了解Axon框架中Saga的实现,我一直关注AxonBank example,并使用了类似这样的代码来开始和结束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进行转帐后,所有这些代码都将执行,因为我可以看到我的日志已打印到控制台,所有交易都存储在帐户表中。
所有条目也都存在于domain_event_entry中,但是saga_entry和association_value_entry表仍然为空,无论事务是成功还是失败。
最初,我认为这可能是由于错误配置的saga存储,所以我用MongoSagaStore
配置了saga存储,但Saga的集合仍然为空。
那么我是否在这里丢失了一些东西,或者在完成传奇之后轴突只是从这些表中删除了数据?
答案 0 :(得分:3)
AxonFramework结束后,它将自动从其存储中删除Saga条目,包括所有关联。因此,您只会在那里看到活动实例的信息。
在示例应用程序中,所有总线组件都使用“ Simple ...”实现,这基本上意味着所有活动都在同一线程中执行。因此,一旦收到OK或NotOK,Saga的所有活动也将结束。 如果要用“异步”或“分布式”实现替换它们,则不再是这种情况。整个过程完成之前,您将返回OK。