0

I want to convert Message type to byte[] without going through string, because I need it for performance issues.

I should not pass through string (Message to string to byte[]).

Message type:javax.jms.MessageConsumer

h.zak
  • 1,407
  • 5
  • 21
  • 40
  • 2
    this javax.mail.Message ?? or what Message ? mention it clearly – Srinath Ganesh May 02 '16 at 13:06
  • In fact, I don't know, I'm working with this code https://docs.wso2.com/display/MB300/Sending+and+Receiving+Messages+Using+Queues – h.zak May 02 '16 at 13:08
  • **1)** You need to know what are you working with **2)** If your message implements serializeable then try http://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array – Srinath Ganesh May 02 '16 at 13:13
  • I found that, the method use javax.jms.MessageConsumer, but the message doesn't implement serializeable – h.zak May 02 '16 at 13:19
  • Saw your code link, i believe its **javax.jms.TextMessage** [child of **javax.jms.Message**] – Srinath Ganesh May 02 '16 at 13:22
  • no, it's not, in the link , they convert consumer.receive() (Message) to TextMessage – h.zak May 02 '16 at 13:24
  • Decide what is the exact class you want to convert into byte[]. Come back and type the class name and exact package. till then we have no idea how to help you. packageName.subpackageName...ClassName – Srinath Ganesh May 02 '16 at 13:28
  • anyway, have you any idea how to convert javax.jms.TextMessage to byte[] ? – h.zak May 02 '16 at 13:32
  • NOT a good way but... in the line below *// create the message to send*, **Create and Send** an instance of **javax.jms.BytesMessage** http://docs.oracle.com/javaee/6/api/javax/jms/BytesMessage.html instead of TextMessage – Srinath Ganesh May 02 '16 at 13:37
  • If you have a TextMessage, you already have a String. You can't avoid it. – VGR May 02 '16 at 15:29

1 Answers1

0

If your 'Message' class implements Serializeable, then you can convert it directly into a byte[].

In Java, Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized - converted into a replica of the original object.

Here's a link to a nice tutorial on serialization in Java.

Vijay Chavda
  • 826
  • 2
  • 15
  • 34