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 2010/07/07 21:24:18 UTC

svn commit: r961482 - in /activemq/activemq-dotnet/Apache.NMS.Stomp/trunk: src/main/csharp/Transport/Failover/FailoverTransport.cs src/main/csharp/Transport/InactivityMonitor.cs src/main/csharp/Util/ThreadUtil.cs vs2008-stomp.csproj

Author: jgomes
Date: Wed Jul  7 19:24:17 2010
New Revision: 961482

URL: http://svn.apache.org/viewvc?rev=961482&view=rev
Log:
Refactored mutex and locking code to support .NET Compact Framework APIs.

Added:
    activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Util/ThreadUtil.cs
Modified:
    activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/Failover/FailoverTransport.cs
    activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/InactivityMonitor.cs
    activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/vs2008-stomp.csproj

Modified: activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/Failover/FailoverTransport.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/Failover/FailoverTransport.cs?rev=961482&r1=961481&r2=961482&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/Failover/FailoverTransport.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/Failover/FailoverTransport.cs Wed Jul  7 19:24:17 2010
@@ -22,6 +22,7 @@ using System.Threading;
 using Apache.NMS.Stomp.Commands;
 using Apache.NMS.Stomp.State;
 using Apache.NMS.Stomp.Threads;
+using Apache.NMS.Stomp.Util;
 using Apache.NMS.Util;
 
 namespace Apache.NMS.Stomp.Transport.Failover
@@ -78,9 +79,9 @@ namespace Apache.NMS.Stomp.Transport.Fai
         {
             Dispose(false);
         }
-        
+
         #region FailoverTask
-        
+
         private class FailoverTask : Task
         {
             private FailoverTransport parent;
@@ -160,7 +161,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
             get { return requestTimeout; }
             set { requestTimeout = value; }
         }
-        
+
         public int InitialReconnectDelay
         {
             get { return initialReconnectDelay; }
@@ -216,7 +217,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
         }
 
         #endregion
-        
+
         public bool IsFaultTolerant
         {
             get { return true; }
@@ -236,7 +237,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
         {
             get { return started; }
         }
-                   
+
         /// <summary>
         /// </summary>
         /// <param name="command"></param>
@@ -245,7 +246,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
         {
             return (command != null && (command.IsShutdownInfo || command is RemoveInfo));
         }
-        
+
         public void OnException(ITransport sender, Exception error)
         {
             try
@@ -388,7 +389,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
         {
             throw new ApplicationException("FailoverTransport does not implement Request(Command, TimeSpan)");
         }
-        
+
         public void OnCommand(ITransport sender, Command command)
         {
             if(command != null)
@@ -426,7 +427,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
         public void Oneway(Command command)
         {
             Exception error = null;
-            
+
             lock(reconnectMutex)
             {
                 if(IsShutdownCommand(command) && ConnectedTransport == null)
@@ -446,7 +447,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
                         return;
                     }
                 }
-                
+
                 // Keep trying until the message is sent.
                 for(int i = 0; !disposed; i++)
                 {
@@ -454,7 +455,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
                     {
                         // Wait for transport to be connected.
                         ITransport transport = ConnectedTransport;
-                        DateTime start = DateTime.Now;                            
+                        DateTime start = DateTime.Now;
                         bool timedout = false;
                         while(transport == null && !disposed && connectionFailure == null)
                         {
@@ -467,17 +468,17 @@ namespace Apache.NMS.Stomp.Transport.Fai
                                 Tracer.DebugFormat("FailoverTransport.oneway - timed out after {0} mills", elapsed );
                                 break;
                             }
-                            
+
                             // Release so that the reconnect task can run
                             try
                             {
                                 // Wait for something
-                                Monitor.Wait(reconnectMutex, 1000);
+                                ThreadUtil.MonitorWait(reconnectMutex, 1000);
                             }
-                            catch(ThreadInterruptedException e)
+                            catch(Exception e)
                             {
                                 Tracer.DebugFormat("Interrupted: {0}", e.Message);
-                            }                            
+                            }
 
                             transport = ConnectedTransport;
                         }
@@ -626,7 +627,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
         {
             Add(new Uri[] { uri });
         }
-        
+
         public void Reconnect()
         {
             lock(reconnectMutex)
@@ -644,7 +645,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
                     {
                         reconnectTask.Wakeup();
                     }
-                    catch(ThreadInterruptedException)
+                    catch(Exception)
                     {
                     }
                 }
@@ -707,7 +708,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
                 t.Oneway(command);
             }
         }
-        
+
         public Uri RemoteAddress
         {
             get
@@ -847,7 +848,7 @@ namespace Apache.NMS.Stomp.Transport.Fai
                     {
                         Thread.Sleep(ReconnectDelay);
                     }
-                    catch(ThreadInterruptedException)
+                    catch(Exception)
                     {
                     }
                 }

Modified: activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/InactivityMonitor.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/InactivityMonitor.cs?rev=961482&r1=961481&r2=961482&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/InactivityMonitor.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Transport/InactivityMonitor.cs Wed Jul  7 19:24:17 2010
@@ -19,6 +19,7 @@ using System;
 using System.Threading;
 using Apache.NMS.Stomp.Commands;
 using Apache.NMS.Stomp.Threads;
+using Apache.NMS.Stomp.Util;
 using Apache.NMS.Util;
 
 namespace Apache.NMS.Stomp.Transport
@@ -95,7 +96,7 @@ namespace Apache.NMS.Stomp.Transport
         {
             if(this.inWrite.Value || this.failed.Value)
             {
-                Tracer.Debug("Inactivity Monitor is in write or already failed.");				
+                Tracer.Debug("Inactivity Monitor is in write or already failed.");
                 return;
             }
 
@@ -210,12 +211,9 @@ namespace Apache.NMS.Stomp.Transport
             {
                 if(monitorStarted.CompareAndSet(true, false))
                 {
-                    AutoResetEvent shutdownEvent = new AutoResetEvent(false);
-
                     // Attempt to wait for the Timer to shutdown, but don't wait
                     // forever, if they don't shutdown after two seconds, just quit.
-                    this.connectionCheckTimer.Dispose(shutdownEvent);
-                    shutdownEvent.WaitOne(TimeSpan.FromMilliseconds(2000), false);
+                    ThreadUtil.DisposeTimer(connectionCheckTimer, 2000);
 
                     this.asyncTask.Shutdown();
                     this.asyncTask = null;

Added: activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Util/ThreadUtil.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Util/ThreadUtil.cs?rev=961482&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Util/ThreadUtil.cs (added)
+++ activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Util/ThreadUtil.cs Wed Jul  7 19:24:17 2010
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Threading;
+
+namespace Apache.NMS.Stomp.Util
+{
+    public class ThreadUtil
+    {
+       public static bool MonitorWait(Mutex mutex, int timeout)
+       {
+#if NETCF
+            int waitTime = 0;
+            bool acquiredLock = false;
+
+            // Release so that the reconnect task can run
+            Monitor.Exit(mutex);
+            // Wait for something
+            while(!(acquiredLock = Monitor.TryEnter(mutex)) && waitTime < timeout)
+            {
+                Thread.Sleep(1);
+                waitTime++;
+            }
+
+            return acquiredLock;
+#else
+           return Monitor.Wait(mutex, timeout);
+#endif
+       }
+
+       public static void DisposeTimer(Timer timer, int timeout)
+       {
+#if NETCF
+            timer.Dispose();
+#else
+            AutoResetEvent shutdownEvent = new AutoResetEvent(false);
+
+            // Attempt to wait for the Timer to shutdown
+            timer.Dispose(shutdownEvent);
+            shutdownEvent.WaitOne(timeout, false);
+#endif
+       }
+    }
+}

Modified: activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/vs2008-stomp.csproj
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/vs2008-stomp.csproj?rev=961482&r1=961481&r2=961482&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/vs2008-stomp.csproj (original)
+++ activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/vs2008-stomp.csproj Wed Jul  7 19:24:17 2010
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -160,6 +160,7 @@
     <Compile Include="src\main\csharp\Transport\ICompositeTransport.cs" />
     <Compile Include="src\main\csharp\State\SynchronizedObjects.cs" />
     <Compile Include="src\main\csharp\IOException.cs" />
+    <Compile Include="src\main\csharp\Util\ThreadUtil.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="keyfile\NMSKey.snk" />
@@ -177,7 +178,4 @@
     </MonoDevelop>
     <VisualStudio />
   </ProjectExtensions>
-  <ItemGroup>
-    <Folder Include="src\main\csharp\State\" />
-  </ItemGroup>
 </Project>
\ No newline at end of file