Skip to content

wowselim/eventbus-service

Repository files navigation

EventBus Service

EbService generates kotlin code that enables a type-safe way of using the Vert.x EventBus. The generated code eliminates the need of guessing which types are required by EventBus consumers and which types are produced by them.

On top of that, the generated functions avoid unnecessary serialization and deserialization by making use of a special EventBus codec.

Getting started

Imagine we have a service that can divide a double by another double.

We might model this service as follows:

interface DivisionService {
  suspend fun divide(dividend: Double, divisor: Double): Division

  sealed interface Division {
    data class Success(val quotient: Double) : Division
    data class Error(val message: String) : Division
  }
}

Next, we need to annotate this interface as follows:

@EventBusService
interface DivisionService

This will generate two things

  • An implementation of this service (DivisionServiceImpl) that translates function calls into EventBus requests.
  • A function that allows you to handle those requests:
    object DivisionServiceRequests {
    
    fun divide(
      vertx: Vertx
    ): Flow<EventBusServiceRequest<DivideParameters, Division>>
    
    }

Since the function has two parameters, we need to wrap them in a container. This is handled automatically via a generated data class (DivideParameters).

Before running the application, we need to call EventBus#initializeServiceCodec, which also lets us add our own codecs in case we want the code to run in a clustered setup.

This service is fully implemented in the example module.

Adding it to your project

Add the JitPack repository to your build script and include the following dependencies:

implementation 'com.github.wowselim.eventbus-service:eventbus-service-core:<latestVersion>'
ksp 'com.github.wowselim.eventbus-service:eventbus-service-codegen:<latestVersion>'

The latest version can be found in releases.

Debugging

To debug the code generator, run the kspKotlin task in the following way to be able to attach the debugger:

./gradlew clean kspKotlin --no-daemon -Dorg.gradle.debug=true -Dkotlin.compiler.execution.strategy="in-process" -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=5005\,server=y\,suspend=n"