When i read message from solace queue using a consumer i get some issues while casting it

akg17
akg17 Member Posts: 76

java.lang.ClassCastException: class com.solacesystems.jcsmp.impl.BytesMessageImpl cannot be cast to class com.solacesystems.jcsmp.TextMessage (com.solacesystems.jcsmp.impl.BytesMessageImpl and com.solacesystems.jcsmp.TextMessage are in unnamed module of loader 'app')
What i get it is SolaceBytesMessage, cant not case to TextMessage, Cant cast To Message, because message.getBody() wont work solace has not implemented it and i cant provide class name arguments.
I need to no how do we get the actual message from this bytes message ?

Comments

  • as63010
    as63010 Member Posts: 14
    edited September 2021 #2

    Hi Team,

    We also get the same issue when spring cloud stream reply the message to non spring cloud stream service.

    com.solacesystems.jcsmp.impl.BytesMessageImpl cannot be cast to com.solacesystems.jcsmp.TextMessage"

    Code:

    textMessage.setText(((TextMessage)replyMessage).getText());

    Do we need to type cast to BytesMessage and use getData API and convert to String?

  • RobO
    RobO Member, Employee Posts: 19 Solace Employee
    edited September 2021 #3

    You will not be able to cast from BytesMessage to TextMessage. You will need to grab the bytes from the payload and convert to string.

    I would do something like the following:

    if (replyMessage instanceof TextMessage) {
    textMessage.setText(((TextMessage)replyMessage).getText());
    } else if (replyMessage instanceof BytesMessage) {
    textMessage.setText(new String(((BytesMessage)replyMessage).getData()));
    } else ...

    Haven't tested the above so I will put that caveat in. Plus there is likely a more efficient way of doing this, but this is the simplest.