Skip to content
Permalink
Browse files
Refactored mutex and locking code to support .NET Compact Framework A…
…PIs.
  • Loading branch information
Jim Gomes committed Jul 7, 2010
1 parent ad9fd0b commit a5e241f6818f2ee88f789cb65f5c0352fbd7e7a9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 27 deletions.
@@ -22,6 +22,7 @@
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 @@ public FailoverTransport()
{
Dispose(false);
}

#region FailoverTask

private class FailoverTask : Task
{
private FailoverTransport parent;
@@ -160,7 +161,7 @@ public TimeSpan RequestTimeout
get { return requestTimeout; }
set { requestTimeout = value; }
}

public int InitialReconnectDelay
{
get { return initialReconnectDelay; }
@@ -216,7 +217,7 @@ public bool UseExponentialBackOff
}

#endregion

public bool IsFaultTolerant
{
get { return true; }
@@ -236,7 +237,7 @@ public bool IsStarted
{
get { return started; }
}

/// <summary>
/// </summary>
/// <param name="command"></param>
@@ -245,7 +246,7 @@ private bool IsShutdownCommand(Command command)
{
return (command != null && (command.IsShutdownInfo || command is RemoveInfo));
}

public void OnException(ITransport sender, Exception error)
{
try
@@ -388,7 +389,7 @@ public Response Request(Command command, TimeSpan ts)
{
throw new ApplicationException("FailoverTransport does not implement Request(Command, TimeSpan)");
}

public void OnCommand(ITransport sender, Command command)
{
if(command != null)
@@ -426,7 +427,7 @@ public void OnCommand(ITransport sender, Command command)
public void Oneway(Command command)
{
Exception error = null;

lock(reconnectMutex)
{
if(IsShutdownCommand(command) && ConnectedTransport == null)
@@ -446,15 +447,15 @@ public void Oneway(Command command)
return;
}
}

// Keep trying until the message is sent.
for(int i = 0; !disposed; i++)
{
try
{
// 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 @@ public void Oneway(Command command)
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 @@ public void Reconnect(Uri uri)
{
Add(new Uri[] { uri });
}

public void Reconnect()
{
lock(reconnectMutex)
@@ -644,7 +645,7 @@ public void Reconnect()
{
reconnectTask.Wakeup();
}
catch(ThreadInterruptedException)
catch(Exception)
{
}
}
@@ -707,7 +708,7 @@ protected void RestoreTransport(ITransport t)
t.Oneway(command);
}
}

public Uri RemoteAddress
{
get
@@ -847,7 +848,7 @@ private bool DoConnect()
{
Thread.Sleep(ReconnectDelay);
}
catch(ThreadInterruptedException)
catch(Exception)
{
}
}
@@ -19,6 +19,7 @@
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 @@ public void WriteCheck(object unused)
{
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 @@ private void StopMonitorThreads()
{
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;
@@ -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
}
}
}
@@ -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>

0 comments on commit a5e241f

Please sign in to comment.