0
votes

I need to get messages from IBM MQ queue and put them into objects. So, I'm using the following code:

public MQMessage GetMessageFromQueue(string queueName)
{
    MQMessage message = new MQMessage();
    MQGetMessageOptions gmo = new MQGetMessageOptions();

    var queueMngrForReading = new MQQueueManager(_managerName, _connectionParams);
    var queue = queueMngrForReading.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);

    queue.Get(message, gmo);
    System.Console.Out.WriteLine("Message Data: " + message.ReadString(message.MessageLength));
    return message;
}

The problem is that when I call the method in Main using the following code:

MQClient mqClient = new MQClient();
mqClient.Connect(AppSettings.MQ.Manager);
string xml11 = mqClient.GetMessageFromQueue("PRQUEUE").ToString();

I get the values from my message displayed in Console, but the object xml11 gets the value "IBM.WMQ.MQMessage#013C8E0F".

How can I put the XML message in the object xml11?

Thanks.

1
Since your GetMessageFromQueue is returning a MQMessage, you need to call ReadString method to get message body. I am assuming the sender of the message is doing a WriteString to set the message body when sending messages. Refer here: ibm.com/support/knowledgecenter/SSFKSJ_9.2.0/… - Shashi

1 Answers

0
votes

With toString you can not read the body of an MQMessage.

I would change GetMessageFromQueue like this:

public String GetMessageFromQueue(string queueName)
{
    MQMessage message = new MQMessage();
    MQGetMessageOptions gmo = new MQGetMessageOptions();

    var queueMngrForReading = new MQQueueManager(_managerName, _connectionParams);
    var queue = queueMngrForReading.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);

    queue.Get(message, gmo);
    String msgText = message.ReadString(message.MessageLength);
    System.Console.Out.WriteLine("Message Data: " + msgText)
    return msgText;
}

To simplify your code, I recommend using the XMS API, see Developing XMS .NET applications.