【问题标题】:IBM MQ .net XMS error when reading byte messages, byte length errorIBM MQ .net XMS 读取字节消息时出错,字节长度错误
【发布时间】:2021-11-10 10:46:20
【问题描述】:

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

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);

错误提示我使用 getBodyLength() 在java中我见过

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

但是我如何在 C# 中做到这一点。我看到它的 GetBodyLength 不可用?

【问题讨论】:

  • 如果传入消息包含 UTF-8 编码数据,则必须使用 ReadUTF 方法。如果消息正文是使用 WriteUTF 方法写入的,通常使用 ReadUTF。如果数据不是 UTF-8 编码的,您应该考虑使用其他 ReadBytes、ReadInt 等方法从 IBytesMessage 读取数据。
  • 嗨@Sashi 我试过 ReadInt() 来获取 bodylength 但它不正确。有没有办法获取字节长度

标签: c# byte websphere mq xms


【解决方案1】:

希望此示例对您有所帮助。消息上有 BodyLength 属性,可用于确定正文长度。我在下面的示例中使用了相同的方法。你真的需要分配字节数组吗?如果您知道传入字节消息的格式,那么您可以简单地使用 IBytesMessage 类的不同 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();
}

【讨论】:

    猜你喜欢
    • 2018-02-20
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多