Store sagas in JpaSagaRepository or MongoSagaRepository

384 views
Skip to first unread message

Viggo Navarsete

unread,
Apr 19, 2014, 7:16:04 AM4/19/14
to axonfr...@googlegroups.com
Hi,

I can see that there are three options for persisting sagas, either  InMemorySagaRepository, JpaSagaRepository or MongoSagaRepository. Since I've chosen to persist the events in a MongoDB, I also assume it would be best to store the sagas in the same DB (less to configure, less to maintain etc..). I've searched various examples on how to setup the Spring configuration for a MongoSagaRepository, but I'm unable to complete it:(

I paste what I've got so far, and hope someone can help me out with the rest:

    <axon:saga-manager id="mySagaManager" saga-repository="mongoSagaRepository" event-bus="eventBus">
        <axon:async processor-count="10" executor="myThreadPool" transaction-manager="txManager"/>
        <axon:types>    
            com.foo.MySaga
        </axon:types>
    </axon:saga-manager>

    <bean id="mongoSagaRepository" class="org.axonframework.saga.repository.mongo.MongoSagaRepository">
        <constructor-arg ref="sagaMongoTemplate"/>
    </bean>

    <bean id="sagaMongoTemplate" class="org.axonframework.saga.repository.mongo.DefaultMongoTemplate">
        <constructor-arg ref="mongoDb"/>
    </bean>

    <bean id="mongoDb" class="com.mongodb.Mongo" factory-bean="mongoFactoryBean" factory-method="createMongo"
          lazy-init="true"/>

    <bean id="mongoFactoryBean" class="org.axonframework.eventstore.mongo.MongoFactory" lazy-init="true">
        <property name="mongoOptions">
            <bean class="com.mongodb.MongoOptions" factory-bean="mongoOptionsFactory"
                  factory-method="createMongoOptions"/>
        </property>
    </bean>    

    <bean id="mongoOptionsFactory" class="org.axonframework.eventstore.mongo.MongoOptionsFactory">
        <property name="connectionsPerHost" value="100"/>
    </bean> 

   // I assume this bean is the one causing problems. According to the JavaDoc, I need to set a PlatformTransactionManager using setter injection, but I don't know to what I should set it! (http://www.axonframework.org/apidocs/2.1/org/axonframework/unitofwork/SpringTransactionManager.html)
  // Currently it results in a NullPointerException on line 67 (return transactionManager.getTransaction(transactionDefinition);
   <bean id="txManager" class="org.axonframework.unitofwork.SpringTransactionManager"/>


    <axon:event-bus id="eventBus"/>
    <task:executor id="myThreadPool" pool-size="30" />




bui...@gmail.com

unread,
Apr 19, 2014, 7:47:24 AM4/19/14
to axonfr...@googlegroups.com
Hi Viggo,

you don't need to use a TransactionManager at all. Mongo doesn't support transactions. When using e.g. JPA, you can set Spring's PlatformTransactionManager there. 

Removing the tx manager should solve your problem. 
Cheers,

Allard
--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Viggo Navarsete

unread,
Apr 19, 2014, 7:59:16 AM4/19/14
to axonfr...@googlegroups.com
Excellent, thanks Allard, it worked:)

Viggo Navarsete

unread,
Apr 19, 2014, 8:02:13 AM4/19/14
to axonfr...@googlegroups.com
Follow-up question on the Saga: It seems Spring doesn't inject the CommandGateway for me, since I get a NullPointerException when building my project. I have my setter for the commandgateway like this:

    private transient CommandGateway commandGateway;
...
...



    @StartSaga
    @SagaEventHandler(associationProperty = "purchaseOrderId")
     public void handle(TransportOrderSentEvent event) {
         supplierOrderId = "supplier-order-id-" + UUID.randomUUID().toString(); // TODO: Refactor into making GSI-compliant identifiers
         description = "supplier-description-" + UUID.randomUUID().toString(); // TODO: Generate meaningful description
         
         log.debug("L1 - Starting SupplierSaga for purchaseOrderId '" + event.getPurchaseOrderId() + "' -> '" + supplierOrderId + "' (supplierOrderId)");
         
         this.purchaseOrderId = event.getPurchaseOrderId(); // keep track of the purchaseOrderId for this supplierOrderId (should always match)                  
         
         commandGateway.send( new CreateSupplierOrderCommand(supplierOrderId, description)); // NullPointerException thrown when building my project
     }

...
...

    @Resource
    public void setCommandGateway(CommandGateway commandGateway) {
        this.commandGateway = commandGateway;

Allard Buijze

unread,
Apr 20, 2014, 4:13:04 PM4/20/14
to Axon Framework Users
Hi Viggo,

for resources to be injected, you need to specify a "ResourceInjector". Since you're using Spring, you'd most likely want to use the SpringResourceInjector. It will use Spring's injection mechanism to autowire beans.

Unfortunately, there is no axon-mongo namespace support yet. That could automatically take care of all this.

Cheers,

Allard


Szymek Seget

unread,
Apr 1, 2016, 6:12:52 PM4/1/16
to Axon Framework Users
Hi, 
have you any idea how can I handle it? I tried add setters, but I have NPE all the time..

Cheers,
Szymek

René de Waele

unread,
Apr 2, 2016, 2:27:59 AM4/2/16
to axonfr...@googlegroups.com
Hi Szymek,

Your spring config for the mongo saga repository is missing a SpringResourceInjector. Eg change to:

<bean id="mongoSagaRepository" class="org.axonframework.saga.repository.mongo.MongoSagaRepository">
        <constructor-arg ref="sagaMongoTemplate"/>
        <property name="resourceInjector" ref="springResourceInjector"/>
</bean>

You will probably also want to use another serializer than the default JavaSerializer. If so you need to set the serializer property too. 

Rene

Szymek Seget

unread,
Apr 10, 2016, 6:35:05 PM4/10/16
to Axon Framework Users
Yeap, that's it. :)

Thanks,
Szymek
Reply all
Reply to author
Forward
0 new messages