vert-x3 / issues

Apache License 2.0
36 stars 7 forks source link

EventBus could use something like registerDefaultCodecWithoutWire #547

Closed TorRanfelt closed 3 years ago

TorRanfelt commented 3 years ago

I might be an outlier, but I only have one instance of Vert.x (although with many verticles and some thread-pools), so I don't need to neither decode nor encode from wire. Not that it is difficult to get rid of the boiler plate as my code shows, but it feels like it should be an inherent option. - Maybe it shouldn't even be necessary to register a codec if this is all you want.

/**
 * Wire capabilities are necessary when the codec is used for messages that is send between difference Vertx instances.
 * However, if messages are send within a single Vertx instance this message codec will be sufficient.
 */
class VertxMessageCodecWithoutWire<T : Any>(private val messageType: KClass<T>) : MessageCodec<T, T> {
    override fun decodeFromWire(pos: Int, buffer: Buffer?): T {
        LOGGER.error { "Message-codec decode capability no implemented" }
        throw NotImplementedException()
    }

    override fun encodeToWire(buffer: Buffer?, s: T?) {
        LOGGER.error { "Message-codec encode capability no implemented" }
        throw NotImplementedException()
    }

    override fun transform(s: T): T {
        return s
    }

    override fun name(): String {
        return messageType.java.simpleName
    }

    override fun systemCodecID(): Byte {
        return -1
    }
}

fun <T : Any> EventBus.registerDefaultCodecWithoutWire(messageType: KClass<T>) {
    this.registerDefaultCodec(messageType.java, VertxMessageCodecWithoutWire(messageType))
}
tsegismont commented 3 years ago

You could extend the EventBus class and override the default send/publish methods. Instead of using an empty DeliveryOptions, set the codec name.

Then you'd have a single codec to register.

Le jeu. 27 août 2020 à 11:44, TorRanfelt notifications@github.com a écrit :

I might be an outlier, but I only have one instance of Vert.x (although with many verticles and some thread-pools), so I don't need to neither decode nor encode from wire. Not that it is difficult to get rid of the boiler plate as my code shows, but it feels like it should be an inherent option. - Maybe it shouldn't even be necessary to register a codec if this is all you want.

/**

  • Wire capabilities are necessary when the codec is used for messages that is send between difference Vertx instances.
  • However, if messages are send within a single Vertx instance this message codec will be sufficient. */ class VertxMessageCodecWithoutWire(private val messageType: KClass) : MessageCodec<T, T> { override fun decodeFromWire(pos: Int, buffer: Buffer?): T { LOGGER.error { "Message-codec decode capability no implemented" } throw NotImplementedException() }

    override fun encodeToWire(buffer: Buffer?, s: T?) { LOGGER.error { "Message-codec encode capability no implemented" } throw NotImplementedException() }

    override fun transform(s: T): T { return s }

    override fun name(): String { return messageType.java.simpleName }

    override fun systemCodecID(): Byte { return -1 } }

fun EventBus.registerDefaultCodecWithoutWire(messageType: KClass) { this.registerDefaultCodec(messageType.java, VertxMessageCodecWithoutWire(messageType)) }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/vert-x3/issues/issues/547, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALOLNX2HIT6GN3PPPU5RGLSCYTI5ANCNFSM4QMZFATA .

TorRanfelt commented 3 years ago

Good idea.

Even though I don't set CodecName I often use DeliveryOptions (e.g. for SendTimeout), so I have to do something like:

class VertxMessageCodecWithoutWire : MessageCodec<Any, Any> {
    override fun decodeFromWire(pos: Int, buffer: Buffer?): Any {
        LOGGER.error { "Message-codec decode capability no implemented" }
        throw NotImplementedException()
    }

    override fun encodeToWire(buffer: Buffer?, s: Any?) {
        LOGGER.error { "Message-codec encode capability no implemented" }
        throw NotImplementedException()
    }

    override fun transform(s: Any): Any {
        return s
    }

    override fun name(): String {
        return VertxMessageCodecWithoutWire::class.simpleName!!
    }

    override fun systemCodecID(): Byte {
        return -1
    }
}

fun <T : Any> EventBus.internalRequest(address: String, message: Any, options: DeliveryOptions, replyHandler: Handler<AsyncResult<Message<T>>> ) {
   options.codecName = VertxMessageCodecWithoutWire::class.simpleName
    this.request(address, message, options, replyHandler)
}

However, I use many different overloads of send, publish, request and reply, and I will have to make a new version of each one of them. Keeping all these updated as Vert.x develops might get annoying. I think having a bunch of vertx.eventBus().registerDefaultCodecWithoutWire(Foo::class) is simpler. For now.

I would still prefer just not having to register/use a codec when objects of a certain class are only send and received inside an instance. But you could argue that solving this for a single instance can lead to confusion down the road when multiple instances are running.