使用 Axon 4.3.5 在我的 Saga 中从跟踪模式切换到订阅模式时,我看到了意外的行为
看来,在订阅模式下,当两个线程同时到达两个 @StarSaga 方法时,会为相同的关联键值创建两个 saga。我错过了什么吗?
我有这个来重现它:
@Saga
@ProcessingGroup("Saga")
public class RaceSaga {
@Inject
protected transient CommandGateway commandGateway;
@StartSaga
@SagaEventHandler(associationProperty = "executionId")
public void on(Exec exec) {
commandGateway.sendAndWait(new CreateExecCommand(exec.getExecutionId(), exec.getDescription()));
}
@StartSaga
@SagaEventHandler(associationProperty = "executionId")
public void on(Risk risk) {
commandGateway.sendAndWait(new CreateRiskCommand(risk.getExecutionId(), risk.getResult()));
}
}
@IntegrationTest
class RaceConditionTest extends BaseIntegrationTest {
@Autowired
private EventGateway eventGateway;
@Autowired
private SagaStore sagaStore;
@Test
void sagaRace() {
var execId = UUID.randomUUID();
CompletableFuture.runAsync(() -> eventGateway.publish(new Exec(execId.toString(), "desc")));
CompletableFuture.runAsync(() -> eventGateway.publish(new Risk(execId.toString(), "OK")));
var association = new AssociationValue("executionId", execId.toString());
await().during(5, SECONDS)
.untilAsserted(() -> assertThat(sagaStore.findSagas(RaceSaga.class, association))
.hasSize(1));
}
}
使用跟踪模式时测试通过,但订阅失败。(yml 配置)