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.
To receive messages asynchronously you must:
public delegate void MessageListener(IMessage msg);
and so you could define the method as:
void SomeMethodName(IMessage msg);
MessageListener OnMsgMethod = new MessageListener(SomeMethodName)
consumer.MessageListener = OnMsgMethod;
Remove the delegate by setting the MessageListener back to null:
consumer.MessageListener = null;
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++.
public delegate void ExceptionListener(Exception ex);
and so the method defined could be:
void SomeMethodName(Exception ex);
ExceptionListener OnExMethod = new ExceptionListener(SomeMethodName)
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); } }