简体   繁体   中英

The SOAP requests I am sending to the queue are becoming malformed on IBM MQ

I've created a utility in Java which picks up XML files, sends them over to a queue on IBM MQ. When I go into IBM MQ Explorer, the message is showing as received, but there is an ASCII character in front if it (as shown in the "Message Data" field in the image below) which is resulting in it not being recognized as a properly formatted SOAP message which the queue can process. I have tried using XML editors to make sure that my XML files don't have any non-white space characters, but that hasn't resolved the issue.

在此处输入图片说明

Here is my code which I am using to put the file on the queue:

 * sending message to MQ
 * 
 * @param file
 * @return messageId
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
private byte[] sendMessageToMQ(File file) throws UnsupportedEncodingException, IOException {

    int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
    try {
        defaultLocalQueue = qManager.accessQueue(queueName, openOptions);

        MQMessage putMessage = new MQMessage();

        String msg = readFile(file);
        putMessage.writeUTF(msg);

        // specify the message options...
        MQPutMessageOptions pmo = new MQPutMessageOptions();
        // accept
        // put the message on the queue
        defaultLocalQueue.put(putMessage, pmo);

        System.out.println("Message is put on MQ.");

        return putMessage.messageId;

    } catch (MQException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

writeUTF is documented as prefixing the data with the length

https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_9.0.0/com.ibm.mq.dev.doc/q030840_.htm

Note: The writeUTF() method of MQMessage automatically encodes the length of the string as well as the Unicode bytes it contains. When your message will be read by another Java program (using readUTF()), this is the simplest way to send string information.

You can set the characterSet of the MQMessage to 1208 (or whatever the ccsid of the message is currently according to the explorer), then use the writeString method

https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000000023465

If you need UTF-8 text in your message, but don't require the two byte length field, set the characterSet field to 1208 (which is the CCSID for UTF-8) and use writeString().

Note although this article talks about .net, but the same is true for Java: http://www-01.ibm.com/support/docview.wss?uid=swg21267940

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM