Search code examples
exceptionvert.xevent-bus

vertx IllegalArgumentException: No message codec for type - how to create a consumer for a custom type


I have a verticle that creates an event bus consumer as follows:

    public void start() {
        vertx.eventBus().consumer(ADDRESS_REQUEST, this::handleRequestMessage);

    }

    private void handleRequestMessage(Message<VWApiConversation> msg) {

       VWApiConversation conversation = msg.body();

    }

But when sending a message to this address :

 vertx.eventBus().send(VehicleStateCoordinatorVerticle.ADDRESS_REQUEST, conversation, deliveryOptions, res -> {

...

I get the error :

java.lang.IllegalArgumentException: No message codec for type: class com.vulog.vwgateway.model.VWApiConversation

Am I missing something?


Solution

  • Vert.x supports serializing JVM primitives, Buffers, and JsonObjects by default. for other custom types you'll need to write your own MessageCodec.

    here's some documentation that might be of help:

    • the official docs has a few notes about this. the section titled "Types of messages" will be of particular interest to you.
    • here is a sample MessageCodec implementation. (not shown in this snippet is registration of the codec via EventBus.registerCodec().)

    for my taste i've always used JsonObject as the messaging medium (as my setups have enabled me to). seems like a hassle to write custom (de)serializers for every domain type.