Skip to content

Commit 3aaed7e

Browse files
committed
Add support for auto-retrying after exception in main RunLoop thread
1 parent 9d8e79e commit 3aaed7e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ServiceStack.Redis/Messaging/RedisMqServer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class RedisMqServer : IMessageService
3535

3636
public int RetryCount { get; protected set; }
3737

38+
public int? KeepAliveRetryAfterMs { get; set; }
39+
3840
public IMessageFactory MessageFactory { get; private set; }
3941

4042
public Func<string, IOneWayClient> ReplyClientFactory { get; set; }
@@ -111,6 +113,7 @@ public RedisMqServer(IRedisClientsManager clientsManager,
111113
//this.RequestTimeOut = requestTimeOut;
112114
this.MessageFactory = new RedisMessageFactory(clientsManager);
113115
this.ErrorHandler = ex => Log.Error("Exception in Redis MQ Server: " + ex.Message, ex);
116+
this.KeepAliveRetryAfterMs = 2000;
114117
}
115118

116119
public void RegisterHandler<T>(Func<IMessage<T>, object> processMessageFn)
@@ -314,6 +317,13 @@ private void RunLoop()
314317

315318
if (this.ErrorHandler != null)
316319
this.ErrorHandler(ex);
320+
321+
322+
if (KeepAliveRetryAfterMs != null)
323+
{
324+
Thread.Sleep(KeepAliveRetryAfterMs.Value);
325+
Start();
326+
}
317327
}
318328
}
319329

src/TestMqHost/Program.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Program
1919
static void Main(string[] args)
2020
{
2121

22-
LogManager.LogFactory = new ConsoleLogFactory();
22+
//LogManager.LogFactory = new ConsoleLogFactory();
2323
var log = LogManager.GetLogger(typeof(Program));
2424

2525
var clientManager = new PooledRedisClientManager(new[] { "localhost" })
@@ -29,12 +29,14 @@ static void Main(string[] args)
2929

3030
var mqHost = new RedisMqServer(clientManager, retryCount: 2);
3131

32+
var msgsProcessed = 0;
3233
var sum = 0;
3334
mqHost.RegisterHandler<Incr>(c =>
3435
{
3536
var dto = c.GetBody();
3637
sum += dto.Value;
3738
log.InfoFormat("Received {0}, sum: {1}", dto.Value, sum);
39+
msgsProcessed++;
3840
return null;
3941
});
4042

@@ -100,6 +102,10 @@ static void Main(string[] args)
100102

101103
Thread.Sleep(1000);
102104
});
105+
106+
Thread.Sleep(2000);
107+
"Messages processed: {0}".Print(msgsProcessed);
108+
Console.ReadKey();
103109
}
104110
}
105111
}

0 commit comments

Comments
 (0)