Solace Community is getting a facelift!

On March 3rd we will be starting the process of migrating Solace Community to a new platform. As a result, Solace Community will go in to a temporary read-only state. You will still be able to come onto Solace Community and search through posts to find answers, but you won't be able to ask questions, post comments, or react in any way.

We hope to have the migration complete by Wednesday March 5th (or sooner), so please keep an eye out!

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.

This Month's Leaders

This Week's Leaders