Kotlin
Kotlin 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 Kotlin Extension is to remove that awkwardness, by providing inline and reified 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 commands, events and queries section on this page.
Experimental Release
Currently, the Kotlin Extension 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 command dispatching and handling 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 CommandCallback directly yourself.
Here is a sample of how this can be utilized within your own project:
1
import org.axonframework.commandhandling.CommandMessage
2
import org.axonframework.commandhandling.gateway.CommandGateway
3
import org.axonframework.messaging.MetaData
4
import org.slf4j.LoggerFactory
5
6
class CommandDispatcher(private val commandGateway: CommandGateway) {
7
8
private val logger = LoggerFactory.getLogger(CommandDispatcher::class.java)
9
10
// Sample usage providing specific logging logic, next to for example the LoggingInterceptor
11
fun issueCardCommand() {
12
commandGateway.send(
13
command = IssueCardCommand(),
14
onSuccess = { message: CommandMessage<out IssueCardCommand>, result: Any, _: MetaData ->
15
logger.info("Successfully handled [{}], resulting in [{}]", message, result)
16
},
17
onError = { result: Any, exception: Throwable, _: MetaData ->
18
logger.warn(
19
"Failed handling the IssueCardCommand, with output [{} and exception [{}]",
20
result, exception
21
)
22
}
23
)
24
}
25
}
26
27
class IssueCardCommand
Copied!

Events

This section describes the additional functionality attached to Axon's event publication and handling logic.

Event Upcasters

A simplified implementation of the Single Event Upcaster 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:
1
import com.fasterxml.jackson.databind.JsonNode
2
import org.axonframework.serialization.upcasting.event.SingleEventUpcaster
3
4
fun `CardIssuedEvent 0 to 1 Upcaster`(): SingleEventUpcaster =
5
EventUpcaster.singleEventUpcaster(
6
eventType = CardIssuedEvent::class,
7
storageType = JsonNode::class,
8
revisions = Revisions("0", "1")
9
) { event ->
10
// Perform your upcasting process of the CardIssuedEvent here
11
event
12
}
13
14
class CardIssuedEvent
Copied!
Alternatively, since Revisions is essentially a Pair of String, it is also possible to use Kotlin's to function:
1
EventUpcaster.singleEventUpcaster(
2
eventType = CardIssuedEvent::class,
3
storageType = JsonNode::class,
4
revisions = "0" to "1"
5
) { event ->
6
// Perform your upcasting process of the CardIssuedEvent here
7
event
8
}
Copied!

Queries

This section describes the additional functionality attached to Axon's query dispatching and handling logic.

QueryGateway

Several inlined methods have been introduced on the QueryGateway to use generics instead of explicit Class objects and ResponseType parameters.
1
import org.axonframework.queryhandling.QueryGateway
2
3
class QueryDispatcher(private val queryGateway: QueryGateway) {
4
fun getTotalNumberOfCards(): Int {
5
val query = CountCardSummariesQuery()
6
// Query will return a CompletableFuture so it has to be handled
7
return queryGateway.query<Int, CountCardSummariesQuery>(query)
8
.join()
9
}
10
}
11
12
data class CountCardSummariesQuery(val filter: String = "")
Copied!
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:
1
import org.axonframework.queryhandling.QueryGateway
2
import java.util.concurrent.CompletableFuture
3
4
class QueryDispatcher(private val queryGateway: QueryGateway) {
5
fun getTotalNumberOfCards(): CompletableFuture<Int> =
6
queryGateway.query(CountCardSummariesQuery())
7
}
8
9
data class CountCardSummariesQuery(val filter: String = "")
Copied!
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.
1
import org.axonframework.queryhandling.QueryUpdateEmitter
2
import org.axonframework.eventhandling.EventHandler
3
4
class CardSummaryProjection (private val queryUpdateEmitter : QueryUpdateEmitter) {
5
@EventHandler
6
fun on(event : CardIssuedEvent) {
7
// Update projection here
8
9
// Then emit the CountChangedUpdate to subscribers of CountCardSummariesQuery
10
// with the given filter
11
queryUpdateEmitter
12
.emit<CountCardSummariesQuery, CountChangedUpdate>(CountChangedUpdate()) { query ->
13
// Sample filter based on ID field
14
event.id.startsWith(query.idFilter)
15
}
16
}
17
}
18
19
class CardIssuedEvent(val id : String)
20
class CountChangedUpdate
21
data class CountCardSummariesQuery(val idFilter: String = "")
Copied!
Last modified 7mo ago