Axon 4 - JVM 重启后再次出现 Saga 运行
Axon 4 - Saga run again after JVM restart
我正在使用带 Spring 引导的 Axon4。创建简单的传奇。它在运行中的 JVM 期间运行良好。但是,一旦重新启动,它会再次重新运行 Saga。
试图将 JpaSagaStore 用于持久化,但没有成功。以下是代码片段。请帮忙
@Configuration
public class AxonConfig {
@PersistenceContext
private EntityManager entityManager;
@Bean
public SagaStore sagaStore() {
return JpaSagaStore.builder().entityManagerProvider(new SimpleEntityManagerProvider(entityManager)).build();
}
}
@Saga(sagaStore = "sagaStore")
@Slf4j
public class OrderSaga {
@Autowired
private transient CommandGateway commandGateway;
private UUID orderId;
private boolean passed;
@StartSaga
@SagaEventHandler(associationProperty = "orderId")
public void on(OrderPlacedEvt evt) {
log.debug("handling {}", evt);
if (!passed) {
orderId = evt.getOrderId();
UUID shipmentId = UUID.randomUUID();
associateWith("shipmentId", shipmentId.toString());
commandGateway.send(new PrepareShipmentCmd(shipmentId, evt.getDestination()));
}
}
@SagaEventHandler(associationProperty = "shipmentId")
public void on(ShipmentPreparedEvt evt) {
log.debug("handling {}", evt);
log.debug("orderId: {}", orderId);
commandGateway.send(new RegisterShipmentForOrderPreparedCmd(orderId, evt.getShipmentId()));
}
@SagaEventHandler(associationProperty = "shipmentId")
@EndSaga
public void on(ShipmentArrivedEvt evt) {
log.debug("handling {}", evt);
log.debug("orderId: {}", orderId);
commandGateway.send(new RegisterShipmentForOrderArrivedCmd(orderId, evt.getShipmentId()));
}
}
对 Prashant 问题的解决方案得到进一步讨论 here
简而言之,数据库在每次启动时都设置为 create
。
因此,跟踪 Saga 处理事件的距离的令牌(用于跟踪事件处理器)在每次启动应用程序时都会重置。
因此将线路 spring.jpa.properties.hibernate.hbm2ddl.auto=create
切换为 spring.jpa.properties.hibernate.hbm2ddl.auto=update
解决了问题。
我正在使用带 Spring 引导的 Axon4。创建简单的传奇。它在运行中的 JVM 期间运行良好。但是,一旦重新启动,它会再次重新运行 Saga。
试图将 JpaSagaStore 用于持久化,但没有成功。以下是代码片段。请帮忙
@Configuration
public class AxonConfig {
@PersistenceContext
private EntityManager entityManager;
@Bean
public SagaStore sagaStore() {
return JpaSagaStore.builder().entityManagerProvider(new SimpleEntityManagerProvider(entityManager)).build();
}
}
@Saga(sagaStore = "sagaStore")
@Slf4j
public class OrderSaga {
@Autowired
private transient CommandGateway commandGateway;
private UUID orderId;
private boolean passed;
@StartSaga
@SagaEventHandler(associationProperty = "orderId")
public void on(OrderPlacedEvt evt) {
log.debug("handling {}", evt);
if (!passed) {
orderId = evt.getOrderId();
UUID shipmentId = UUID.randomUUID();
associateWith("shipmentId", shipmentId.toString());
commandGateway.send(new PrepareShipmentCmd(shipmentId, evt.getDestination()));
}
}
@SagaEventHandler(associationProperty = "shipmentId")
public void on(ShipmentPreparedEvt evt) {
log.debug("handling {}", evt);
log.debug("orderId: {}", orderId);
commandGateway.send(new RegisterShipmentForOrderPreparedCmd(orderId, evt.getShipmentId()));
}
@SagaEventHandler(associationProperty = "shipmentId")
@EndSaga
public void on(ShipmentArrivedEvt evt) {
log.debug("handling {}", evt);
log.debug("orderId: {}", orderId);
commandGateway.send(new RegisterShipmentForOrderArrivedCmd(orderId, evt.getShipmentId()));
}
}
对 Prashant 问题的解决方案得到进一步讨论 here
简而言之,数据库在每次启动时都设置为 create
。
因此,跟踪 Saga 处理事件的距离的令牌(用于跟踪事件处理器)在每次启动应用程序时都会重置。
因此将线路 spring.jpa.properties.hibernate.hbm2ddl.auto=create
切换为 spring.jpa.properties.hibernate.hbm2ddl.auto=update
解决了问题。