WebSphere Message Service Clients for C/C++ and .NET, Version 1.2 Operating Systems: Linux, Windows

Using message and exception listeners in .NET

A .NET application uses a message listener to receive messages asynchronously, and uses an exception listener to be notified asynchronously of a problem with a connection.

The functionality of both the message and exception listeners is exactly the same for .NET as it is for C++. However there are some small implementation differences.

Using message listeners in .NET

To receive messages asynchronously you must:

  1. Define a method which matches the signature of the message listener delegate. The method that you define can be either a static or an instance method and can be defined in any accessible class. The delegate signature is as follows:
    public delegate void MessageListener(IMessage msg); 

    and so you could define the method as:

    void SomeMethodName(IMessage msg);
  2. Instantiate this method as a delegate using something similar to:
    MessageListener OnMsgMethod = new MessageListener(SomeMethodName)
  3. Register the delegate with one or more consumers by setting it to the MessageListener property of the consumer as follows:
    consumer.MessageListener = OnMsgMethod;

    Remove the delegate by setting the MessageListener back to null:

    consumer.MessageListener = null;

Using exception listeners in .NET

The exception listener works in much the same way as the message listener, but has a different delegate definition and is assigned to the connection rather then the message consumer. This is the same as for C++.

  1. Define the method. The delegate signature is as follows:
    public delegate void ExceptionListener(Exception ex);  

    and so the method defined could be:

    void SomeMethodName(Exception ex);
  2. Instantiate this method as a delegate using something similar to:
    ExceptionListener OnExMethod = new ExceptionListener(SomeMethodName)
  3. Register the delegate with the connection by setting its ExceptionListener property:
    connection.ExceptionListener = OnExMethod ;  

    and remove the delegate by setting the ExceptionListener back to:

    null:  connection.ExceptionListener = null;

It is not necessary to delete exceptions or messages as this is taken care of automatically by the garbage collector when no references to them remain.

A sample code for using the above would be:

using System;
using System.Threading;
using IBM.XMS;

public class Sample
{
    public static void Main()
    {
        XMSFactoryFactory factoryFactory = XMSFactoryFactory.GetInstance(XMSC.CT_RTT);

        IConnectionFactory connectionFactory = factoryFactory.CreateConnectionFactory();
        connectionFactory.SetStringProperty(XMSC.RTT_HOST_NAME, "localhost");
        connectionFactory.SetStringProperty(XMSC.RTT_PORT, "1506");

        //
        // Create the connection and register an exception listener
        //

        IConnection connection = connectionFactory.CreateConnection();
        connection.ExceptionListener = new ExceptionListener(Sample.OnException);

        ISession session = connection.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
        IDestination topic = session.CreateTopic("topic://xms/sample");

        //
        // Create the consumer and register an async message listener
        //

        IMessageConsumer consumer = session.CreateConsumer(topic);
        consumer.MessageListener = new MessageListener(Sample.OnMessage);

        connection.Start();

        while (true) 
        {
            Console.WriteLine("Waiting for messages....");
            Thread.Sleep(1000);
        }
    }

    static void OnMessage(IMessage msg)
    {
        Console.WriteLine(msg);
    }

    static void OnException(Exception ex)
    {
        Console.WriteLine(ex);
    }
}

Concept topic

Terms of Use | Rate this page

Last updated: 7 Dec 2005

© Copyright IBM Corporation 2005. All Rights Reserved.