tags:

views:

316

answers:

4

'm trying to do simple pub/sub with ActiveMq. I can get it all working fine, but the subscriber disconnects after about 30 seconds. I've looked for a timeout type of value I can change but nothing seems to be working. Here is the subscriber:

using System;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Apache.NMS.ActiveMQ.Commands;

namespace ActiveMQCatcher
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");

            using (IConnection connection = factory.CreateConnection())
            {
                connection.ClientId = "MYID";
                connection.Start();

                using (ISession session = connection.CreateSession())
                {
                    IMessageConsumer consumer = session.CreateConsumer(new ActiveMQTopic("MYTOPIC"), null, false);
                    consumer.Listener += consumer_Listener;

                    Console.ReadLine();
                }

                connection.Stop();
            }
        }

        private static void consumer_Listener(IMessage message)
        {
            Console.WriteLine("Got: " + ((ITextMessage) message).Text);
        }
    }
}

I tried this:

connection.RequestTimeout = TimeSpan.MaxValue;

But it didn't seem to change anything.

To get the problem just run the program and sit waiting for about 30 seconds. You can see the connection disappear in the ActiveMQ Console (http://localhost:8161/admin/connections.jsp by default)

Any ideas?

A: 

Of course I figure it out just a few minutes after posting the question. Here is the answer for anyone else looking:

The problem is that NMS is using OpenWire, and OpenWire by default has a 30 second timeout. You can change this in the \conf\ActiveMq.xml file. Here is what you need to change:

<transportConnectors>
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?wireFormat.maxInactivityDuration=0"/>
</transportConnectors>

That wireFormat.maxInactivityDuration parameter is the key.

Kelly
A: 

WARNING! It appears that if you set maxInactivityDuration=0 then the socket never dies. Even if you call Close and Dispose on your IConnection , the underlying connection and the thread it is running on still remain. Depending on your implementation, this could could mean a memory leak.

Peter Goras
Good catch. For now I've had to implement it like this knowing that there is the potential of a leak. Still haven't found a better way.
Kelly
A: 

Not sure if it's an answer or a more of a question (or two ;),

but in our NMS use, we specify wireFormat.MaxInactivityDuration=-1 on the client side in connection url.

Seems to have same effect, but should we use "-1" or "0"... ?? Wonder, what's the difference...

Also, interestingly enough, somehow we don't specify anything on server config, but all our JAVA app connections seems to stay conneccted regardless (is it because JAVA client uses a different default for OpenWire config.params or smtng?)

Oleg Kiorsak
ok, I found out that "-1" is same as "0"http://activemq.apache.org/configuring-wire-formats.html
Oleg Kiorsak
A: 

Sounds like you are using an older version of NMS, try updating to the latest release (1.3.0) and this problem should go away.

Tim Fusesource.com

Tim Bish