javajmsibm-mqmq

Is it possible to convert a JMS TextMessage to a BytesMessage?


I am currently using IBM MQ and receiving messages. The message is being sent as a javax.jms.TextMessage, and I don't have control over it. The messages being sent are quite big, and I need my application to run within a certain amount of memory. Therefore, I don't want to load the entire message body into memory. Is there anyway to convert/accept the message as a javax.jms.BytesMessage? My aim is to basically use an output stream and send the data to a file so I don't have to keep it in memory and save space.

@JmsListener(...)
public void receiveMessage(TextMessage m){
   String s = m.getText() // taking lot of memory
}

Solution

  • It is technically possible to convert the message, but it is almost certainly not going to help you if you do it in your memory-limited environment.

    Any conversion process on the client will involve reading the body String of the javax.jms.TextMessage and copying it to the body of the javax.jms.BytesMessage as a byte[]. However, then you will have 2 copies of the data in memory at the same time which seems to be exactly what you're trying to avoid.

    If you have another environment with access to more memory then you could write an application whose only job was to convert these messages and then send them to a new queue where your memory-limited application could then read them.