IBMMQ:消息以二进制形式发布,但以字符串形式发送

问题描述 投票:0回答:1

我正在尝试使用java将简单消息发布到ibmmq。消息发送成功。但是当我在ibm控制台上检查队列时,消息显示为enter image description here

但是我期望它是简单的字符串。

enter image description here

这是我的代码。当我尝试转换时,我收到以下消息无法将类型为jms_bytes的消息的主体分配给java.lang.String

import com.ibm.mq.*;
import com.ibm.mq.constants.MQConstants;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;

import javax.jms.*;
import java.io.IOException;
import java.util.Hashtable;

public class PublisherTest
{
    static private String CHANNEL = "anychannel";
    static private int    PORT = 1414;
    static private String HOST = localhost;
    static private String QMANAGER = "QM1";
    static private String QUEUE = "queue.test";
    static private String USER = USER;
    static private Hashtable<String, Object> props =
            new Hashtable<String, Object>();
    static MQQueueManager qMgr = null;

    static private void putMsgOnQueue(String message) {
        // Disabling IBM cipher suite mapping due to
        // using Oracle Java and not IBM Java
        System.setProperty("com.ibm.mq.cfg.useIBMCipherMappings", "false");
        // Enabling SSL debug to view the communication
        System.setProperty("javax.net.debug", "ssl:handshake");

        props.put(MQConstants.CHANNEL_PROPERTY, CHANNEL);
        props.put(MQConstants.PORT_PROPERTY, PORT);
        props.put(MQConstants.HOST_NAME_PROPERTY, HOST);
        props.put(MQConstants.USER_ID_PROPERTY, USER);
        props.put(MQConstants.PASSWORD_PROPERTY, PASSWORD);
        props.put(MQConstants.SSL_CIPHER_SUITE_PROPERTY, "TLS_RSA_WITH_AES_256_CBC_SHA256");


        try {

            qMgr = new MQQueueManager(QMANAGER, props);


            // MQOO_OUTPUT = Open the queue to put messages
            // MQOO_INPUT_AS_Q_DEF = Using queue-defined defaults
            int openOptions = MQConstants.MQOO_OUTPUT;


            // creating destination
            MQQueue queue = qMgr.accessQueue(QUEUE, openOptions);

            // specify the message options...
            MQPutMessageOptions pmo = new MQPutMessageOptions(); // Default


            // MQPMO_ASYNC_RESPONSE = MQPUT operation is completed without the
            // application waiting for the queue manager to complete the call
            // Using this option can improve messaging performance,
            // particularly for applications using client bindings.
            pmo.options = MQConstants.MQPMO_ASYNC_RESPONSE;

            // create message
            MQMessage mqMessage = new MQMessage();


            System.out.println("Writing message to queue: " + QUEUE);
            mqMessage.writeString(message.toString());

            // Put message on queue
            queue.put(mqMessage, pmo);

            // Close queue
            queue.close();

            // Get status
            MQAsyncStatus asyncStatus = qMgr.getAsyncStatus();

            // Print status code (0 = successful)
            System.out.println(asyncStatus.reasonCode);

        } catch (MQException e) {
            System.out.println("The connection to MQ could not be established." + e.getMessage());

        } catch (IOException e) {
            System.out.println("Error while writing message." +
                    e.getMessage());
        } finally {
            try {
                qMgr.disconnect();
            } catch (MQException e) {
                System.out.println("The connection could not be closed." +
                        e.getMessage());
            }
        }
    }


    public static void main(String[] args) {

         putMsgOnQueue("WELCOME");
    }
}

将获得任何帮助。

java jms ibm-mq spring-jms mq
1个回答
1
投票

默认消息格式为MQFMT_NONE。这意味着消息主体由字节组成。您的代码未设置消息格式。所以我的想法是MQ控制台将此类消息指示为二进制。

建议您将消息格式设置为字符串并运行。这会将消息格式设置为字符串。

        MQMessage mqMessage = new MQMessage();
        mqMessage.format = MQConstants.MQFMT_STRING;
© www.soinside.com 2019 - 2024. All rights reserved.