C# Apache.NMS MessageListener OnMessage not firing

4.1k Views Asked by At

I have following code to connect to an ActiveMQ server. The connection works, the consumer is visible on the AMQ web interface, there are messages on the queue but the OnMessage is not executed.

I tried moving the start call but that doesn't help. TestConnection shows client id & started : true

The number of messages in the queue slowly decreases and according to the web interface it's my consumer that is doing this.

web interface showing dequeues

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Apache.NMS;

namespace MQLed
{
    class amqclientns
    {
        public String UriString = "activemq:tcp://hostname:61616";
        public string UserName = "";
        public string Password = "";
        private IConnection connection;
        private ISession session;
        private IDestination destination;
        private IMessageConsumer consumer;

        public ILogger Logger = null;

        public void TestLog(string Message) 
        {
            if (Logger != null) Logger.WriteLine(Message);
        }

        public void Connect()
        {
            try
            {
                IConnectionFactory factory = new NMSConnectionFactory(new Uri(UriString));
                connection = factory.CreateConnection(UserName, Password);
                connection.ExceptionListener += new ExceptionListener(OnException);
                session = connection.CreateSession();
                destination = session.GetDestination("queue://" + "mqled");
                consumer = session.CreateConsumer(destination);
                // connection.Start();
                consumer.Listener += new MessageListener(OnMessage);
                connection.Start();

                // OnMessage(consumer.ReceiveNoWait());
                if (Logger != null) Logger.WriteLine("Listening on " + destination.ToString());
            }
            catch (Exception ex)
            {
                if (Logger != null) Logger.WriteLine(ex.Message);
            }
        }

        public void TestConnection()
        {
            if (Logger != null) {
                Logger.WriteLine("TestConnection");
                Logger.WriteLine("Client id: " + connection.ClientId);
                Logger.WriteLine("Connection started: " + connection.IsStarted);
                Logger.WriteLine("Connection metadata: " + connection.MetaData);

                Logger.WriteLine("Consumer: " + consumer.ToString());
            }

        }

        public void Disconnect()
        {
            connection.Close();
        }

        public void OnException(Exception e)
        {
            Logger.Log(e.Message, "Exception");
        }

        public void OnMessage(IMessage message)
        {
            Logger.WriteLine("OnMessage " + (message != null).ToString());
            try
            {
                if (Logger != null) Logger.WriteLine("Message received");
                ITextMessage msg = (ITextMessage)message;
                message.Acknowledge();
                if (Logger != null) Logger.WriteLine(msg.Text);
            }
            catch (Exception ex)
            {
                if (Logger != null) Logger.WriteLine(ex.Message);
            }
        }
    }
}
1

There are 1 best solutions below

0
On

I'm answering my own question here, for future reference and to help other who make the same mistake. Thanks to Tim Bish for pointing me into right direction.

There is nothing wrong with the code shown here, the problem is that the OnMethod executes in another thread than the WinForms control that shows the logging information. As exceptions were supposed to be shown by the same mechanism none of the errors were visible.

The solution for the cross-thread operation can be found here https://stackoverflow.com/a/925067/1817610