Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 52635 invoked from network); 20 May 2009 00:41:52 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 20 May 2009 00:41:52 -0000 Received: (qmail 98397 invoked by uid 500); 20 May 2009 00:42:05 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 98351 invoked by uid 500); 20 May 2009 00:42:05 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 98342 invoked by uid 99); 20 May 2009 00:42:05 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 May 2009 00:42:05 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 May 2009 00:42:01 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E118023888AD; Wed, 20 May 2009 00:41:40 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@activemq.apache.org From: jgomes@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090520004140.E118023888AD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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