6
votes

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

  • 1

    1 Answers

    8
    votes

    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:

    • point-to-point style messaging
    • publish/subscribe style messaging
    • Synchronous message delivery
    • Asynchronous message delivery

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

    • An XMS application
    • A WebSphere MQ JMS application
    • A native WebSphere MQ application
    • A JMS application that is using the WebSphere default messaging provider

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

    • A WebSphere MQ queue manager - The application can connect in either bindings or client mode.
    • A WebSphere service integration bus - The application can use a direct TCP/IP connection, or it can use HTTP over TCP/IP.
    • A broker of WebSphere Event Broker or WebSphere Message Broker - Messages are transported between the application and the broker using WebSphere MQ Real-Time Transport and, depending on the configuration, messages can be delivered to the application using WebSphere MQ Multicast Transport.

    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.