Axon 参考指南
  • 介绍
  • 架构概览
    • DDD & CQRS 概念
    • 事件溯源
    • 事件驱动的微服务
  • Axon Server
  • 发行说明
    • Axon Framework
      • Major Releases
      • Minor Releases
    • Axon Server
      • Major Releases
      • Minor Releases Standard Edition
      • Minor Releases Enterprise Edition
    • Axon Framework Extensions
      • AMQP
        • Major Releases
      • CDI
        • Major Releases
      • JGroups
        • Major Releases
      • Kafka
        • Major Releases
        • Minor Releases
      • Kotlin
        • Experimental Releases
      • Mongo
        • Major Releases
        • Minor Releases
      • Reactor
        • Major Releases
        • Minor Releases
      • Spring Cloud
        • Major Releases
        • Minor Releases
      • Tracing
        • Major Releases
        • Minor Releases
  • Getting Started
    • 快速开始
  • Axon Framework
    • 介绍
    • 消息传递概念
      • 消息剖析
      • 消息关联
      • 消息拦截
      • 支持带注解的处理程序
      • 异常处理
      • 工作单元
    • 命令
      • 建模
        • 聚合
        • 多实体聚合
        • 聚合状态存储
        • 从另一个聚合创建聚合
        • 聚合多态性
        • 解决冲突
      • 命令调度器
      • 命令处理程序
      • 基础设施
      • 配置
    • 事件
      • 事件调度器
      • 事件处理程序
      • 事件处理器
        • 订阅事件处理器
        • 流式事件处理器
      • 事件总线和事件存储
      • 事件版本控制
    • 查询
      • 查询处理
      • 查询调度器
      • 查询处理程序
      • 实现
      • 配置
    • 长时处理过程(Sagas)
      • 实现
      • 关联
      • 基础设施
    • Deadlines
      • Deadline Managers
      • Event Schedulers
    • 测试
      • 命令 / 事件
      • 长时处理过程(Sagas)
    • 序列化
    • 调整
      • 事件快照
      • 事件处理
      • 命令处理
    • 监控和指标
    • Spring Boot 集成
    • 模块
  • Axon Server
    • 介绍
    • 安装
      • 本地安装
        • Axon Server SE
        • Axon Server EE
      • Docker / K8s
        • Axon Server SE
        • Axon Server EE
    • 管理
      • 配置
        • System Properties
        • Command Line Interface
        • REST API
        • GRPC API
      • Monitoring
        • Actuator Endpoints
        • gRPC Metrics
        • Heartbeat Monitoring
      • Clusters
      • Replication Groups
      • Multi-Context
      • Tagging
      • Backup and Messaging-only Nodes
      • Backups
      • Recovery
      • Plugins
      • Error Codes
    • 安全
      • SSL
      • 访问控制
      • 访问控制 - 标准版
      • 访问控制 - 企业版
      • 访问控制 - 客户端应用程序
      • 访问控制 - 命令行
      • 访问控制 - REST API
      • 访问控制 - LDAP
      • 访问控制 - OAuth 2.0
    • 性能
      • 事件段
      • 流量控制
    • 迁移
      • Standard to Enterprise Edition
      • Non-Axon Server to Axon Server
  • Extensions
    • Spring AMQP
    • JGroups
    • Kafka
    • Kotlin
    • Mongo
    • Reactor
      • Reactor Gateways
    • Spring Cloud
    • Tracing
  • Appendices
    • A. RDBMS Tuning
    • B. Message Handler Tuning
      • 参数解析器
      • 处理程序增强
    • C. 元数据注解
    • D. 标识符生成
    • E. Axon Server Query Language
由 GitBook 提供支持
在本页
  • Commands
  • CommandGateway
  • Events
  • Event Upcasters
  • Queries
  • QueryGateway
  • QueryUpdateEmitter
  1. Extensions

Kotlin

上一页Kafka下一页Mongo

最后更新于2年前

is a programming language which interoperates fully with Java and the JVM. As Axon is written in Java it can be used in conjunction with Kotlin too, offering a different feel when using the framework.

Some of Axon's API's work perfectly well in Java, but have a rather awkward feel when transitioning over to Kotlin. The goal of the is to remove that awkwardness, by providing methods of Axon's API.

Several solutions are currently given, which can roughly be segregated into the distinct types of messages used by Axon. This thus provides a , and section on this page.

Experimental Release

Currently, the has been release experimentally (e.g. release 0.1.0). This means that all implementations are subject to change until a full release (e.g. a release 1.0.0) has been made.

Commands

This section describes the additional functionality attached to Axon's logic.

CommandGateway

An inlined method has been introduced on the CommandGateway which allows the introduction of a dedicated function to be invoked upon success or failure of handling the command. As such it provides a shorthand instead of using the directly yourself.

Here is a sample of how this can be utilized within your own project:

import org.axonframework.commandhandling.CommandMessage
import org.axonframework.commandhandling.gateway.CommandGateway
import org.axonframework.messaging.MetaData
import org.slf4j.LoggerFactory

class CommandDispatcher(private val commandGateway: CommandGateway) {
    
    private val logger = LoggerFactory.getLogger(CommandDispatcher::class.java)

    // Sample usage providing specific logging logic, next to for example the LoggingInterceptor
    fun issueCardCommand() {
        commandGateway.send(
                command = IssueCardCommand(),
                onSuccess = { message: CommandMessage<out IssueCardCommand>, result: Any, _: MetaData ->
                    logger.info("Successfully handled [{}], resulting in [{}]", message, result)
                },
                onError = { result: Any, exception: Throwable, _: MetaData ->
                    logger.warn(
                            "Failed handling the IssueCardCommand, with output [{} and exception [{}]",
                            result, exception
                    )
                }
        )
    }
}

class IssueCardCommand

Events

Event Upcasters

import com.fasterxml.jackson.databind.JsonNode
import org.axonframework.serialization.upcasting.event.SingleEventUpcaster

fun `CardIssuedEvent 0 to 1 Upcaster`(): SingleEventUpcaster =
        EventUpcaster.singleEventUpcaster(
                eventType = CardIssuedEvent::class,
                storageType = JsonNode::class,
                revisions = Revisions("0", "1")
        ) { event ->
            // Perform your upcasting process of the CardIssuedEvent here
            event
        }

class CardIssuedEvent
EventUpcaster.singleEventUpcaster(
        eventType = CardIssuedEvent::class,
        storageType = JsonNode::class,
        revisions = "0" to "1"
) { event ->
    // Perform your upcasting process of the CardIssuedEvent here
    event
}

Queries

QueryGateway

Several inlined methods have been introduced on the QueryGateway to use generics instead of explicit Class objects and ResponseType parameters.

import org.axonframework.queryhandling.QueryGateway

class QueryDispatcher(private val queryGateway: QueryGateway) {
    fun getTotalNumberOfCards(): Int {
           val query = CountCardSummariesQuery()
           // Query will return a CompletableFuture so it has to be handled
           return queryGateway.query<Int, CountCardSummariesQuery>(query)
                   .join()
       }
}

data class CountCardSummariesQuery(val filter: String = "")

In some cases, Kotlin's type inference system can deduce types without explicit generic parameters. One example of this would be an explicit return parameter:

import org.axonframework.queryhandling.QueryGateway
import java.util.concurrent.CompletableFuture

class QueryDispatcher(private val queryGateway: QueryGateway) {
    fun getTotalNumberOfCards(): CompletableFuture<Int> =
            queryGateway.query(CountCardSummariesQuery())
}

data class CountCardSummariesQuery(val filter: String = "")

There are multiple variants of the query method provided, for each type of ResponseType:

  • query

  • queryOptional

  • queryMany

QueryUpdateEmitter

An inline emit method has been added to QueryUpdateEmitter to simplify emit method's call by using generics and moving the lambda predicate at the end of parameter list. This way the lambda function can be moved outside of the parentheses.

import org.axonframework.queryhandling.QueryUpdateEmitter
import org.axonframework.eventhandling.EventHandler

class CardSummaryProjection (private val queryUpdateEmitter : QueryUpdateEmitter) {
    @EventHandler
    fun on(event : CardIssuedEvent) {
        // Update projection here

        // Then emit the CountChangedUpdate to subscribers of CountCardSummariesQuery
        // with the given filter
        queryUpdateEmitter
                .emit<CountCardSummariesQuery, CountChangedUpdate>(CountChangedUpdate()) { query ->
                    // Sample filter based on ID field
                    event.id.startsWith(query.idFilter)
                }
    }
}

class CardIssuedEvent(val id : String)
class CountChangedUpdate
data class CountCardSummariesQuery(val idFilter: String = "")

This section describes the additional functionality attached to Axon's logic.

A simplified implementation of the is given, which allows for a shorter implementation cycle. Making an upcaster to upcast the CardIssuedEvent from revision 0 to 1 can be written as follows:

Alternatively, since Revisions is essentially a Pair of String, it is also possible to use Kotlin's :

This section describes the additional functionality attached to Axon's logic.

event publication and handling
to function
query dispatching and handling
Kotlin
Kotlin Extension
inline and reified
Kotlin Extension
command dispatching and handling
CommandCallback
commands
events
queries
Single Event Upcaster