.netibm-mqpublish-subscribemqwebsphere-7

Using Websphere MQ Topic .NET API


I read this article

And still didn't understand the notion of how topics works in MQ. In JMS I know that you can publish a message on a Topic, and in order to receive a message from it you first need to subscribe to it (using the subscription name at the receive phase).

How it is work in MQ? I want to write a simple scenario of (as in JMS):

  • 1. Publish to a topic
  • 2. Receive from a Topic - that will fail (i'm not subscribed yet)
  • 3. Subscribe
  • 4. Receive - nothing to receive since I subscribed after the publish
  • 5. Publish
  • 6. Receive - successful

    A sample code (.NET) would be helpful

    Guy


  • Solution

  • Have you looked at the included sample code? In a default installation it resides at:
    C:\Program Files\IBM\WebSphere MQ\tools\dotnet\samples

    The MQPubSubSample.cs program illustrates both durable and non-durable subscriptions for managed and unmanaged code. In each case it first subscribes, then publishes but you can easily modify it to your use case of publish/subscribe/receive/publish/receive as described in the question. (You won't be able to receive from a topic to which you have not yet subscribed, though.)

    Here's one of the sections from the sample code:

      // Managed/nondurable
      string topicName = DEFAULT_TOPIC_STRING;
      string topicObject = null;
      int openOptionsForGet = MQC.MQSO_CREATE | MQC.MQSO_FAIL_IF_QUIESCING | MQC.MQSO_MANAGED | MQC.MQSO_NON_DURABLE;
      int destType = MQC.MQOT_TOPIC;
    
      try
      { 
        destForGet = mqQMgr.AccessTopic(topicName, topicObject, MQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, openOptionsForGet);
    
        messageForPut = new MQMessage();
        messageForPut.WriteString(DEFAULT_MESSAGE_DATA);
      
        mqQMgr.Put(destType, topicObject, null, topicName, messageForPut);
    
        messageForGet = new MQMessage();
    
        destForGet.Get(messageForGet);
    
        string messageDataFromGet = messageForGet.ReadLine();
    
        if(!DEFAULT_MESSAGE_DATA.Equals(messageDataFromGet))
          Console.WriteLine("Incorrect Message Received.");
    
        destForGet.Close();
      }
      catch(MQException mqE)
      {
        Console.WriteLine("MQException caught. " + mqE.ToString());
      }
    

    Your question references JMS as an example. Please be aware that you have the option of using IBM XMS - Extensible Message Service Client for .Net. XMS implements the JMS API over .Net with WMQ, WMB or WAS as the transport so you can use JMS code with very little modification. The Message Service Client for .Net 2.0.0 manual describes the offering:


    Message Service Client for .NET
    Message Service Client for .NET provides an application programming Interface (API) called XMS that has the same set of interfaces as the Java Message Service (JMS) API. Message Service Client for .NET contains a fully managed implementation of XMS, which can be used by any .NET compliant language. XMS supports:

    An XMS application can exchange messages with the following types of application:

    An XMS application can connect to, and use the resources of, any of the following messaging servers:

    By connecting to a WebSphere MQ queue manager, an XMS application can use WebSphere MQ Enterprise Transport to communicate with a broker of WebSphere Event Broker or WebSphere Message Broker. Alternatively, an XMS application can use a WebSphere MQ Publish/Subscribe broker.


    If you wish to explore further, the sample XMS code resides in subdirectories below the native .Net samples referred to earlier. The samples are described in the Using the XMS Sample Applications section of the manual.

    The .Net support is integrated into WMQ as of v7. If you are lacking the samples then your WMQ client installation is back-level or incompletely installed. The latest v7.0 WMQ Client for Windows is downloadable as SupportPac MQC7.

    The XMS support is delivered as SupportPac IA9H.