Message peek in IBM MQ

3.8k views Asked by At

In MSMQ there is a functionality that lets users to peek at a message without actually consuming it. i.e. I peek at the next message in a Queue based on MessageID. If I am not interested in the message I can put the message back into the Queue (i.e. unacknowledged are added back to the Queue and the messageID is maintained).

Similar functionality also exists in RabbitMQ. However in RabbitMQ its not done in a clean way. You can simulate peeking messages by taking the message off the queue and then not sending an acknowledgement so RabbitMQ will then add that message back to the queue. However I read that RabbitMQ can reorder the messages and increment message IDs when unacknowledged messages are re-added to the queue.

Has anyone encountered this problem before.

Also does any one know if IBM MQ supports this behaviour/functionality of peek and seek?

regards D

2

There are 2 answers

1
Shashi On

IBM MQ provides a way to browse messages without removing them from queue. You can start browsing messages from the beginning and iterate through all messages in a queue. You can also browse a specific message using MessageId or CorrelationId.

Here is snippet in C# for browsing messages in a queue.

    /// <summary>
    /// Browse messages in a queue
    /// </summary>
    private void BrowseMessages()
    {
        MQQueueManager qm = null;
        MQQueue queueGet = null;
        Hashtable mqProps = null;

        try
        {
            mqProps = new Hashtable();
            // Setup properties for connection
            mqProps.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
            mqProps.Add(MQC.HOST_NAME_PROPERTY, "localhost");
            mqProps.Add(MQC.PORT_PROPERTY, 1414);
            mqProps.Add(MQC.CHANNEL_PROPERTY, "QM.SVRCONN");

            // Open connection to queue manager
            qm = new MQQueueManager("QM", mqProps);

            // Open queue for browsing
            queueGet = qm.AccessQueue("Q1", MQC.MQOO_BROWSE | MQC.MQOO_FAIL_IF_QUIESCING);

            // In a loop browse all messages till we reach end of queue
            while (true)
            {
                try
                {
                    // Need to create objects everytime
                    MQMessage msg = new MQMessage();
                    MQGetMessageOptions gmo = new MQGetMessageOptions();

                    // Use browse next option to start browsing
                    gmo.Options = MQC.MQGMO_BROWSE_NEXT;
                    queueGet.Get(msg, gmo);
                    Console.WriteLine(msg.ReadString(msg.MessageLength));
                }
                catch (MQException mqex)
                {
                    // When there are no more messages to browse, the Get call
                    // will throw MQException with reason code MQC.MQRC_NO_MSG_AVAILABLE.
                    // But here we close the queue and break out of loop for all exceptions
                    queueGet.Close();
                    break;
                }
            }
            qm.Disconnect();
        }
        catch (MQException mqex)
        {
            Console.WriteLine(mqex);
        }
    }
2
ValerieLampkin On

In IBM MQ, a way to view messages on the queue if they are not too large in size is the amqsbcg sample program (browse as Tim mentioned). You can use this to dump messages to an output file without doing a destructive get. You could then parse the file to check for the message ID or other info you need. If you found a message that matched the criteria you needed, you would then have to do a GET with those options to actually remove it from the queue.

amqsbcg QUEUENAME QMGRNAME > output.file

This sample program can be found in

AIX/Unix: $MQ_HOME/samp/bin/amqsbcg
Windows: $MQ_HOME\tools\c\Samples\Bin\amqsbcg.exe

Where $MQ_HOME is the appropriate location for your operation system. The default location for $MQ_HOME is:

AIX: /usr/mqm

Unix: /opt/mqm

Windows: C:\Program Files\IBM\Websphere MQ

Another possible option may be the "qload" MO03 support pac. It has an option to let you filter by Message Id, CorrelId Id or Group Id

http://www-01.ibm.com/support/docview.wss?acss=wmq062007&rs=171&uid=swg24009368