/kediatR

Mediator implementation in Kotlin with native coroutine support

Primary LanguageKotlinMIT LicenseMIT

kediatR codecov

Humus! The kediatr mascot

Mediator implementation in kotlin with native coroutine support. Supports Spring-Boot, Quarkus and Koin dependency providers.

Documentation is available at https://trendyol.github.io/kediatR/

Show me the code

class PingCommand: Command // or
class PingQuery: Query<String> // or
class PingNotification: Notification
class PingCommandHandler: CommandHandler<PingCommand> {
    override suspend fun handle(command: PingCommand) {
        println("Pong!")
    }
}
class PingQueryHandler: QueryHandler<PingQuery, String> {
    override suspend fun handle(query: PingQuery): String {
        return "Pong!"
    }
}

class PingNotificationHandler: NotificationHandler<PingNotification> {
    override suspend fun handle(notification: PingNotification) {
        println("Pong!")
    }
}

class MeasurePipelineBehaviour: PipelineBehaviour {
    override suspend fun <TRequest, TResponse> handle(
        request: TRequest,
        next: RequestHandlerDelegate<TRequest, TResponse>
    ): TResponse {
        val start = System.currentTimeMillis()
        val response = next(request)
        val end = System.currentTimeMillis()
        println("Request ${request::class.simpleName} took ${end - start} ms")
        return response
    }
} 

val mediator = // create mediator instance in-memory or with dependency injection, take a look at the documentation
mediator.send(PingCommand()) // 1..1
mediator.send(PingQuery()) // 1..1
mediator.send(PingNotification()) // 0..N