0

我有一个用于获取 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);

该错误表明我在我见过的 java 中使用 getBodyLength()

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

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

4

1 回答 1

0

希望这个示例对您有所帮助。消息上有 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();
}
于 2021-10-01T15:41:19.523 回答