2

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);
            }
        }
    }
}
carl verbiest
  • 1,113
  • 1
  • 15
  • 30
  • Are the messages sent to the Queue with a TTL value set? – Tim Bish Aug 02 '14 at 21:18
  • @Tim bish, currently just testing with default messages sent through the ActiveMQ web interface (apache-activemq-5.10). maybe TTL is causing the messages to disappear but I wouldn't expect them to be shown as dequeued by my consumer. – carl verbiest Aug 03 '14 at 06:17
  • 1
    If the clock on the consumer is not in sync with the producing client then the messages will arrive at the consumer and be detected as expired which would account for them being dequeued by it. In any event that's the best guess until you debug more and provide more details. – Tim Bish Aug 03 '14 at 11:01
  • @Tim bish, Thanks for the tips. There was a 3 minute clock difference between server & client. WireShark showed that messages were sent. Debugging shows the OnMessage is called but somehow my logging doesn't show them. – carl verbiest Aug 03 '14 at 15:12
  • It seems to be a multi thread issue {"Cross-thread operation not valid: Control 'LogView' accessed from a thread other than the thread it was created on."} System.Exception {System.InvalidOperationException} – carl verbiest Aug 03 '14 at 15:45
  • to set the time on a windows box within a domain, use net time /domain:domainname /set – SMerrill8 Oct 16 '18 at 05:26

1 Answers1

3

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

Community
  • 1
  • 1
carl verbiest
  • 1,113
  • 1
  • 15
  • 30