Message and exception listeners in .NET

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

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

Message listeners in .NET

To receive messages asynchronously, you must complete the following steps:

  1. Define a method that 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 the following:
    
    MessageListener OnMsgMethod = new MessageListener(SomeMethodName)
    
  3. Register the delegate with one or more consumers by setting it to the MessageListener property of the consumer:
    
    consumer.MessageListener = OnMsgMethod;
    

    You can remove the delegate by setting the MessageListener back to null:

    
    consumer.MessageListener = null;
    

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 ;  
    

    You can remove the delegate by resetting the ExceptionListener to:

    
    null:  connection.ExceptionListener = null;
    

When no references to them remain, exceptions or messages are deleted automatically by the systems garbage collector.

The following is a sample code:

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);
    }
}