You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by jg...@apache.org on 2009/06/08 23:49:09 UTC

svn commit: r782803 - in /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp: State/SynchronizedObjects.cs Transport/Failover/FailoverTransport.cs

Author: jgomes
Date: Mon Jun  8 21:49:09 2009
New Revision: 782803

URL: http://svn.apache.org/viewvc?rev=782803&view=rev
Log:
Reimplemented the copy constructor for AtomicCollection to work around bug in Mono.  Improved locking algorithms to use SyncRoot.
Fixed broken Reconnect() call in FailoverTransport.cs when previous code changes were rolled back.  The background thread task was longer being started and the reconnect actions would never be performed.
Fixes [AMQNET-152]. (See https://issues.apache.org/activemq/browse/AMQNET-152)

Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/State/SynchronizedObjects.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Failover/FailoverTransport.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/State/SynchronizedObjects.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/State/SynchronizedObjects.cs?rev=782803&r1=782802&r2=782803&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/State/SynchronizedObjects.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/State/SynchronizedObjects.cs Mon Jun  8 21:49:09 2009
@@ -24,24 +24,28 @@
 	public class AtomicCollection<TValue>
 		where TValue : class
 	{
-		private Object myLock = new Object();
-		private ArrayList _collection;
+		private ArrayList _collection = new ArrayList();
 
 		public AtomicCollection()
 		{
-			_collection = new ArrayList();
 		}
 
 		public AtomicCollection(ICollection c)
 		{
-			_collection = new ArrayList(c);
+			lock(c.SyncRoot)
+			{
+				foreach(object obj in c)
+				{
+					_collection.Add(obj);
+				}
+			}
 		}
 
 		public int Count
 		{
 			get
 			{
-				lock(myLock)
+				lock(_collection.SyncRoot)
 				{
 					return _collection.Count;
 				}
@@ -58,7 +62,7 @@
 
 		public int Add(TValue v)
 		{
-			lock(myLock)
+			lock(_collection.SyncRoot)
 			{
 				return _collection.Add(v);
 			}
@@ -66,7 +70,7 @@
 
 		public void Clear()
 		{
-			lock(myLock)
+			lock(_collection.SyncRoot)
 			{
 				_collection.Clear();
 			}
@@ -74,7 +78,7 @@
 
 		public bool Contains(TValue v)
 		{
-			lock(myLock)
+			lock(_collection.SyncRoot)
 			{
 				return _collection.Contains(v);
 			}
@@ -82,7 +86,7 @@
 
 		public void CopyTo(TValue[] a, int index)
 		{
-			lock(myLock)
+			lock(_collection.SyncRoot)
 			{
 				_collection.CopyTo(a, index);
 			}
@@ -90,7 +94,7 @@
 
 		public void Remove(TValue v)
 		{
-			lock(myLock)
+			lock(_collection.SyncRoot)
 			{
 				_collection.Remove(v);
 			}
@@ -98,7 +102,7 @@
 
 		public void RemoveAt(int index)
 		{
-			lock(myLock)
+			lock(_collection.SyncRoot)
 			{
 				_collection.RemoveAt(index);
 			}
@@ -109,7 +113,7 @@
 			get
 			{
 				TValue ret;
-				lock(myLock)
+				lock(_collection.SyncRoot)
 				{
 					ret = (TValue) _collection[index];
 				}
@@ -117,7 +121,7 @@
 			}
 			set
 			{
-				lock(myLock)
+				lock(_collection.SyncRoot)
 				{
 					_collection[index] = value;
 				}
@@ -126,7 +130,7 @@
 
 		public IEnumerator GetEnumerator()
 		{
-			lock(myLock)
+			lock(_collection.SyncRoot)
 			{
 				return _collection.GetEnumerator();
 			}
@@ -135,7 +139,7 @@
 #if !NETCF
 		public IEnumerator GetEnumerator(int index, int count)
 		{
-			lock(myLock)
+			lock(_collection.SyncRoot)
 			{
 				return _collection.GetEnumerator(index, count);
 			}
@@ -147,7 +151,6 @@
 		where TKey : class
 		where TValue : class
 	{
-		private Object myLock = new Object();
 		private Dictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();
 
 		public void Clear()
@@ -160,7 +163,7 @@
 			get
 			{
 				TValue ret;
-				lock(myLock)
+				lock(((ICollection) _dictionary).SyncRoot)
 				{
 					ret = _dictionary[key];
 				}
@@ -168,7 +171,7 @@
 			}
 			set
 			{
-				lock(myLock)
+				lock(((ICollection) _dictionary).SyncRoot)
 				{
 					_dictionary[key] = value;
 				}
@@ -179,7 +182,7 @@
 		{
 			get
 			{
-				lock(myLock)
+				lock(((ICollection) _dictionary).SyncRoot)
 				{
 					return new AtomicCollection<TKey>(_dictionary.Keys);
 				}
@@ -190,7 +193,7 @@
 		{
 			get
 			{
-				lock(myLock)
+				lock(((ICollection) _dictionary).SyncRoot)
 				{
 					return new AtomicCollection<TValue>(_dictionary.Values);
 				}
@@ -199,7 +202,7 @@
 
 		public void Add(TKey k, TValue v)
 		{
-			lock(myLock)
+			lock(((ICollection) _dictionary).SyncRoot)
 			{
 				_dictionary.Add(k, v);
 			}
@@ -207,7 +210,7 @@
 
 		public bool Remove(TKey v)
 		{
-			lock(myLock)
+			lock(((ICollection) _dictionary).SyncRoot)
 			{
 				return _dictionary.Remove(v);
 			}

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Failover/FailoverTransport.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Failover/FailoverTransport.cs?rev=782803&r1=782802&r2=782803&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Failover/FailoverTransport.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Failover/FailoverTransport.cs Mon Jun  8 21:49:09 2009
@@ -16,6 +16,7 @@
  */
 
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Threading;
 using Apache.NMS.ActiveMQ.Commands;
@@ -151,7 +152,7 @@
 				if(command.IsResponse)
 				{
 					Object oo = null;
-					lock(requestMap)
+					lock(((ICollection) requestMap).SyncRoot)
 					{
 						int v = ((Response) command).CorrelationId;
 						try
@@ -481,8 +482,7 @@
 
 						if(transport == null)
 						{
-							// Previous loop may have exited due to use being
-							// disposed.
+							// Previous loop may have exited due to use being disposed.
 							if(disposed)
 							{
 								error = new IOException("Transport disposed.");
@@ -499,11 +499,10 @@
 						}
 
 						// If it was a request and it was not being tracked by
-						// the state tracker,
-						// then hold it in the requestMap so that we can replay
-						// it later.
+						// the state tracker, then hold it in the requestMap so
+						// that we can replay it later.
 						Tracked tracked = stateTracker.track(command);
-						lock(requestMap)
+						lock(((ICollection) requestMap).SyncRoot)
 						{
 							if(tracked != null && tracked.WaitingForResponse)
 							{
@@ -530,12 +529,14 @@
 							{
 
 								// since we will retry in this method.. take it
-								// out of the request
-								// map so that it is not sent 2 times on
-								// recovery
+								// out of the request map so that it is not
+								// sent 2 times on recovery
 								if(command.ResponseRequired)
 								{
-									requestMap.Remove(command.CommandId);
+									lock(((ICollection) requestMap).SyncRoot)
+									{
+										requestMap.Remove(command.CommandId);
+									}
 								}
 
 								// Rethrow the exception so it will handled by
@@ -699,14 +700,17 @@
 
 		protected void restoreTransport(ITransport t)
 		{
+			Tracer.Info("Restoring previous transport connection.");
 			t.Start();
 			//send information to the broker - informing it we are an ft client
 			ConnectionControl cc = new ConnectionControl();
 			cc.FaultTolerant = true;
 			t.Oneway(cc);
 			stateTracker.DoRestore(t);
+			
+			Tracer.Info("Sending queued commands...");
 			Dictionary<int, Command> tmpMap = null;
-			lock(requestMap)
+			lock(((ICollection) requestMap).SyncRoot)
 			{
 				tmpMap = new Dictionary<int, Command>(requestMap);
 			}
@@ -792,7 +796,7 @@
 									ConnectedTransportURI = uri;
 									ConnectedTransport = t;
 									connectFailures = 0;
-                                    connected = true;
+									connected = true;
 									Tracer.InfoFormat("Successfully reconnected to backup {0}", uri.ToString());
 									return false;
 								}
@@ -833,6 +837,7 @@
 								ConnectedTransportURI = uri;
 								ConnectedTransport = t;
 								connectFailures = 0;
+								connected = true;
 
 								if(firstConnection)
 								{
@@ -844,7 +849,6 @@
 									Tracer.InfoFormat("Successfully reconnected to: {0}", uri.ToString());
 								}
 
-								connected = true;
 								return false;
 							}
 							catch(Exception e)