Trying to write a simple string to a socket's writeHandlerID

252 views
Skip to first unread message

murtuza chhil

unread,
Jan 19, 2018, 3:39:25 AM1/19/18
to vert.x
Hi,

When I try to use the writeHandleID of a socket as below , I get a cast exception, the NetSocket api expects a buffer. I understand I can use the socket, 
this is me trying to learn using the eventbus.


                        vertx.eventBus()
                             .send(netSocket.writeHandlerID(), "4TEST");

SEVERE: Failed to handleMessage. address: 5a56a125-fc81-4e36-a3b1-cbb10ed2b161
java.lang.ClassCastException: java.lang.String cannot be cast to io.vertx.core.buffer.Buffer
    at io.vertx.core.net.impl.NetSocketImpl.lambda$new$1(NetSocketImpl.java:95)
    at io.vertx.core.eventbus.impl.HandlerRegistration.deliver(HandlerRegistration.java:212)
    at io.vertx.core.eventbus.impl.HandlerRegistration.handle(HandlerRegistration.java:189)
    at io.vertx.core.eventbus.impl.EventBusImpl.lambda$deliverToHandler$3(EventBusImpl.java:538)
    at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:344)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:16

And when I pass a buffer, eventbus complains that it needs a codec as it doesn't understand buffers as it supports JSONObjects and Strings out of the box. 

                        vertx.eventBus()
                             .send(netSocket.writeHandlerID(), Buffer.buffer("4TEST"));

java.lang.IllegalArgumentException: No message codec for type: class io.vertx.reactivex.core.buffer.Buffer
    at io.vertx.core.eventbus.impl.CodecManager.lookupCodec(CodecManager.java:90)
    at io.vertx.core.eventbus.impl.clustered.ClusteredEventBus.createMessage(ClusteredEventBus.java:203)
    at io.vertx.core.eventbus.impl.EventBusImpl.send(EventBusImpl.java:110)
    at io.vertx.core.eventbus.impl.EventBusImpl.send(EventBusImpl.java:95)
    at chhil.vertx.example2.TCPServer.lambda$3(TCPServer.java:108)

Is there a codec for the simple conversion between vertx buffers and java strings?
Do I need to write one for this simple conversion? 
Do these writeHandlerIDs work across jvms and across machines boundaries in a cluster? 

Any help will be appreciated.

thanks
-chhil

Julien Viet

unread,
Jan 19, 2018, 4:22:45 AM1/19/18
to vert.x
in your case you need to send an io.vertx.core.buffer.Buffer and not the reactive version.

it's because Java does not have reified generics it happens.

Julien

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/53c57a5f-60e7-415a-833d-21f2a9e4927a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Grey Seal

unread,
Jan 19, 2018, 4:32:35 AM1/19/18
to vert.x
For the code snippet 1 that you have shared, there seems to be no address to handle the message. netSocket.writeHandlerID() is dynamic, and event bus looks for an already registered address to send the message to.
For the code snippet 2, you can send String/JsonObject as messages, so you have to get String/JsonObject from the buffer -> Buffer.buffer("4TEST").toString() as an example.

For more look at Vertx core doc and Vertx samples.

murtuza chhil

unread,
Jan 19, 2018, 5:08:48 AM1/19/18
to vert.x
Thanks Julien,  
Using the correct Buffer import fixed it.

-chhil

murtuza chhil

unread,
Jan 19, 2018, 5:17:21 AM1/19/18
to vert.x
Thank you Grey Seal for the response.

For the code snippet 1 that you have shared, there seems to be no address to handle the message. netSocket.writeHandlerID() is dynamic, and event bus looks for an already registered address to send the message to.


There is a connection and data has come over the socket, so I assume, the value is already registered by vertx internals.

For the code snippet 2, you can send String/JsonObject as messages, so you have to get String/JsonObject from the buffer -> Buffer.buffer(“4TEST”).toString() as an example.



I was using the reactive Buffer  instead of the non reactive one as Julien pointed out earlier. The  Buffer.buffer.tostring() does not work as the socket expects a Buffer.
When I do that ....
SEVERE: Unhandled exception
java.lang.ClassCastException: java.lang.String cannot be cast to io.vertx.core.buffer.Buffer
    at io.vertx.core.net.impl.NetSocketImpl.lambda$new$1(NetSocketImpl.java:95)
    at io.vertx.core.eventbus.impl.HandlerRegistration.deliver(HandlerRegistration.java:212)
    at io.vertx.core.eventbus.impl.HandlerRegistration.handle(HandlerRegistration.java:189)
    at io.vertx.core.eventbus.impl.EventBusImpl.lambda$deliverToHandler$3(EventBusImpl.java:538)
    at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:344)
Thank you.
-chhil

murtuza chhil

unread,
Jan 19, 2018, 5:19:13 AM1/19/18
to vert.x


Do these writeHandlerIDs work for verticles across jvms and across machines boundaries in a cluster?


-chhil

Julien Viet

unread,
Jan 19, 2018, 9:15:12 AM1/19/18
to vert.x
they are local only.


On 19 Jan 2018, at 11:19, murtuza chhil <chi...@gmail.com> wrote:



Do these writeHandlerIDs work for verticles across jvms and across machines boundaries in a cluster?


-chhil

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.

murtuza chhil

unread,
Jan 19, 2018, 9:27:03 AM1/19/18
to vert.x
Thank you, Julien.

-chhil
Reply all
Reply to author
Forward
0 new messages