1

我有一个正在使用 Axon 开发的项目,但我的 Saga 没有正确执行。

My Saga 包含与此类似的行:

@StartSaga
@SagaEventHandler(associationProperty = "uuid")
public void handle(FirstEvent event) { 
    System.out.println("Processing FirstEvent for uuid=" + event.getUuid());
    associateWith("uuid", event.getUuid().toString());
    initialiseWorkflow(event.getUuid(), Status.CREATED) ;
} 

@SagaEventHandler(associationProperty = "uuid")
public void handle(SecondEvent event) { 
    System.out.println("Processing SecondEvent for uuid=" + event.getUuid());
    this.processStep(STEP_2,event.getUuid());
} 

第一个事件正在触发一个开始 saga,并且还触发了 initialiseWorkflow 任务(它正确地创建了一组额外的步骤。)但是,当 SecondEvent 到达时(与 FirstEvent 具有相同的 UUID 关联属性值),saga 不会拿起那第二个事件。

我已经尝试专门包括以下行来增强关联,但这也不起作用:

associateWith("uuid", event.getUuid().toString());

具有讽刺意味的是,我有一个测试用例,使用正常工作的轴突测试框架,这类似于:

@Test
public void testSecondEvent() {  
     fixture.givenAggregate(uuid).published(new FirstEvent(uuid))
     .whenAggregate(uuid).publishes(new SecondEvent(uuid))
     .expectDispatchedCommandsMatching(exactSequenceOf(
             new CompleteTaskCommandMatcher("SecondEvent"))); 
}

问题出现在我的端到端测试中,我将命令直接放入 CommandGateway,并直接在存储库中检查结果。

我已经仔细检查了 AnnotatedSagaManager 是否正在使用,并且确实如此。

有没有人对可能出现的问题有任何想法,或者我误解了 Sagas 应该如何工作?

编辑:还有一些更新:

1) 我注意到在直接关联 UUID 时需要使用 toString(),所以我尝试将值变成事件的字符串 - 没有进展。

2)我尝试打印出关联的值,发现不需要直接关联行(在start saga过程中关联了uuid)

3)我尝试将@StartSaga 放在 secondEvent 上,这达到了“Processing SecondEvent ...”的代码,但在一个新的传奇中。

更多了解,但还没有解决方案!

4

1 回答 1

2

我找到了问题的原因...

我的配置基于 AxonTrader 示例应用程序的 Mongo 配置文件。但是,AxonTrader persistence-infrastructure-context.xml(如下所示)包含一个缺陷:

<beans profile="mongodb">
    <bean id="mongoSpringTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo"/>
        <constructor-arg name="databaseName" value="axontrader"/>
    </bean>

    <bean id="mongoTemplate" class="org.axonframework.eventstore.mongo.DefaultMongoTemplate">
        <constructor-arg index="0" ref="mongo"/>
        <constructor-arg index="1" value="axontrader"/>
        <constructor-arg index="2" value="domainevents"/>
        <constructor-arg index="3" value="snapshotevents"/>
        <constructor-arg index="4">
            <null/>
        </constructor-arg>
        <constructor-arg index="5">
            <null/>
        </constructor-arg>
    </bean>

    <bean id="mongoSagaTemplate" class="org.axonframework.saga.repository.mongo.DefaultMongoTemplate">
        <constructor-arg index="0" ref="mongo"/>
        <constructor-arg index="1" value="axontrader"/>
        <constructor-arg index="2" value="snapshotevents"/>
        <constructor-arg index="3">
            <null/>
        </constructor-arg>
        <constructor-arg index="4">
            <null/>
        </constructor-arg>
    </bean>

    <mongo:mongo id="mongo" host="127.0.0.1" port="27017"/>
</beans>

从上面的代码片段可以看出,eventStore 和 sagaRepository 都使用“snapshotevents”作为参数。但是,快照事件仅与 eventStore 相关,并且在与 sagaRepository 结合时似乎会导致冲突。

当我将此值更改为 sagaRepository 的“sagas”时,一切都会正确到位!

于 2014-10-02T10:01:23.133 回答