How to get information about message retrieved from queue

977 views Asked by At

I'm new in IBM MQ. Using the following code I can easily put a message in a queue and get that message.

public void QueuePut()
{
        queue = queueManager.AccessQueue("Q1", MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
        MQMessage message = new MQMessage();
        message.WriteString("stackoverflow");

        MQPutMessageOptions putMessageOptions = new MQPutMessageOptions(); 
        putMessageOptions.Options += MQC.MQPMO_ASYNC_RESPONSE;

        queue.Put(message, putMessageOptions);
}


public void QueueGet()
{

        queue = queueManager.AccessQueue("Q2", MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
        MQMessage gotMessage = new MQMessage();

        queue.Get(gotMessage);

        string str = message.ReadString(gotMessage.MessageLength);
}

You can easily see I'm writing a message to 'Q1' and reading it from 'Q2' since Q1 is alias queue

Now, the thing I want is to get information about the message that I got in the QueueGet function. What I want to know is that gotMessage comes from 'Q1' even If I'm reading it in 'Q2'.

3

There are 3 answers

1
Consult Yarla On

Here is the related MQ property from the IBM documentation:

MQ_Property_From_IBM_Documentation

I'm unable to test this because I don't have all the required components to test this but I believe, this should work:

string queueName = "Q2";
queue = queueManager.AccessQueue(queueName, MQC.MQOO_OUTPUT 
                                       + MQC.MQOO_INQUIRE
                                       + MQC.MQCA_BASE_Q_NAME
                                       + MQC.MQOO_FAIL_IF_QUIESCING);

Console.WriteLine("QueueName=" + queueName 
                  + " BaseQueueName=" + mqQueue.BaseQueueName);

if (queueName.Equals(mqQueue.BaseQueueName))
   Console.WriteLine("Message is coming from a different underlying queue");
2
Shashi On

The BaseQueueName will point to the real queue an alias queue refers to. In this case the queue being opened to get queues is the real queue itself. Hence BaseQueueName will not point to anything.

It's not right to use MQC.MQCA_BASE_Q_NAME while opening a queue as it's not a queue open option. All queue open options begin with MQOO_.

You can use PCF classes to query an alias queue and find it's base queue name. But at the moment I am unaware if there is a way to find the alias(es) of a base queue.

Edit:

Alias queue is not really a queue like the Local queue. As the name suggests, it's another name for a local queue. It will not hold any messages. When an application opens an Alias queue, the queue manager resolves it to actual queue.

Aliasing helps

1) To hide queue/topic it is pointing to. This way applications are unaffected by any change to queue/topic.

2) Provide different level of authority to applications. One application can put but not get while another application can get but not put to the same queue.

2
Morag Hughson On

Upon return from an MQGET, the MQGMO structure has a field which tells you the name of the local queue the message was retrieved from, i.e. the base queue, even if you got it from an alias.

Read about MQGMO field ResolvedQName here

The above is the C procedural MQ API, to translate this into the OO classes you are using, this means you need to use queue.Get with two parameters, the second one being an instance of the MQGetMessageOptions.

See "Using .NET > Handling Messages"

You should then be able to access the ResolvedQName field in the MQGetMessageOptions.