简体   繁体   中英

C# Apache.NMS MessageListener OnMessage not firing

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界面显示出队

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM