Microservices
Custom transporters
Add a message system Kest does not support.
Built-in transports cover TCP, Redis, NATS, MQTT, RabbitMQ, Kafka and gRPC. If you use something else (a cloud queue, an in-house bus) you can add a transport. It is two small interfaces: messages travel as JSON envelope strings, and the transport only moves them. Kest handles everything else: finding the handler, the guard/pipe/interceptor/filter pipeline, encoding and replies.
class MyBusServer : MicroserviceTransport {
override val name = "mine"
override fun listen(patterns: Set<String>, handler: (String) -> String?) {
// subscribe to each pattern; for every message string call handler(msg):
// a request returns the reply envelope to send back; an event returns null
}
override fun close() { /* disconnect */ }
}
class MyBusClient : ClientTransport {
override val name = "mine"
override fun connect() { /* open the connection */ }
override fun request(pattern: String, id: String, dataJson: String, timeout: Duration, headers: Map<String, String>): String = TODO("send, then wait for the reply or throw TimeoutException")
override fun publish(pattern: String, dataJson: String, headers: Map<String, String>) { /* fire and forget */ }
override fun close() { }
}
app.microservice(MyBusServer())
val client = ClientProxy(MyBusClient()) { app.get(Codec::class) }What you implement
| Interface | Methods | Job |
|---|---|---|
MicroserviceTransport (server) | listen(patterns, handler), close() | receive messages for the patterns; give each to handler(messageText); for a request, send back the string it returns |
ClientTransport (caller) | connect(), request(...), publish(...), close() | send a request and wait for the matching reply (by id), or throw TimeoutException; or just publish an event |
The envelope helpers (Envelope.parse(text)) and the router do the rest.
Test it
The shared contract test (TransportContractTest) runs the same checks against every built-in transport (request/reply, events, errors, guards, roles, interceptors, filters, pipes). Run it against yours: if it passes, your transport behaves like the others.
Hints
- Make the client reconnect after the broker restarts, and re-subscribe on every reconnect.
- Never wait forever for a reply: honour
timeout. - Carry
headersend to end so guards work.