繁体   English   中英

IBM MQ .net 读取字节消息时出现XMS错误,字节长度错误

[英]IBM MQ .net XMS error when reading byte messages, byte length error

我有一个 C# 客户端,用于获取 IBM mq 消息。 我正在尝试从队列中读取字节消息。 但是我收到以下错误。

IBM.XMS.MessageEOFException:CWSMQ0136I:尝试读取超出消息末尾的内容。 试图读取超出消息末尾的内容。 如果已将应用程序编码为使用 JMS 1.0.2 规范读取可变长度数据,则这可能是正常情况。 如有必要,重新编码应用程序以使用新的 getBodyLength 方法。 在 IBM.XMS.Client.Impl.XmsBytesMessageImpl.ReadUTF()

我在 C# 中尝试使用以下代码,

                        var msg = (IBytesMessage)message;
                        var result = msg.ReadUTF();
                        var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(result);

该错误提示我在 java 中使用了 getBodyLength() 我已经看到

             byte[] uploadPayload = null;
             BytesMessage bytesMessage = (BytesMessage) receivedMessage; 
             uploadPayload = new byte[(int) bytesMessage.getBodyLength()];
                    bytesMessage.readBytes(uploadPayload);

但是在 C# 中我该怎么做。我看到它的 GetBodyLength 不可用?

希望这个示例对您有所帮助。 消息上有 BodyLength 属性,可用于确定正文长度。 我在下面的示例中使用了相同的方法。 你真的需要分配字节数组吗? 如果您知道传入字节消息的格式,那么您可以简单地使用 IBytesMessage class 的不同 Read 方法来读取数据。 例如使用 ReadInt 读取 Integer 数据,然后如果你有一个字节,则调用 ReadByte 等。

void ReceiveMessages()
{
    XMSFactoryFactory factoryFactory;
    IConnectionFactory cf;
    IConnection connectionWMQ;
    ISession sessionWMQ;
    IDestination destination;
    IMessageConsumer consumer;
    IBytesMessage bytesMessage;

    // Get an instance of factory.
    factoryFactory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);

    // Create WMQ Connection Factory.
    cf = factoryFactory.CreateConnectionFactory();
    Console.WriteLine("Connection Factory created.");

    // Set the properties
    cf.SetStringProperty(XMSC.WMQ_HOST_NAME, "localhost");
    cf.SetIntProperty(XMSC.WMQ_PORT, 1414);
    cf.SetStringProperty(XMSC.WMQ_CHANNEL, "SVR_CHN");
    cf.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT);
    cf.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "MYQM");
    cf.SetStringProperty(XMSC.USERID, "myuserId");
    cf.SetStringProperty(XMSC.PASSWORD, "mypassw0rd");
    // Create connection.
    connectionWMQ = cf.CreateConnection();
    Console.WriteLine("Connection created.");

    // Create session
    sessionWMQ = connectionWMQ.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
    Console.WriteLine("Session created.");

    // Create destination
    destination = sessionWMQ.CreateQueue("INBQ");
    Console.WriteLine("Destination created.");

    IMessageProducer prod = sessionWMQ.CreateProducer(destination);
    IBytesMessage sendMsg = sessionWMQ.CreateBytesMessage();
    sendMsg.WriteBytes(Encoding.ASCII.GetBytes("Hello world from WMQ"));
    prod.Send(sendMsg);
    // Create consumer
    consumer = sessionWMQ.CreateConsumer(destination);
    Console.WriteLine("Message Consumer created. Starting the connection now.");
    // Start the connection to receive messages.
    connectionWMQ.Start();

    // Wait for 30 seconds for messages. Exit if no message by then
    bytesMessage = (IBytesMessage)consumer.Receive(30000);
    if (bytesMessage != null)
    {
        Console.WriteLine("Message received.");
        byte[] uploadPayload = null;
        uploadPayload = new byte[(int)bytesMessage.BodyLength];
        bytesMessage.ReadBytes(uploadPayload);
        Console.WriteLine(bytesMessage.BodyLength + "\n");
    }
    else
        Console.WriteLine("Wait timed out.");

    // Cleanup
    consumer.Close();
    destination.Dispose();
    sessionWMQ.Dispose();
    connectionWMQ.Close();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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