I'm trying to send and receive messages with Qpid JMS and ProtonJ (AMQP 1.0) and cannot receive a TextMessage
. The exception says that it cannot cast a JmsBytesMessage
to a TextMessage
:
org.apache.qpid.jms.message.JmsBytesMessage cannot be cast to javax.jms.TextMessage
Even trying to receive the message as a JmsTextMessage
leads to a:
org.apache.qpid.jms.message.JmsBytesMessage cannot be cast to org.apache.qpid.jms.message.JmsTextMessage
I'm sending a message as follows:
TextMessage textMessage = session.createTextMessage(message)
Queue queue = session.createQueue(queueName);
MessageProducer producer = session.createProducer(queue);
producer.send(textMessage);
I try to receive the message with:
Queue queue = session.createQueue(queueName);
MessageConsumer consumer = session.createConsumer(queue);
connection.start();
TextMessage message = (TextMessage) consumer.receive();
As this did not work I tried to get the Bytes and use the toString method:
JmsBytesMessage message = (JmsBytesMessage) consumer.receive();
String stringMessage = message.toString();
with the following result:
JmsBytesMessage { org.apache.qpid.jms.provider.amqp.message.AmqpJmsBytesMessageFacade@10664b39 }
With the next test I just used the bytes and turned them into a String:
JmsBytesMessage message = (JmsBytesMessage) consumer.receive();
message.readUTF();
byte[] byteData = null;
byteData = new byte[(int) message.getBodyLength()];
message.readBytes(byteData);
message.reset();
String messageString = new String(byteData);
The result is Sw�testmessage
. I'm wondering what that prefix is as I haven't sent it. I guess it must be some encoding stuff.
How do I receive the message as a String?
QPid JMS will return a TextMessage based variant of the incoming message based on the manner of encoding and annotations that are used to describe the inbound message. If you are receiving a BytesMessage then it implies that the message being delivered it not annotated as being a TextMessage or it is not carrying a content type indicating a string is encoded in the body bytes or it is not a body section that is an AmqpValue type which carries an encoded String.
Your intermediary may be re-encoding the message incorrectly or you have other messages queued that are not encoded properly.