2

I'm trying to subscribe to an ActiveMQ topic from a .NET application and here below is my code (I use Apache NMS 1.7.0):

using Apache.NMS;
using Apache.NMS.ActiveMQ;
...

public void Start()
{
    try {
        // connect to ActiveMQ
        long timestamp = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
        string providerUrl = String.Format("tcp://{0}:{1}", Host, Port);
        IConnectionFactory connectionFactory = new ConnectionFactory(providerUrl);

        connection = connectionFactory.CreateConnection(); // fails here
        connection.ClientId = String.Format("{0}/{1}/{2}", AssemblyInfo.Title, Username, timestamp);
        connection.Start();

        // create consumer and register callback for incoming messages
        ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
        IMessageConsumer messageConsumer = session.CreateConsumer(
            new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(TopicName), MessageSelector, false
        );

        messageConsumer.Listener += new MessageListener(OnMessage);
    } catch (Exception e) {
       _.Logger.Error(Resources.ErrorStartingConsumer, e.Message);
    }
}

public void OnMessage(IMessage message)
{
    if (!(message is ITextMessage)) {
        // 'message' is not a text message
        _.Logger.Warn(Resources.SkippedMessage, message.NMSMessageId);
    } else {
        string textMessage = (message as ITextMessage).Text;
        foreach (string acceptedProtocol in AcceptedProtocols) {
            if (textMessage.StartsWith(acceptedProtocol + ":")) {
                // process message here
                return;
            }
        }

        // the url contained in 'message' is not supported
        _.Logger.Warn(Resources.SkippedMessage, message.NMSMessageId);
    }
}

The code above compiles successfully... but the attempt to connect to the ActiveMQ server fails. Here below is the statement that fails...

connection = connectionFactory.CreateConnection(); 

... and here is the error message:

Channel was inactive for too long: tcp://172.16.126.194:61615

I'm a bit confused since I get this error even if it's the first time my client attempts to connect the ActiveMQ server.

I've also tried to set wireformat.maxinactivityduration=0, but no way. Any help would be really appreciated.

j3d
  • 9,492
  • 22
  • 88
  • 172
  • Sounds like you are not connecting to a broker, are you sure there is a live broker on that port and not something else. – Tim Bish Apr 11 '16 at 22:06
  • Yes, `telnet 172.16.126.194 61615` works as expected. Furthermore, with a Java Producer I'm able to add content to the topic. – j3d Apr 12 '16 at 04:31
  • What is the broker side configuration for that port, i.e. the TransportConnector. Really not enough to go on here to help – Tim Bish Apr 12 '16 at 14:34

2 Answers2

2

In my case, I was using the wrong port number to access the broker. I had to use the secure port 61616.

str8ball
  • 111
  • 1
  • 12
1

In our case, port 61615 is a secure port... so I just had to modify the connection string like this:

    string providerUrl = String.Format("ssl://{0}:{1}", Host, Port);

After this small modification everything worked as expected.

j3d
  • 9,492
  • 22
  • 88
  • 172