Logo

dev-resources.site

for different kinds of informations.

How to extend Vert.x EventBus API to save on serialization.

Published at
4/28/2020
Categories
sip3
kotlin
vertx
performance
Author
agafox
Categories
4 categories in total
sip3
open
kotlin
open
vertx
open
performance
open
Author
6 person written this
agafox
open
How to extend Vert.x EventBus API to save on serialization.

Alexey Soshin has a great blog post: Understanding Vert.x: Event Bus. In the blog post he explains how to extend MessageCodec interface and start sending objects via Event Bus without paying anything for serialization.

SIP3 is a monitoring and troubleshooting platform that has to handle hundreds of thousands SIP packets and millions of RTP packets per second. That's why we've started using this approach from the very beginning. However, as a Kotlin users, we couldn't do not add some sugar 🍬🍭🍰.

Here is our recipe:

🍬 Register MessageCodec

Let's use Kotlin method extensions to define our local codec:

fun Vertx.registerLocalCodec() {
    eventBus().unregisterCodec("local")
    eventBus().registerCodec(object : MessageCodec<Any, Any> {
        override fun decodeFromWire(pos: Int, buffer: Buffer?) = throw NotImplementedError()
        override fun encodeToWire(buffer: Buffer?, s: Any?) = throw NotImplementedError()
        override fun transform(s: Any?) = s
        override fun name() = "local"
        override fun systemCodecID(): Byte = -1
    })
}
Enter fullscreen mode Exit fullscreen mode

All is left is just to register our codec explicitly while initializing Vertx object.

🍭 Extend EventBus

Kotlin has two great features - method extensions and named arguments. We decided to use a combination of those to introduce a custom method called localSend:

object EventBusUtil {

    val USE_LOCAL_CODEC = deliveryOptionsOf(codecName = "local", localOnly = true)
}

fun EventBus.localSend(address: String, message: Any, options: DeliveryOptions? = null) {
    options?.apply {
        codecName = "local"
        isLocalOnly = true
    }
    send(address, message, options ?: USE_LOCAL_CODEC)
}
Enter fullscreen mode Exit fullscreen mode

🍰 Just enjoy

Now we can start sending messages within the application without paying for serialization:

fun main() {
    val vertx = Vertx.vertx()
    vertx.registerLocalCodec()

    val answer = BigDecimal(42)
    vertx.eventBus().localSend("question", answer)
}
Enter fullscreen mode Exit fullscreen mode

I hope you loved the approach and will start using it in your projects. If you have any questions, just leave me a message πŸ˜„

Happy coding...

vertx Article's
28 articles in total
Favicon
Error handlers and failure handlers in Vert.x
Favicon
Why we discarded Reactive systems architecture from our code?
Favicon
Build web application in Vert.x [Part 1/ ♾️]
Favicon
Yet another ode to Vert.x, or how to write a performance-wise expiring map in less than 100 lines of code.
Favicon
Surprising Qualities of Event Driven System
Favicon
Idiomatic Kotlin Abstractions for the Vert.x EventBus
Favicon
Vert.x Circuit Breaker
Favicon
Writing Async Tests for Vert.x using Kotlin
Favicon
Reducing Boilerplate in Vert.x Tests written in Kotlin
Favicon
Writing Vert.x Integration Tests with Kotlin & Testcontainers
Favicon
Quarkus: Entendendo a relação entre o Mutiny e o Vert.x
Favicon
HTTPS Client Certificate Authentication With Java
Favicon
Throttle HTTP requests on paged resources with Vert.x
Favicon
Supercharge Your Kotlin Vert.x Application with EventBus Extensions
Favicon
Handle backpressure between Kafka and a database with Vert.x
Favicon
Handling unknown JSON structures
Favicon
Introduction to Vert.x
Favicon
Future Composition in Vert.x
Favicon
How to extend Vert.x EventBus API to save on serialization.
Favicon
How to write beautiful unit tests in Vert.x
Favicon
Scaling Vert.x application for session dependent data processing.
Favicon
KVision v3.7.0 is released (with Vert.x support)
Favicon
Reactive Java using the Vert.x toolkit
Favicon
Vert.x Kotlin Coroutines
Favicon
How we built a RESTful API with Vert.x, Kotlin Coroutines and Keycloak
Favicon
vertx-jooq 2.4 released
Favicon
Sirix - Released 0.9.1 (time travel queries and versioning made easy)
Favicon
Reactive Programming with Kotlin - Quick Intro to Vert.x

Featured ones: