6

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

  • T.Rob
    • 31,522
    • 9
    • 59
    • 103
    Guy
    • 915
    • 2
    • 14
    • 27

    1 Answers1

    8

    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.

    T.Rob
    • 31,522
    • 9
    • 59
    • 103
    • Thanks for your great responsiveness. I don't have the samples :-( .In the code above you have open the Topic as MQC.MQTOPIC_OPEN_AS_SUBSCRIPTION. does it means that it is valid for publishing as well (obviously yes, but what if vice-versa)? Where I get the subscription name If I want to access it later or because it was opened as non-durable subscription it cannot be done? If I'm performing AccessTopic for each of the steps I mentioned (publish/receive...) how can I bind with the subscription name on each receive steps? Thanks. – Guy Dec 23 '10 at 15:12
    • Please see edits above. The different sections in the sample code should answer some of these questions. If you check responses to your "JMS interfaces and implementations" you will see I've responded to your issue with durable/non-durable subscriptions. Note that subscriptions must specify a topic during the open but publications can specify topics on-the-fly. Compare this to postal delivery - the post box in front of your house serves any outbound address but receives only mail addressed to it. A producer can specify any topic but a subscriber must specify a topic, even if only a wildcard. – T.Rob Dec 23 '10 at 16:13
    • @T.Rob In Pub/Sub pattern I would have expect for some callback function to be called for every new message Why the code example doesn't contain it? it's look like P2P. – Yair Nevet May 07 '14 at 12:59
    • @YairNevet, the product has several samples and supports callback. This sample isn't intended to demonstrate every possible pattern but does illustrate OP's specific need. Please see [The Asynchronous consumption sample program](http://iopt.us/1oq4uLi). – T.Rob May 07 '14 at 14:09