Vert.x

Eclipse Vert.x is a toolkit for building reactive applications on the JVM. It's event driven, non-blocking, lightweight and fast.

Build configuration

Kilua RPC provides two different modules for Vert.x:

  • kilua-rpc-vertx, which doesn't use dependency injection (DI) and requires manual services registration,

  • kilua-rpc-vertx-koin, which uses Koin dependency injection framework to access services implementations.

You need to add one of these modules to your project.

val commonMain by getting {
    dependencies {
        implementation("dev.kilua:kilua-rpc-vertx:$kiluaRpcVersion")
//        implementation("dev.kilua:kilua-rpc-vertx-koin:$kiluaRpcVersion")
    }
}

Service implementation

Service class

The implementation of the service class comes down to implementing required interface methods.

class AddressService : IAddressService {
    override suspend fun getAddressList(search: String?, sort: Sort) {
        return listOf()
    }
    override suspend fun addAddress(address: Address) {
        return Address()
    }
    override suspend fun updateAddress(id: Int, address: Address) {
        return Address()
    }
    override suspend fun deleteAddress(id: Int) {
        return false
    }
}

You can use @Factory annotation, if you use Koin and koin-annotations to configure dependency injection.

Injecting server objects

You can use constructor parameters to inject server objects - RoutingContext , Vertx and ServerWebSocket (see Websockets chapter) into your service classes. These objects give you access to the application configuration, its state, the current request and the user session.

Blocking code

Since Vert.x architecture is asynchronous and non-blocking, you should never block a thread in your application code. If you have to use some blocking code (e.g. blocking I/O, JDBC) always use the dedicated coroutine dispatcher.

Application configuration

The main verticle

Vert.x services are deployed as "verticles". Main verticle is the application starting point. It's used to initialize and configure application modules and features. Minimal implementation for Kilua RPC integration contains initRpc and applyRoutes function calls.

When using manual service registration, you call initRpc with a lambda function, which binds interfaces with their implementations. Different overloads of registerService function allow injecting server objects into your service classes.

When using Koin, you call initRpc with a list of Koin modules. Constructor parameter injection is automatically supported by Koin.

If you are using websockets, you need to use a special version of initRcp function, and pass a HttpServer instance and a list of all services declaring websocket channel connections.

Security

You can use standard Vert.x configuration to add authentication and authorization to your application. You can use serviceRoute extension function to apply your AuthHandler to the selected Kilua RPC services.

Last updated