Axon State-Stored Aggregates with Axon Configuration API (without spring)

Hi,

Im new with Java, Axon and Event sourcing approach.

Im doing some experiments with Axon and Vertx and first problem that i have i don’t want to use Axon with Spring.

First issue i face is that there is no example of implementing Axon without Spring framework.

After few days of searching and googling how to use and initialise configuration, commandGateway and queryGateway i was able to create simple PoC app based on GiftCard example without spring framework.

Now i face new problem where i want to implement State-Stored Aggregates with MySQL or Mongo.
From reference guide i found only this but it seems incomplete and i was not able to found any example of how to use it (all examples online using Spring).

I would appreciate if someone do something similar or if there is some example or guide i can follow.

Thanks in advance

This is what i end up with:

`

val projection = DeviceSummaryProjection()
val eventHandlingConfiguration = EventProcessingModule()
eventHandlingConfiguration.registerEventHandler { c -> projection }

DefaultConfigurer.defaultConfiguration().configureAggregate(Device::class.java)

val emf = Persistence.createEntityManagerFactory("DevicesManager")
val em = emf.createEntityManager()

val configuration = DefaultConfigurer.defaultConfiguration()
        .configureAggregate(AggregateConfigurer.defaultConfiguration(Device::class.java).configureRepository{c -> GenericJpaRepository.builder(Device::class.java).entityManagerProvider(EntityManagerProvider { em }).eventBus(SimpleEventBus.builder().build()).build()})
        .registerModule(eventHandlingConfiguration) // (3)
        .registerQueryHandler { c -> projection } // (4)
        .buildConfiguration() // (5)

configuration.start()

commandGateway = configuration.commandGateway()

commandGateway.sendAndWait<CreateDeviceCmd>(CreateDeviceCmd(UUID.randomUUID().toString(), "FA123DB11A1111", null, null))

`

I’m getting " javax.persistence.TransactionRequiredException: no transaction is in progress"

INFO] javax.persistence.TransactionRequiredException: no transaction is in progress

[INFO] at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3613)

[INFO] at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1474)

[INFO] at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1470)

[INFO] at org.axonframework.modelling.command.GenericJpaRepository.doSaveWithLock(GenericJpaRepository.java:145)

[INFO] at org.axonframework.modelling.command.GenericJpaRepository.doSaveWithLock(GenericJpaRepository.java:53)

[INFO] at org.axonframework.modelling.command.LockingRepository.doSave(LockingRepository.java:149)

[INFO] at org.axonframework.modelling.command.LockingRepository.doSave(LockingRepository.java:52)

[INFO] at org.axonframework.modelling.command.AbstractRepository.doCommit(AbstractRepository.java:194)

[INFO] at org.axonframework.modelling.command.AbstractRepository.prepareForCommit(AbstractRepository.java:180)

[INFO] at org.axonframework.modelling.command.LockingRepository.prepareForCommit(LockingRepository.java:131)

[INFO] at org.axonframework.modelling.command.LockingRepository.prepareForCommit(LockingRepository.java:52)

[INFO] at org.axonframework.modelling.command.AbstractRepository.lambda$newInstance$0(AbstractRepository.java:86)

[INFO] at org.axonframework.messaging.unitofwork.MessageProcessingContext.notifyHandlers(MessageProcessingContext.java:71)

[INFO] at org.axonframework.messaging.unitofwork.DefaultUnitOfWork.notifyHandlers(DefaultUnitOfWork.java:106)

[INFO] at org.axonframework.messaging.unitofwork.AbstractUnitOfWork.changePhase(AbstractUnitOfWork.java:222)

[INFO] at org.axonframework.messaging.unitofwork.AbstractUnitOfWork.commitAsRoot(AbstractUnitOfWork.java:83)

[INFO] at org.axonframework.messaging.unitofwork.AbstractUnitOfWork.commit(AbstractUnitOfWork.java:71)

[INFO] at org.axonframework.messaging.unitofwork.DefaultUnitOfWork.executeWithResult(DefaultUnitOfWork.java:92)

[INFO] at org.axonframework.commandhandling.SimpleCommandBus.handle(SimpleCommandBus.java:176)

[INFO] at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:141)

[INFO] at org.axonframework.commandhandling.SimpleCommandBus.dispatch(SimpleCommandBus.java:110)

[INFO] at org.axonframework.commandhandling.gateway.AbstractCommandGateway.send(AbstractCommandGateway.java:75)

[INFO] at org.axonframework.commandhandling.gateway.DefaultCommandGateway.send(DefaultCommandGateway.java:73)

[INFO] at org.axonframework.commandhandling.gateway.DefaultCommandGateway.sendAndWait(DefaultCommandGateway.java:90)

[INFO] at com.company.labs.device.command.MainVerticle.start(MainVerticle.kt:116)

[INFO] at com.company.labs.device.command.MainVerticle$start$1.invokeSuspend(MainVerticle.kt)

[INFO] at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)

[INFO] at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)

[INFO] at io.vertx.kotlin.coroutines.VertxCoroutineExecutor.execute(VertxCoroutine.kt:215)

[INFO] at kotlinx.coroutines.ExecutorCoroutineDispatcherBase.dispatch(Executors.kt:64)

[INFO] at kotlinx.coroutines.DispatchedKt.dispatch(Dispatched.kt:256)

[INFO] at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:218)

[INFO] at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:227)

[INFO] at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:168)

[INFO] at io.vertx.kotlin.coroutines.VertxCoroutineKt$awaitEvent$2$1.handle(VertxCoroutine.kt:52)

[INFO] at io.vertx.config.impl.ConfigRetrieverImpl.lambda$getConfig$2(ConfigRetrieverImpl.java:180)

[INFO] at io.vertx.config.impl.ConfigRetrieverImpl.lambda$compute$9(ConfigRetrieverImpl.java:294)

[INFO] at io.vertx.core.impl.CompositeFutureImpl.lambda$all$1(CompositeFutureImpl.java:49)

[INFO] at io.vertx.core.impl.FutureImpl.tryComplete(FutureImpl.java:125)

[INFO] at io.vertx.config.impl.ConfigRetrieverImpl.lambda$null$6(ConfigRetrieverImpl.java:264)

[INFO] at io.vertx.config.impl.ConfigurationProvider.lambda$null$0(ConfigurationProvider.java:83)

[INFO] at io.vertx.config.impl.spi.JsonProcessor.process(JsonProcessor.java:42)

[INFO] at io.vertx.config.impl.ConfigurationProvider.lambda$get$1(ConfigurationProvider.java:71)

[INFO] at io.vertx.core.impl.ContextImpl.lambda$null$0(ContextImpl.java:284)

[INFO] at io.vertx.core.impl.ContextImpl.executeTask(ContextImpl.java:320)

[INFO] at io.vertx.core.impl.EventLoopContext.lambda$executeAsync$0(EventLoopContext.java:38)

[INFO] at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)

[INFO] at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)

[INFO] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:462)

[INFO] at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897)

[INFO] at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)

[INFO] at java.base/java.lang.Thread.run(Thread.java:834)

[INFO] Mar 25, 2019 3:56:41 PM io.vertx.core.impl.launcher.commands.VertxIsolatedDeployer

[INFO] SEVERE: Failed in deploying verticle

Here is persistance.xml

`

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="DevicesManager" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.company.labs.device.command.command.Device</class>
    <properties>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value="root"/>
        <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/test"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    </properties>
</persistence-unit>
</persistence>

`

Anyone know what i’m missing or doing wrong?

Hi Amer,

From the exception you’re getting, I assume you need to specify the TransactionManager which Axon will leverage to wrap the Commands and Events in a Transaction.
My hunch is you need to specify a TransactionManagingInterceptor on the CommandGateway.

It will ensure that, prior to handling the Command, a Transaction will be started.

This should ensure that the storing of the Events and the Aggregate is performed in a Transaction.

Hope this helps!

Cheers,
Steven