I have a C# client for fetching IBM mq messages. I am trying to read byte messages from the queue. But I receive the following error.
IBM.XMS.MessageEOFException: CWSMQ0136I: An attempt was made to read beyond the end of the message. An attempt was made to read beyond the end of the message. This may be a normal condition if an application has been coded to read variable length data using the JMS 1.0.2 specification. If necessary, recode the application to use the new getBodyLength method. at IBM.XMS.Client.Impl.XmsBytesMessageImpl.ReadUTF()
I tried with the following code in C#,
var msg = (IBytesMessage)message;
var result = msg.ReadUTF();
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(result);
The error suggests I use getBodyLength() in java I have seen
byte[] uploadPayload = null;
BytesMessage bytesMessage = (BytesMessage) receivedMessage;
uploadPayload = new byte[(int) bytesMessage.getBodyLength()];
bytesMessage.readBytes(uploadPayload);
But how do I do that in C#. I see that it's the GetBodyLength is not available?
Hope this sample helps you. There is BodyLength property on the message that can be used to determine the body length. I have used the same here in the example below. Do you really need to allocate the byte array? If you know the format of the incoming bytes message, then you could simply use the different Read methods of IBytesMessage class to read the data. For example use ReadInt to read the Integer data, then if you have a byte, then call ReadByte etc.
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();
}
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.