malymakar
malymakar

Reputation: 99

WebSphere MQ put message on topic

I create a small application to subscribe message from definite topic. In my test environment I have only trial version WebSphere MQ and i don't know how can I put message to the my definite topic. I can put message to queue and get it, but when i want get message from definite topic, a get a empty message.

Update
Moved question update from another answer by OP into the question.

I use sample code C:\Program Files\IBM\WebSphere MQ\Tools\dotnet\samples\cs\base\SimpleSubscribe

// mq properties
            properties = new Hashtable();
            properties.Add(MQC.HOST_NAME_PROPERTY, hostName);
            properties.Add(MQC.PORT_PROPERTY, port);
            properties.Add(MQC.CHANNEL_PROPERTY, channelName);
            #region pass
            properties.Add(MQC.USER_ID_PROPERTY, "LOGIN");
            properties.Add(MQC.PASSWORD_PROPERTY, "PASSWORD");
            #endregion
            properties.Add(MQC.MQCA_TOPIC_NAME, "News.Topic");

            if (sslKeyRepository != null)
            {
                properties.Add(MQC.SSL_CERT_STORE_PROPERTY, sslKeyRepository);
            }
            if (cipherSpec != null)
            {
                properties.Add(MQC.SSL_CIPHER_SPEC_PROPERTY, cipherSpec);
            }
            if (sslPeerName != null)
            {
                properties.Add(MQC.SSL_PEER_NAME_PROPERTY, sslPeerName);
            }
            if (keyResetCount != 0)
            {
                properties.Add(MQC.SSL_RESET_COUNT_PROPERTY, keyResetCount);
            }
            if (sslCertRevocationCheck != false)
            {
                MQEnvironment.SSLCertRevocationCheck = sslCertRevocationCheck;
            }

            if (transportMode == "managed")
                properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
            else if (transportMode == "unmanaged")
                properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);

            // create connection
            Console.Write("Connecting to queue manager.. ");
            queueManager = new MQQueueManager(queueManagerName, properties);
            Console.WriteLine("done");

            // accessing topic
            Console.Write("Accessing topic " + topicName + ".. ");
            if (durability == "nondurable")
                topic = queueManager.AccessTopic(topicName, null, MQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, MQC.MQSO_CREATE | MQC.MQSO_FAIL_IF_QUIESCING);
            else if (durability == "durable")
                topic = queueManager.AccessTopic(topicName, null, MQC.MQSO_CREATE | MQC.MQSO_FAIL_IF_QUIESCING | MQC.MQSO_DURABLE | MQC.MQSO_RESUME, null, "DurableSubscriptionName");
            Console.WriteLine("done");

            // creating a message object
            message = new MQMessage();
            message.WriteString(messageString);

            int time = 1;
            // getting messages continuously
            for (int i = 1; i <= numberOfMsgs; i++)
            {
                // creating a message object
                message = new MQMessage();

                try
                {
                    topic.Get(message);
                    Console.WriteLine("Message " + i + " got = " + message.ReadString(message.MessageLength));
                    message.ClearMessage();
                }
                catch (MQException mqe)
                {
                    if (mqe.ReasonCode == 2033)
                    {
                        ++time;
                        --i;
                        Console.WriteLine("No message available");
                        Thread.Sleep(1000);
                        //waiting for 10sec 
                        if (time > 10)
                        {
                            Console.WriteLine("Timeout : No message available");
                            break;
                        }
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message);
                    }
                }
            }

            // closing topic
            Console.Write("Closing topic.. ");
            topic.Close();
            Console.WriteLine("done");

            // disconnecting queue manager
            Console.Write("Disconnecting queue manager.. ");
            queueManager.Disconnect();
            Console.WriteLine("done");

Upvotes: 0

Views: 2102

Answers (2)

Shashi
Shashi

Reputation: 15283

While I don't see much of a problem with the code except for the below. This line neither helps nor hurt your application. The constant is actually used for MQ administrative purpose. You can take it off.

 properties.Add(MQC.MQCA_TOPIC_NAME, "News.Topic");

Do you have an application publishing on the topic specified while creating subscription with below method?

topic = queueManager.AccessTopic(topicName,...

Without someone publishing on the topic, your application will not receive any publications. Your application should have thrown an MQRC 2033 - MQRC_NO_MESSAGE_AVAILABLE exception and not return an empty message. Are you not hitting that exception? When this exception is received application will print "No message available" on the console. Are you not seeing that?

Upvotes: 1

Morag Hughson
Morag Hughson

Reputation: 7619

The Publish/Subscribe methodology is different from putting and getting from queues. In order to receive a message that has been published to a topic, the receiving application must have a subscription to that topic.

If you create a subscription first, and then receive messages from it, then when you put to the topic, your receiving application will get a copy of the message.

I would reiterate Shashi's request though, post the code you have so far so we can help further.

Upvotes: 2

Related Questions