2

I have a requirement to write a message to the MQ and if it fails due to any reason, I have to write that message into the text file. I have to keep on adding the messages to this text file until the messages starts flowing to MQ again. Once the messages starts flowing, I have to check the text file and load all the pending messages from it and delete them from the text file.

try{
    sender.send(mqMessage); where mqMessage is of JMSBytesMessage
}

now if the sending of message failed, I have to write this into a text file

catch(JMSException mqe)
       {
           mqe.printStackTrace();
           try{


                String msgString= mqMessage.toString();
                FileWriter fstream = new FileWriter(filePath);
                BufferedWriter out = new BufferedWriter(fstream);
                out.append(msgString);
                out.close();
              }
           catch (Exception e){//Catch exception if any
                  System.err.println("Error: " + e.getMessage());

                }

       }

Once the messages starts flowing I have to read the textfile and load all these message inside the 1st try block after sender.send(mqMessage)

I am reading the complete file content and after splitting the messages and storing them to a string how can I typecast that string into the JMSBytesMessage type in order to put them on MQ.

I have mentioned the complete scenario here because I wanted to tell that though I wanted to convert JMSBytesMessage to string and then again String to JMSBytesMessage but I dont want to change the actual content while doing both the type-castings.

Thanks

4

1 回答 1

3

字节消息到文本

int length = new Long(message.getBodyLength()).intValue();
byte[] b = new byte[length];
message.readBytes(b, length);
String text = new String(b, "UTF-8");

文本到字节消息

byte[] b = mytext.getBytes("UTF-8");
BytesMessage message = session.createBytesMessage();
message.writeBytes(bArray);
于 2013-02-17T19:28:22.173 回答