activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgo...@apache.org
Subject svn commit: r776510 - /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Discovery/DiscoveryTransportFactory.cs
Date Wed, 20 May 2009 00:41:40 GMT
Author: jgomes
Date: Wed May 20 00:41:40 2009
New Revision: 776510

URL: http://svn.apache.org/viewvc?rev=776510&view=rev
Log:
Refactored locking to minimize the length locks are held.

Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Discovery/DiscoveryTransportFactory.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Discovery/DiscoveryTransportFactory.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Discovery/DiscoveryTransportFactory.cs?rev=776510&r1=776509&r2=776510&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Discovery/DiscoveryTransportFactory.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/Discovery/DiscoveryTransportFactory.cs
Wed May 20 00:41:40 2009
@@ -31,12 +31,26 @@
 		private static string currentServiceName;
 		private static readonly object uriLock = new object();
 		private static readonly AutoResetEvent uriDiscoveredEvent = new AutoResetEvent(false);
-		public static event ExceptionListener OnException;
+		private static event ExceptionListener OnException;
 
+		static DiscoveryTransportFactory()
+		{
+			DiscoveryTransportFactory.OnException += TransportFactory.HandleException;
+			agent = new MulticastDiscoveryAgent();
+			agent.OnNewServiceFound += agent_OnNewServiceFound;
+			agent.OnServiceRemoved += agent_OnServiceRemoved;
+		}
+		
 		public DiscoveryTransportFactory()
 		{
 			currentServiceName = String.Empty;
 		}
+		
+		public static Uri DiscoveredUri
+		{
+			get { lock(uriLock) { return discoveredUri; } }
+			set { lock(uriLock) { discoveredUri = value; } }
+		}
 
 		private static void agent_OnNewServiceFound(string brokerName, string serviceName)
 		{
@@ -47,84 +61,47 @@
 					currentServiceName = serviceName;
 					discoveredUri = new Uri(currentServiceName);
 				}
-
-				// This will end the wait in the CreateTransport method.
-				uriDiscoveredEvent.Set();
 			}
+
+			// This will end the wait in the CreateTransport method.
+			uriDiscoveredEvent.Set();
 		}
 
 		private static void agent_OnServiceRemoved(string brokerName, string serviceName)
 		{
 			if(serviceName == currentServiceName)
 			{
-				lock(uriLock)
-				{
-					discoveredUri = null;
-				}
-
-				if(OnException != null)
-				{
-					OnException(new Exception("Broker is dead!"));
-				}
+				DiscoveredUri = null;
+				DiscoveryTransportFactory.OnException(new Exception("Broker connection is no longer valid."));
 			}
 		}
 
-		private static MulticastDiscoveryAgent Agent
-		{
-			get
-			{
-				if(agent == null)
-				{
-					agent = new MulticastDiscoveryAgent();
-					agent.OnNewServiceFound += agent_OnNewServiceFound;
-					agent.OnServiceRemoved += agent_OnServiceRemoved;
-				}
-
-				return agent;
-			}
-		}
-
-		#region Overloaded FailoverTransportFactory Members
+		#region Overloaded ITransportFactory Members
 
 		public ITransport CreateTransport(Uri location)
 		{
-			if(!Agent.IsStarted)
+			if(!agent.IsStarted)
 			{
-				Agent.Start();
+				agent.Start();
 			}
-
-			DateTime expireTime = DateTime.Now.AddSeconds(TIMEOUT_IN_SECONDS);
-
-			// If a new broker is found the agent will fire an event which will result in discoveredUri
being set.
-			lock(uriLock)
+			
+			if(null == DiscoveredUri)
 			{
-				while(discoveredUri == null)
+				// If a new broker is found the agent will fire an event which will result in discoveredUri
being set.
+				uriDiscoveredEvent.WaitOne(TIMEOUT_IN_SECONDS * 1000, true);
+				if(null == DiscoveredUri)
 				{
-					if(expireTime < DateTime.Now)
-					{
-						throw new NMSConnectionException(
-							"Unable to find a connection before the timeout period expired.");
-					}
-
-					uriDiscoveredEvent.WaitOne(TIMEOUT_IN_SECONDS * 1000, true);
+					throw new NMSConnectionException("Unable to find a connection before the timeout period
expired.");
 				}
 			}
 
-			ITransport transport;
-
-			lock(uriLock)
-			{
-				TcpTransportFactory tcpTransFactory = new TcpTransportFactory();
-
-				transport = tcpTransFactory.CreateTransport(new Uri(discoveredUri + location.Query));
-			}
-
-			return transport;
+			TcpTransportFactory tcpTransFactory = new TcpTransportFactory();
+			return tcpTransFactory.CreateTransport(new Uri(DiscoveredUri + location.Query));
 		}
 
 		public ITransport CompositeConnect(Uri location)
 		{
-			throw new NMSConnectionException("Composite connection not supported with Discovery transport.");
+			throw new NMSConnectionException("Composite connection not supported with MulticastDiscovery
transport.");
 		}
 
 		#endregion



Mime
View raw message