Message codec example for vert.x event bus.
I have a use case where I want to register my custom object to send it on the event bus.For that,I have to implement Message codec class .I would like to know if more than one object can be registered under the same name with event bus?If not,what is the correct way of registering multiple custom objects as codec.
If you're not using clustered EventBus, then you can just implement every codec like this:
class IdentityCodec implements MessageCodec {
private final Class aClass;
public IdentityCodec(Class aClass) {
this.aClass = aClass;
}
@Override
public void encodeToWire(Buffer buffer, Object o) {
}
@Override
public Object decodeFromWire(int pos, Buffer buffer) {
return null;
}
@Override
public Object transform(Object o) {
return o;
}
@Override
public String name() {
return aClass.getName() + "Codec";
}
@Override
public byte systemCodecID() {
return -1;
}
}
The important part here is that every codec must have a unique name, so I'm generating it based on the class name.
Then register it as a default codec:
vertx.eventBus().registerDefaultCodec(A.class, new IdentityCodec(A.class));
vertx.eventBus().registerDefaultCodec(B.class, new IdentityCodec(B.class));
Probably there are nicer ways to generify it, but without seeing your code, it's impossible to tell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With