activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgo...@apache.org
Subject svn commit: r925411 - in /activemq/activemq-dotnet: Apache.NMS.ActiveMQ/trunk/src/main/csharp/QueueBrowser.cs Apache.NMS.EMS/trunk/src/main/csharp/QueueBrowser.cs
Date Fri, 19 Mar 2010 20:39:02 GMT
Author: jgomes
Date: Fri Mar 19 20:39:01 2010
New Revision: 925411

URL: http://svn.apache.org/viewvc?rev=925411&view=rev
Log:
Added support for IDisposable to EMS QueueBrowser.
Modified locking strategy on QueueBrowser to be consistent with locking strategy used for
Session.  Added safety checks for closing an already closed QueueBrowser.


Fixes [AMQNET-206]. (See https://issues.apache.org/activemq/browse/AMQNET-206)

Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/QueueBrowser.cs
    activemq/activemq-dotnet/Apache.NMS.EMS/trunk/src/main/csharp/QueueBrowser.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/QueueBrowser.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/QueueBrowser.cs?rev=925411&r1=925410&r2=925411&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/QueueBrowser.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/QueueBrowser.cs Fri
Mar 19 20:39:01 2010
@@ -51,7 +51,7 @@ namespace Apache.NMS.ActiveMQ
 			this.dispatchAsync = dispatchAsync;
 			this.consumer = CreateConsumer();
 		}
-		
+
 		~QueueBrowser()
 		{
 			Dispose(false);
@@ -86,48 +86,48 @@ namespace Apache.NMS.ActiveMQ
 
 			disposed = true;
 		}
-		
+
 		private MessageConsumer CreateConsumer()
 		{
-            this.browseDone.Value = false;
+			this.browseDone.Value = false;
 			BrowsingMessageConsumer consumer = null;
 
 			try
 			{
-                consumer = new BrowsingMessageConsumer(
-                    this, session, this.consumerId, this.destination, null, this.selector,

-                    this.session.Connection.PrefetchPolicy.QueueBrowserPrefetch,
-                    this.session.Connection.PrefetchPolicy.MaximumPendingMessageLimit,
-                    false, true, this.dispatchAsync);
-
-                this.session.AddConsumer(consumer);
-                this.session.Connection.SyncRequest(consumer.ConsumerInfo);
-
-                if(this.session.Connection.IsStarted)
-                {
-                    consumer.Start();
-                }
-            }
-            catch(Exception)
-            {
-                if(consumer != null)
-                {
-                    this.session.RemoveConsumer(consumer.ConsumerId);
-                    consumer.Close();
-                }
+				consumer = new BrowsingMessageConsumer(
+					this, session, this.consumerId, this.destination, null, this.selector,
+					this.session.Connection.PrefetchPolicy.QueueBrowserPrefetch,
+					this.session.Connection.PrefetchPolicy.MaximumPendingMessageLimit,
+					false, true, this.dispatchAsync);
 
-                throw;
-            }
+				this.session.AddConsumer(consumer);
+				this.session.Connection.SyncRequest(consumer.ConsumerInfo);
 
-            return consumer;
+				if(this.session.Connection.IsStarted)
+				{
+					consumer.Start();
+				}
+			}
+			catch(Exception)
+			{
+				if(consumer != null)
+				{
+					this.session.RemoveConsumer(consumer.ConsumerId);
+					consumer.Close();
+				}
+
+				throw;
+			}
+
+			return consumer;
 		}
 
 		private void DestroyConsumer()
 		{
 			if(consumer == null)
-            {
+			{
 				return;
-            }
+			}
 
 			try
 			{
@@ -136,7 +136,7 @@ namespace Apache.NMS.ActiveMQ
 			}
 			catch(NMSException e)
 			{
-                Tracer.Debug(e.StackTrace.ToString());
+				Tracer.Debug(e.StackTrace.ToString());
 			}
 		}
 
@@ -144,10 +144,13 @@ namespace Apache.NMS.ActiveMQ
 		{
 			CheckClosed();
 
-			if(this.consumer == null)
-            {
-				this.consumer = CreateConsumer();
-            }
+			lock(myLock)
+			{
+				if(this.consumer == null)
+				{
+					this.consumer = CreateConsumer();
+				}
+			}
 
 			return this;
 		}
@@ -156,9 +159,9 @@ namespace Apache.NMS.ActiveMQ
 		private void CheckClosed()
 		{
 			if(this.closed)
-            {
+			{
 				throw new IllegalStateException("The Consumer is closed");
-            }
+			}
 		}
 
 		public bool MoveNext()
@@ -168,20 +171,20 @@ namespace Apache.NMS.ActiveMQ
 				lock(myLock)
 				{
 					if(consumer == null)
-                    {
+					{
 						return false;
-                    }
-				}
+					}
 
-				if(consumer.UnconsumedMessageCount > 0)
-                {
-					return true;
-                }
+					if(consumer.UnconsumedMessageCount > 0)
+					{
+						return true;
+					}
 
-				if(browseDone.Value || !session.Started)
-				{
-					DestroyConsumer();
-					return false;
+					if(browseDone.Value || !session.Started)
+					{
+						DestroyConsumer();
+						return false;
+					}
 				}
 
 				WaitForMessage();
@@ -197,31 +200,31 @@ namespace Apache.NMS.ActiveMQ
 					lock(myLock)
 					{
 						if(consumer == null)
-                        {
+						{
 							return null;
-                        }
-					}
-
-					try
-					{
-						IMessage answer = consumer.ReceiveNoWait();
+						}
 
-						if(answer != null)
-                        {
-							return answer;
-                        }
-					}
-					catch(NMSException)
-					{
-						//TODO: Not implemented.
-						//this.session.Connection.OnClientInternalException(e);
-						return null;
-					}
+						try
+						{
+							IMessage answer = consumer.ReceiveNoWait();
+
+							if(answer != null)
+							{
+								return answer;
+							}
+						}
+						catch(NMSException)
+						{
+							//TODO: Not implemented.
+							//this.session.Connection.OnClientInternalException(e);
+							return null;
+						}
 
-					if(browseDone.Value || !session.Started)
-					{
-						DestroyConsumer();
-						return null;
+						if(browseDone.Value || !session.Started)
+						{
+							DestroyConsumer();
+							return null;
+						}
 					}
 
 					WaitForMessage();
@@ -229,11 +232,28 @@ namespace Apache.NMS.ActiveMQ
 			}
 		}
 
-		[MethodImpl(MethodImplOptions.Synchronized)]
 		public void Close()
 		{
-			DestroyConsumer();
-			closed = true;
+			lock(myLock)
+			{
+				if(this.closed)
+				{
+					return;
+				}
+
+				try
+				{
+					DestroyConsumer();
+				}
+				catch(Exception ex)
+				{
+					Tracer.ErrorFormat("Error during QueueBrowser close: {0}", ex);
+				}
+				finally
+				{
+					this.closed = true;
+				}
+			}
 		}
 
 		public IQueue Queue
@@ -277,9 +297,9 @@ namespace Apache.NMS.ActiveMQ
 		public void Reset()
 		{
 			if(consumer != null)
-            {
+			{
 				DestroyConsumer();
-            }
+			}
 
 			consumer = CreateConsumer();
 		}
@@ -288,9 +308,9 @@ namespace Apache.NMS.ActiveMQ
 		{
 			private QueueBrowser parent;
 
-			public BrowsingMessageConsumer(QueueBrowser parent, Session session, ConsumerId id, ActiveMQDestination
destination, 
-                                           String name, String selector, int prefetch, int
maxPendingMessageCount, 
-                                           bool noLocal, bool browser, bool dispatchAsync)
+			public BrowsingMessageConsumer(QueueBrowser parent, Session session, ConsumerId id, ActiveMQDestination
destination,
+										   String name, String selector, int prefetch, int maxPendingMessageCount,
+										   bool noLocal, bool browser, bool dispatchAsync)
 				: base(session, id, destination, name, selector, prefetch, maxPendingMessageCount, noLocal,
browser, dispatchAsync)
 			{
 				this.parent = parent;
@@ -299,9 +319,9 @@ namespace Apache.NMS.ActiveMQ
 			public override void Dispatch(MessageDispatch md)
 			{
 				if(md.Message == null)
-                {
+				{
 					parent.browseDone.Value = true;
-                }
+				}
 				else
 				{
 					base.Dispatch(md);

Modified: activemq/activemq-dotnet/Apache.NMS.EMS/trunk/src/main/csharp/QueueBrowser.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.EMS/trunk/src/main/csharp/QueueBrowser.cs?rev=925411&r1=925410&r2=925411&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.EMS/trunk/src/main/csharp/QueueBrowser.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.EMS/trunk/src/main/csharp/QueueBrowser.cs Fri Mar
19 20:39:01 2010
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 
+using System;
 using System.Collections;
 
 namespace Apache.NMS.EMS
@@ -22,15 +23,78 @@ namespace Apache.NMS.EMS
 	public class QueueBrowser : Apache.NMS.IQueueBrowser
 	{
 		public TIBCO.EMS.QueueBrowser tibcoQueueBrowser;
+		private bool closed = false;
+		private bool disposed = false;
 
 		public QueueBrowser(TIBCO.EMS.QueueBrowser queueBrowser)
 		{
 			this.tibcoQueueBrowser = queueBrowser;
 		}
 
+		~QueueBrowser()
+		{
+			Dispose(false);
+		}
+
+		#region IDisposable Members
+
+		///<summary>
+		/// Performs application-defined tasks associated with freeing, releasing, or resetting
unmanaged resources.
+		///</summary>
+		public void Dispose()
+		{
+			Dispose(true);
+			GC.SuppressFinalize(this);
+		}
+
+		protected void Dispose(bool disposing)
+		{
+			if(disposed)
+			{
+				return;
+			}
+
+			if(disposing)
+			{
+				// Dispose managed code here.
+			}
+
+			try
+			{
+				Close();
+			}
+			catch
+			{
+				// Ignore errors.
+			}
+
+			disposed = true;
+		}
+
+		#endregion
+
 		public void  Close()
 		{
-			this.tibcoQueueBrowser.Close();
+			lock(this)
+			{
+				if(closed)
+				{
+					return;
+				}
+
+				try
+				{
+					this.tibcoQueueBrowser.Close();
+				}
+				catch(Exception ex)
+				{
+					ExceptionUtil.WrapAndThrowNMSException(ex);
+				}
+				finally
+				{
+					closed = true;
+				}
+			}
 		}
 
 		public string MessageSelector



Mime
View raw message