Search code examples
springkotlinaxon

Axon external event handler not working and table not being created


i'm new to axon. i want to make simple axon app.

i'm

  1. using two instances (command app, query app)
  2. using axon server
  3. using kotlin
  4. using multi-module
  5. axon framework 4.5

and i checked command app that storing event data from dashboard(http://localhost:8024)

However, event handler not invoked

here is project structure

practice-root
│
├── command
│   ├── build
│   ├── src
│   │    ├── main
│   │    │     ├ kotlin
│   │    │     │  └ com.cqrs.axon
│   │    │     │         ├ Application.kt
│   │    │     │         ├ SimpleDTO
│   │    │     │         ├ SimpleController
│   │    │     │         ...
│   │    │     └ resources
│   │    ├── test
│   │    │
│   └── build.gradle
│
├── query
│     ├ ...
│     ...
│
├── README
├── build.gradle
└── settings.gradle

here is my code

command module

Application.kt

@SpringBootApplication()
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}

SimpleService.kt

@Service
class SimpleService(
    private val eventGateway: EventGateway
) {
    @CommandHandler
    fun createSimple(simpleDTO: SimpleDTO): Unit {
        return this.eventGateway.publish(
            SimpleEvent(
                id = UUID.randomUUID().toString(),
                data = simpleDTO.data
            )
        )
    }
}

SimpleDTO.kt

data class SimpleDTO (
    val data: String
)

SimpleController.kt


@RestController
class SimpleController(
    private val simpleService: SimpleService
    ) {

    @PostMapping("/simple")
    fun createSimple(@RequestBody simpleDTO: SimpleDTO): Unit {
        return simpleService.createSimple(simpleDTO)
    }
}

application.yml

---
server:
  port: 8080

spring:
  application:
    name: commandSpringApplication
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:33060/test?useSSL=false&characterEncoding=utf8&useUnicode=true
    username: userA
    password: u123

  jpa:
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL8Dialect
        ddl-auto: update
        format_sql: true
        jdbc:
          time_zone: UTC
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

axon:
  serializer:
    general: xstream
  axonserver:
    servers: localhost:8124

logging:
  level:
    com:
      cqrs:
        command: debug
    org:
      axonframework: debug

query module

Application.kt

@SpringBootApplication()
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}

QuerySimpleProjection.kt

@Component
class QuerySimpleProjection (
    private val simpleRepository: QuerySimpleRepository
) {

    @EventHandler
    fun on(event: SimpleEvent, @Timestamp instant: Instant) {
        val simpleMV = SimpleMV(
            id = event.id,
            data = event.data
        )
        simpleRepository.save(simpleMV)
    }
}

QuerySimpleRepository.kt

@Repository
interface QuerySimpleRepository : JpaRepository<SimpleMV, String>

SimpleMV.kt

@Entity
@Table(name = "mv_simple")
data class SimpleMV (

    @Id
    val id: String,
    val data: String
)

AxonConfig.kt

@Configuration
class AxonConfig {

    @Autowired
    fun configureProcessorDefault(processingConfigurer: EventProcessingConfigurer) {
        processingConfigurer.usingSubscribingEventProcessors()
    }
}

application.yml

---
server:
  port: 9090

spring:
  application:
    name: querySpringApplication
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:33060/test?useSSL=false&characterEncoding=utf8&useUnicode=true
    username: userA
    password: u123

  jpa:
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL8Dialect
        ddl-auto: create
        format_sql: true
        jdbc:
          time_zone: UTC

axon:
  serializer:
    general: xstream
  axonserver:
    servers: localhost:8124


logging:
  level:
    com:
      cqrs:
        command: debug
    org:
      axonframework: debug

common module

SimpleEvent.kt

data class SimpleEvent (
    val id: String,
    val data: String
)

build.gradle of command module and query module

apply plugin: 'kotlin-jpa'
apply plugin: 'org.springframework.boot'
apply plugin: 'kotlin-allopen'
...
dependencies {
    implementation project(':common')
    implementation 'org.springframework.boot:spring-boot-starter-validation'

    implementation 'mysql:mysql-connector-java'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation "org.axonframework:axon-spring-boot-starter:4.5.8"
    implementation "org.axonframework:axon-configuration:4.5.8"
    implementation "org.axonframework:axon-server-connector:4.5.8"
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

I thought that the event would be stored in the command module and data would be included in the mv_simple table of the query module. The table was not created either.

What should I do to make it work?


[update]

i change to this

 spring:
  jpa:
    show-sql: true
    properties:
      hibernate.hbm2ddl.auto: update
      ...

now i can see tables that created. but still can't store data to mv_simple table.

eventhandler isn't invoked


Solution

  • change common package names to com.cqrs.axon. it's impotant to use same package names! it works after removing Axonconfig.kt to use Tracking Event Processor in above code.