You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by Gaurav Hariani <ga...@blackspark.com> on 2007/06/06 20:04:02 UTC

Orphan connections from .NET clients

Since James and Hiram are back on the list ... I thought I'd repost this-

We are facing a problem of orphan connections to ActiveMQ. A simple .NET 
client that only creates a connection/session and then closes and exits, 
still shows up in jconsole.
It is possible to stop it manually in jconsole.

Below is an example to reproduce the problem:


The following code creates a connection to an OpenWire transport 
connector, waits for 1 second and then closes the session/connection.
Looking at JConsole ... ActiveMQ reports the connection as open. Too 
many open connections and ActiveMQ stops processing messages.

However if the connection is not closed by the application and the 
application is killed using Ctrl-C ... then ActiveMQ closes the 
connections.

Is there something obvious that I'm doing wrong or is this a bug in the 
TcpTransport code?



using System;
using System.Threading;
using NMS;

namespace ActiveMQ {
   class TestMain {

       static void Main(string[] args) {

           Uri uri = new Uri("tcp://activemqserver:61616");

           ConnectionFactory factory = new ConnectionFactory(uri);
           IConnection connection = null;
           ISession session = null;

           try {
               connection = factory.CreateConnection();
               Console.WriteLine("Connection Created");

               connection.ClientId = "[test1] " + connection.ClientId;

               session = connection.CreateSession();
               Console.WriteLine("Session Created");

               Thread.Sleep(1000);

           } finally {
               session.Close();
               connection.Close();
               Console.WriteLine("Connection Closed");
           }
       }
   }
}

Re: Orphan connections from .NET clients

Posted by Jim_Cross <ji...@googlemail.com>.
If not, here's a quick class I hacked together to stop the inactive
connections - just give your JMX url as a command line argument. Not
guarantees provided with this code!


package com.ubs.eq.speed;

import java.util.Hashtable;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class OrphanConnectionCleaner
{
	private static String url; 
	
	public static void main(String[] args) throws Exception
	{
		if(args.length < 1)
			throw new IllegalArgumentException("URL argument must be provided");
		
		url = args[0];
		
		JMXServiceURL address = new JMXServiceURL(url);
		JMXConnector connector = JMXConnectorFactory.connect(address);
		MBeanServerConnection mbs = connector.getMBeanServerConnection();
		
		Set s = mbs.queryMBeans(new
ObjectName("org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,*"),
null);
		for(Object o : s)
		{
			if(o instanceof ObjectInstance)
			{
				ObjectInstance i = (ObjectInstance)o;
				ObjectName n  = i.getObjectName();
				Hashtable keys = n.getKeyPropertyList();
				if(keys.containsKey("Connection"))
				{
					try
					{
						Object connectionId = mbs.getAttribute(n, "ConnectionId");
						System.out.println("Not stopping connection " + connectionId + " -
connection still active");
					}
					catch (Exception ex)
					{
						try
						{
							System.out.println("Stopping connection");
							mbs.invoke(n, "stop", new Object[]{}, new String[]{});
						}
						catch(Exception ignore)
						{
							ignore.printStackTrace();
						}
					}
					
				}
					
			}
		}
	}
}




Jim_Cross wrote:
> 
> Gaurav,
> 
> Did you ever find a workaround for this? I've hit the same problem with
> NMS and ActiveMQ 4.1.1 (also seems to affect 4.1.0 but not 4.0.2), and it
> looks like it could be a show stopper for us.
> 
> Cheers,
> 
> Jim
> 
> 
> Gaurav Hariani-2 wrote:
>> 
>> yes
>> 
>> Hiram Chirino wrote:
>>> Odd. look like a bug.  You using 4.1.1?
>>>
>>>
>>> On 6/6/07, Gaurav Hariani <ga...@blackspark.com> wrote:
>>>> Since James and Hiram are back on the list ... I thought I'd repost 
>>>> this-
>>>>
>>>> We are facing a problem of orphan connections to ActiveMQ. A simple
>>>> .NET
>>>> client that only creates a connection/session and then closes and
>>>> exits,
>>>> still shows up in jconsole.
>>>> It is possible to stop it manually in jconsole.
>>>>
>>>> Below is an example to reproduce the problem:
>>>>
>>>>
>>>> The following code creates a connection to an OpenWire transport
>>>> connector, waits for 1 second and then closes the session/connection.
>>>> Looking at JConsole ... ActiveMQ reports the connection as open. Too
>>>> many open connections and ActiveMQ stops processing messages.
>>>>
>>>> However if the connection is not closed by the application and the
>>>> application is killed using Ctrl-C ... then ActiveMQ closes the
>>>> connections.
>>>>
>>>> Is there something obvious that I'm doing wrong or is this a bug in the
>>>> TcpTransport code?
>>>>
>>>>
>>>>
>>>> using System;
>>>> using System.Threading;
>>>> using NMS;
>>>>
>>>> namespace ActiveMQ {
>>>>    class TestMain {
>>>>
>>>>        static void Main(string[] args) {
>>>>
>>>>            Uri uri = new Uri("tcp://activemqserver:61616");
>>>>
>>>>            ConnectionFactory factory = new ConnectionFactory(uri);
>>>>            IConnection connection = null;
>>>>            ISession session = null;
>>>>
>>>>            try {
>>>>                connection = factory.CreateConnection();
>>>>                Console.WriteLine("Connection Created");
>>>>
>>>>                connection.ClientId = "[test1] " + connection.ClientId;
>>>>
>>>>                session = connection.CreateSession();
>>>>                Console.WriteLine("Session Created");
>>>>
>>>>                Thread.Sleep(1000);
>>>>
>>>>            } finally {
>>>>                session.Close();
>>>>                connection.Close();
>>>>                Console.WriteLine("Connection Closed");
>>>>            }
>>>>        }
>>>>    }
>>>> }
>>>>
>>>
>>>
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11229810
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Orphan connections from .NET clients

Posted by Jim_Cross <ji...@googlemail.com>.
Gaurav,

Did you ever find a workaround for this? I've hit the same problem with NMS
and ActiveMQ 4.1.1 (also seems to affect 4.1.0 but not 4.0.2), and it looks
like it could be a show stopper for us.

Cheers,

Jim


Gaurav Hariani-2 wrote:
> 
> yes
> 
> Hiram Chirino wrote:
>> Odd. look like a bug.  You using 4.1.1?
>>
>>
>> On 6/6/07, Gaurav Hariani <ga...@blackspark.com> wrote:
>>> Since James and Hiram are back on the list ... I thought I'd repost 
>>> this-
>>>
>>> We are facing a problem of orphan connections to ActiveMQ. A simple .NET
>>> client that only creates a connection/session and then closes and exits,
>>> still shows up in jconsole.
>>> It is possible to stop it manually in jconsole.
>>>
>>> Below is an example to reproduce the problem:
>>>
>>>
>>> The following code creates a connection to an OpenWire transport
>>> connector, waits for 1 second and then closes the session/connection.
>>> Looking at JConsole ... ActiveMQ reports the connection as open. Too
>>> many open connections and ActiveMQ stops processing messages.
>>>
>>> However if the connection is not closed by the application and the
>>> application is killed using Ctrl-C ... then ActiveMQ closes the
>>> connections.
>>>
>>> Is there something obvious that I'm doing wrong or is this a bug in the
>>> TcpTransport code?
>>>
>>>
>>>
>>> using System;
>>> using System.Threading;
>>> using NMS;
>>>
>>> namespace ActiveMQ {
>>>    class TestMain {
>>>
>>>        static void Main(string[] args) {
>>>
>>>            Uri uri = new Uri("tcp://activemqserver:61616");
>>>
>>>            ConnectionFactory factory = new ConnectionFactory(uri);
>>>            IConnection connection = null;
>>>            ISession session = null;
>>>
>>>            try {
>>>                connection = factory.CreateConnection();
>>>                Console.WriteLine("Connection Created");
>>>
>>>                connection.ClientId = "[test1] " + connection.ClientId;
>>>
>>>                session = connection.CreateSession();
>>>                Console.WriteLine("Session Created");
>>>
>>>                Thread.Sleep(1000);
>>>
>>>            } finally {
>>>                session.Close();
>>>                connection.Close();
>>>                Console.WriteLine("Connection Closed");
>>>            }
>>>        }
>>>    }
>>> }
>>>
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11228145
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Orphan connections from .NET clients

Posted by Gaurav Hariani <ga...@blackspark.com>.
yes

Hiram Chirino wrote:
> Odd. look like a bug.  You using 4.1.1?
>
>
> On 6/6/07, Gaurav Hariani <ga...@blackspark.com> wrote:
>> Since James and Hiram are back on the list ... I thought I'd repost 
>> this-
>>
>> We are facing a problem of orphan connections to ActiveMQ. A simple .NET
>> client that only creates a connection/session and then closes and exits,
>> still shows up in jconsole.
>> It is possible to stop it manually in jconsole.
>>
>> Below is an example to reproduce the problem:
>>
>>
>> The following code creates a connection to an OpenWire transport
>> connector, waits for 1 second and then closes the session/connection.
>> Looking at JConsole ... ActiveMQ reports the connection as open. Too
>> many open connections and ActiveMQ stops processing messages.
>>
>> However if the connection is not closed by the application and the
>> application is killed using Ctrl-C ... then ActiveMQ closes the
>> connections.
>>
>> Is there something obvious that I'm doing wrong or is this a bug in the
>> TcpTransport code?
>>
>>
>>
>> using System;
>> using System.Threading;
>> using NMS;
>>
>> namespace ActiveMQ {
>>    class TestMain {
>>
>>        static void Main(string[] args) {
>>
>>            Uri uri = new Uri("tcp://activemqserver:61616");
>>
>>            ConnectionFactory factory = new ConnectionFactory(uri);
>>            IConnection connection = null;
>>            ISession session = null;
>>
>>            try {
>>                connection = factory.CreateConnection();
>>                Console.WriteLine("Connection Created");
>>
>>                connection.ClientId = "[test1] " + connection.ClientId;
>>
>>                session = connection.CreateSession();
>>                Console.WriteLine("Session Created");
>>
>>                Thread.Sleep(1000);
>>
>>            } finally {
>>                session.Close();
>>                connection.Close();
>>                Console.WriteLine("Connection Closed");
>>            }
>>        }
>>    }
>> }
>>
>
>

Re: Orphan connections from .NET clients

Posted by Hiram Chirino <hi...@hiramchirino.com>.
Odd. look like a bug.  You using 4.1.1?


On 6/6/07, Gaurav Hariani <ga...@blackspark.com> wrote:
> Since James and Hiram are back on the list ... I thought I'd repost this-
>
> We are facing a problem of orphan connections to ActiveMQ. A simple .NET
> client that only creates a connection/session and then closes and exits,
> still shows up in jconsole.
> It is possible to stop it manually in jconsole.
>
> Below is an example to reproduce the problem:
>
>
> The following code creates a connection to an OpenWire transport
> connector, waits for 1 second and then closes the session/connection.
> Looking at JConsole ... ActiveMQ reports the connection as open. Too
> many open connections and ActiveMQ stops processing messages.
>
> However if the connection is not closed by the application and the
> application is killed using Ctrl-C ... then ActiveMQ closes the
> connections.
>
> Is there something obvious that I'm doing wrong or is this a bug in the
> TcpTransport code?
>
>
>
> using System;
> using System.Threading;
> using NMS;
>
> namespace ActiveMQ {
>    class TestMain {
>
>        static void Main(string[] args) {
>
>            Uri uri = new Uri("tcp://activemqserver:61616");
>
>            ConnectionFactory factory = new ConnectionFactory(uri);
>            IConnection connection = null;
>            ISession session = null;
>
>            try {
>                connection = factory.CreateConnection();
>                Console.WriteLine("Connection Created");
>
>                connection.ClientId = "[test1] " + connection.ClientId;
>
>                session = connection.CreateSession();
>                Console.WriteLine("Session Created");
>
>                Thread.Sleep(1000);
>
>            } finally {
>                session.Close();
>                connection.Close();
>                Console.WriteLine("Connection Closed");
>            }
>        }
>    }
> }
>


-- 
Regards,
Hiram

Blog: http://hiramchirino.com

Re: Orphan connections from .NET clients

Posted by Jim_Cross <ji...@googlemail.com>.
Hmm, very odd - taking out connection.Stop() and connection.Close() still
doesn't fix it for me.
Also connection.Dispose() just calls Close() anyway, so I'm not sure why
that fixed it for you either...


Dris wrote:
> 
> Not really. Actually on looking at it I am closing and disposing of
> sessions and consumers, and just disposing of producers and connections so
> I don't have your connection.Stop and connection.Close in mine. I would be
> surprised if that made a diff though. I had the issue until I added the
> connection.Dispose() and then it went away. I think I assumed that
> disposing of the session would stop and close it, though I have never
> checked the source for this. I was just being lazy.
> 

-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11285407
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Orphan connections from .NET clients

Posted by Dris <ch...@hotmail.com>.
Not really. Actually on looking at it I am closing and disposing of sessions
and consumers, and just disposing of producers and connections so I don't
have your connection.Stop and connection.Close in mine. I would be surprised
if that made a diff though. I had the issue until I added the
connection.Dispose() and then it went away. I think I assumed that disposing
of the session would stop and close it, though I have never checked the
source for this. I was just being lazy.
-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11284651
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Orphan connections from .NET clients

Posted by Jim_Cross <ji...@googlemail.com>.
Hi Chris,

My shutdown code looks like this, but I still get the problem:

                if (consumer != null)
                {
                    consumer.Close();
                    consumer.Dispose();
                }
                
                if (session != null)
                {
                    session.Close();
                    session.Dispose();
                }
                
                if(connection != null)
                {
                    connection.Stop();
                    connection.Close();
                    connection.Dispose();
                }


Are you doing anything different to that?


Dris wrote:
> 
> Have you tried explicitly calling connection.Dispose() before exiting your
> program? To get round this problem I just called Close() and Dispose() on
> every NMS object that exposed either or both those methods and it seemed
> to work for me. I suspect that just disposing of the connection may be
> enough to do the job though. I would be interested to know what happens
> for you. 
> 
> Chris
> 
> 

-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11284483
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Orphan connections from .NET clients

Posted by Jim_Cross <ji...@googlemail.com>.
OK, so finally (!!!!) I've got it.

There seems to be some kind of race condition around closing sessions and
connections. I haven't been through the code enough to figure out where it
is, but I noticed that with this bit of code:

                    if (session != null)
                    {
                        session.Close();
                        session.Dispose();
                    }

                    if (connection != null)
                    {
                        connection.Close();
                        connection.Dispose();
                    }

..putting a breakpoint on the if(connection != null) line stopped the
connection leak. Removing the breakpoint meant the leak came back. So it
seems that the connection may be getting closed before the session, which
leaves the connection hanging around on the server side (or something like
that).

I then had a look at the Connection class, and found that the close() method
closes all sessions, so tried removing my explicit session.close()...and
voila, no more connection leaks!

Jim


Jim_Cross wrote:
> 
> OK, just tried a copy and paste of your code against a remote Windows box,
> and the connection leak appears there as well, so for me it only works
> correctly when run against ActiveMQ on my local box.
> Does it work for you on a remote machine also? If so, could you please
> mail me your NMS.dll and ActiveMQ.dll?
> 
> Thanks,
> 
> Jim
> 
> 
> 
> Jim_Cross wrote:
>> 
>> Thanks Chris.
>> 
>> I just tried this against the Active MQ running on my Windows box, and it
>> worked fine. Then tried it against Active MQ running on the Linux server,
>> and the connections leak appeared again.
>> So it either seems to be related to the OS it's running on, or the fact
>> that when running against my windows box the connections are effectively
>> local...
>> 
>> 
>> 
>> Dris wrote:
>>> 
>>> OK, I compiled the following against a recent-ish version of NMS. I
>>> haven't done a more recent compile because as I pointed out in the NMS
>>> issue tracker there was a bug in the use of selectors which I had fixed
>>> in my source but which hadn't been fixed in the SVN Repo (though it
>>> might have been very recently). This is just your original post with
>>> your disconnect logic shoved in the finally block. When I monitor this
>>> in JConsole the connection gets cleaned up fine and there is no
>>> Connection folder under localhost in the MBeans treeview. Hope this
>>> helps (though obviously it doesn't I guess!). Happy to mail you my
>>> NMS.dll and ActiveMQ.dll compiles for you to try.
>>> 
>>> Chris
>>> 
>>> 
>>> using System;
>>> using System.Threading;
>>> using NMS;
>>> 
>>> namespace ActiveMQ
>>> {
>>> 	class TestMain
>>> 	{
>>> 		static void Main(string[] args)
>>> 		{
>>> 			Uri uri = new Uri("tcp://bhw152:61616");
>>> 			
>>> 			ConnectionFactory factory = new ConnectionFactory(uri);
>>> 			
>>> 			IConnection connection = null;
>>> 			ISession session = null;
>>> 			
>>> 			try
>>> 			{
>>> 				connection = factory.CreateConnection();
>>> 				Console.WriteLine("Connection Created");
>>> 				
>>> 				connection.ClientId = "[test1] " + connection.ClientId;
>>> 				
>>> 				session = connection.CreateSession();
>>> 				Console.WriteLine("Session Created");
>>> 				
>>> 				Thread.Sleep(10000);
>>> 			}
>>> 			finally
>>> 			{
>>> 				try
>>> 				{
>>> 					if (session != null)
>>> 					{
>>> 					    session.Close();
>>> 					    session.Dispose();
>>> 					}
>>> 					
>>> 					if (connection != null)
>>> 					{
>>> 					    connection.Dispose();
>>> 					}
>>> 				}
>>> 				catch (Exception ex)
>>> 				{
>>> 					Console.WriteLine("ERROR DISCONNECTING: " + ex.StackTrace);
>>> 				}
>>> 				Console.WriteLine("Connection Closed");
>>> 			}
>>> 		}
>>> 	}
>>> }
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11323469
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Orphan connections from .NET clients

Posted by Jim_Cross <ji...@googlemail.com>.
OK, just tried a copy and paste of your code against a remote Windows box,
and the connection leak appears there as well, so for me it only works
correctly when run against ActiveMQ on my local box.
Does it work for you on a remote machine also? If so, could you please mail
me your NMS.dll and ActiveMQ.dll?

Thanks,

Jim



Jim_Cross wrote:
> 
> Thanks Chris.
> 
> I just tried this against the Active MQ running on my Windows box, and it
> worked fine. Then tried it against Active MQ running on the Linux server,
> and the connections leak appeared again.
> So it either seems to be related to the OS it's running on, or the fact
> that when running against my windows box the connections are effectively
> local...
> 
> 
> 
> Dris wrote:
>> 
>> OK, I compiled the following against a recent-ish version of NMS. I
>> haven't done a more recent compile because as I pointed out in the NMS
>> issue tracker there was a bug in the use of selectors which I had fixed
>> in my source but which hadn't been fixed in the SVN Repo (though it might
>> have been very recently). This is just your original post with your
>> disconnect logic shoved in the finally block. When I monitor this in
>> JConsole the connection gets cleaned up fine and there is no Connection
>> folder under localhost in the MBeans treeview. Hope this helps (though
>> obviously it doesn't I guess!). Happy to mail you my NMS.dll and
>> ActiveMQ.dll compiles for you to try.
>> 
>> Chris
>> 
>> 
>> using System;
>> using System.Threading;
>> using NMS;
>> 
>> namespace ActiveMQ
>> {
>> 	class TestMain
>> 	{
>> 		static void Main(string[] args)
>> 		{
>> 			Uri uri = new Uri("tcp://bhw152:61616");
>> 			
>> 			ConnectionFactory factory = new ConnectionFactory(uri);
>> 			
>> 			IConnection connection = null;
>> 			ISession session = null;
>> 			
>> 			try
>> 			{
>> 				connection = factory.CreateConnection();
>> 				Console.WriteLine("Connection Created");
>> 				
>> 				connection.ClientId = "[test1] " + connection.ClientId;
>> 				
>> 				session = connection.CreateSession();
>> 				Console.WriteLine("Session Created");
>> 				
>> 				Thread.Sleep(10000);
>> 			}
>> 			finally
>> 			{
>> 				try
>> 				{
>> 					if (session != null)
>> 					{
>> 					    session.Close();
>> 					    session.Dispose();
>> 					}
>> 					
>> 					if (connection != null)
>> 					{
>> 					    connection.Dispose();
>> 					}
>> 				}
>> 				catch (Exception ex)
>> 				{
>> 					Console.WriteLine("ERROR DISCONNECTING: " + ex.StackTrace);
>> 				}
>> 				Console.WriteLine("Connection Closed");
>> 			}
>> 		}
>> 	}
>> }
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11319677
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Orphan connections from .NET clients

Posted by Jim_Cross <ji...@googlemail.com>.
Thanks Chris.

I just tried this against the Active MQ running on my Windows box, and it
worked fine. Then tried it against Active MQ running on the Linux server,
and the connections leak appeared again.
So it either seems to be related to the OS it's running on, or the fact that
when running against my windows box the connections are effectively local...



Dris wrote:
> 
> OK, I compiled the following against a recent-ish version of NMS. I
> haven't done a more recent compile because as I pointed out in the NMS
> issue tracker there was a bug in the use of selectors which I had fixed in
> my source but which hadn't been fixed in the SVN Repo (though it might
> have been very recently). This is just your original post with your
> disconnect logic shoved in the finally block. When I monitor this in
> JConsole the connection gets cleaned up fine and there is no Connection
> folder under localhost in the MBeans treeview. Hope this helps (though
> obviously it doesn't I guess!). Happy to mail you my NMS.dll and
> ActiveMQ.dll compiles for you to try.
> 
> Chris
> 
> 
> using System;
> using System.Threading;
> using NMS;
> 
> namespace ActiveMQ
> {
> 	class TestMain
> 	{
> 		static void Main(string[] args)
> 		{
> 			Uri uri = new Uri("tcp://bhw152:61616");
> 			
> 			ConnectionFactory factory = new ConnectionFactory(uri);
> 			
> 			IConnection connection = null;
> 			ISession session = null;
> 			
> 			try
> 			{
> 				connection = factory.CreateConnection();
> 				Console.WriteLine("Connection Created");
> 				
> 				connection.ClientId = "[test1] " + connection.ClientId;
> 				
> 				session = connection.CreateSession();
> 				Console.WriteLine("Session Created");
> 				
> 				Thread.Sleep(10000);
> 			}
> 			finally
> 			{
> 				try
> 				{
> 					if (session != null)
> 					{
> 					    session.Close();
> 					    session.Dispose();
> 					}
> 					
> 					if (connection != null)
> 					{
> 					    connection.Dispose();
> 					}
> 				}
> 				catch (Exception ex)
> 				{
> 					Console.WriteLine("ERROR DISCONNECTING: " + ex.StackTrace);
> 				}
> 				Console.WriteLine("Connection Closed");
> 			}
> 		}
> 	}
> }
> 

-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11309857
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Orphan connections from .NET clients

Posted by Dris <ch...@hotmail.com>.
OK, I compiled the following against a recent-ish version of NMS. I haven't
done a more recent compile because as I pointed out in the NMS issue tracker
there was a bug in the use of selectors which I had fixed in my source but
which hadn't been fixed in the SVN Repo (though it might have been very
recently). This is just your original post with your disconnect logic shoved
in the finally block. When I monitor this in JConsole the connection gets
cleaned up fine and there is no Connection folder under localhost in the
MBeans treeview. Hope this helps (though obviously it doesn't I guess!).
Happy to mail you my NMS.dll and ActiveMQ.dll compiles for you to try.

Chris


using System;
using System.Threading;
using NMS;

namespace ActiveMQ
{
	class TestMain
	{
		static void Main(string[] args)
		{
			Uri uri = new Uri("tcp://bhw152:61616");
			
			ConnectionFactory factory = new ConnectionFactory(uri);
			
			IConnection connection = null;
			ISession session = null;
			
			try
			{
				connection = factory.CreateConnection();
				Console.WriteLine("Connection Created");
				
				connection.ClientId = "[test1] " + connection.ClientId;
				
				session = connection.CreateSession();
				Console.WriteLine("Session Created");
				
				Thread.Sleep(10000);
			}
			finally
			{
				try
				{
					if (session != null)
					{
					    session.Close();
					    session.Dispose();
					}
					
					if (connection != null)
					{
					    connection.Dispose();
					}
				}
				catch (Exception ex)
				{
					Console.WriteLine("ERROR DISCONNECTING: " + ex.StackTrace);
				}
				Console.WriteLine("Connection Closed");
			}
		}
	}
}
-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11307469
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Orphan connections from .NET clients

Posted by Jim_Cross <ji...@googlemail.com>.
Sure, thanks for checking this out. 
As per your last suggestion, here's a version which closes and disposes the
consumer and session, and only disposes the connection.

public void Disconnect()
        {
            try
            {
                if (consumer != null)
                {
                    consumer.Close();
                    consumer.Dispose();
                }

                if (session != null)
                {
                    session.Close();
                    session.Dispose();
                }

                if (connection != null)
                {
                    connection.Dispose();
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR DISCONNECTING: " + ex.StackTrace);
            }
        }


FYI I'm using ActiveMQ 4.1.1 and have tried this with the latest NMS code
from Subversion.



Dris wrote:
> 
> Can you post a piece of code like the one in your original post, but
> including your cleanup code (I can't see the calls to dispose etc. in the
> original post). I will compile it and run it on my set up (4.1.1 + some
> fairly recent version of NMS) and see what happens. 
> 
> Chris
> 
> 

-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11301140
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Orphan connections from .NET clients

Posted by Dris <ch...@hotmail.com>.
Can you post a piece of code like the one in your original post, but
including your cleanup code (I can't see the calls to dispose etc. in the
original post). I will compile it and run it on my set up (4.1.1 + some
fairly recent version of NMS) and see what happens. 

Chris

-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11290610
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Orphan connections from .NET clients

Posted by Dris <ch...@hotmail.com>.
Have you tried explicitly calling connection.Dispose() before exiting your
program? To get round this problem I just called Close() and Dispose() on
every NMS object that exposed either or both those methods and it seemed to
work for me. I suspect that just disposing of the connection may be enough
to do the job though. I would be interested to know what happens for you. 

Chris

-- 
View this message in context: http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11269315
Sent from the ActiveMQ - User mailing list archive at Nabble.com.