首页 > 解决方案 > Axon 4 - JVM 重启后 Saga 再次运行

问题描述

我将 Axon4 与 Spring Boot 一起使用。创造了简单的传奇。它在运行中的 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()));
    }
}

标签: axon

解决方案


对Prashant他的问题的决议在这里进一步讨论

简而言之,数据库create在每次启动时设置为。因此,跟踪 Saga 处理事件的距离的令牌(用于跟踪事件处理器)在应用程序的每次启动时被重置。

因此切换线路spring.jpa.properties.hibernate.hbm2ddl.auto=createspring.jpa.properties.hibernate.hbm2ddl.auto=update解决问题。


推荐阅读