Search code examples
javavert.xevent-bus

Is it possible to avoid serialization when using the Vert.x eventbus 'locally' (java, single jvm)?


My case is:

  • single JVM
  • Java only (i don't need to be polyglot)
  • I don't want to pay serialization costs to publish an immutable event on the bus (publishing the reference to the java object would work).

I understand the scope of the vert.x event bus is much broader than my use case.

I had in mind a behaviour similar to akka: when you go distributed you have to provide serialization for your messages, if you stay local references get passed.

Is there anything that would allow me to do that in Vert.x?


Solution

  • Vert.x already has such an optimization. When sending to the same JVM, objects won't get serialized or deserialized.

    You can see the actual code here: https://github.com/eclipse/vert.x/blob/master/src/main/java/io/vertx/core/eventbus/impl/EventBusImpl.java#L372

    When you implement your MessageCodec, you actually have two methods: decodeFromWire() and transform(). You can implement only transform with the most naive approach:

    @Override
    public Object transform(Object o) {
       return o;
    }