Permalink
Browse files

Headers are now allowed to be changed on the current message in order to

support deferred processing. Closes #232
  • Loading branch information...
1 parent 23ff632 commit 0a429017079d133fec9c23b05aed5cf248b50ac2 @andreasohlund andreasohlund committed Mar 22, 2012
@@ -1,9 +1,10 @@
namespace MyServer.DeferedProcessing
{
+ using System;
using NServiceBus;
public class DeferredMessage:IMessage
{
-
+ public DateTime ProcessAt { get; set; }
}
}
@@ -5,9 +5,24 @@ namespace MyServer.DeferedProcessing
public class DeferredMessageHandler : IHandleMessages<DeferredMessage>
{
+ public IBus Bus { get; set; }
+
public void Handle(DeferredMessage message)
{
- Console.WriteLine(string.Format("{0} - {1}", DateTime.Now.ToLongTimeString(), "Deferred message processed"));
+ if(DateTime.Now < message.ProcessAt)
+ {
+ LogMessage("Message will be processed at " + message.ProcessAt.ToLongTimeString());
+
+ Bus.Defer(message.ProcessAt, message);
+ return;
+ }
+
+ LogMessage("Deferred message processed");
+ }
+
+ static void LogMessage(string message)
+ {
+ Console.WriteLine(string.Format("{0} - {1}", DateTime.Now.ToLongTimeString(), message));
}
}
}
@@ -40,8 +40,11 @@ public void Run()
void DeferMessage()
{
- Bus.Defer(TimeSpan.FromSeconds(10), new DeferredMessage());
- Console.WriteLine(string.Format("{0} - {1}", DateTime.Now.ToLongTimeString(), "Sent a message that is deferred for 10 seconds"));
+ Console.WriteLine(string.Format("{0} - {1}", DateTime.Now.ToLongTimeString(), "Sending a message to be processed at a later time"));
+ Bus.SendLocal(new DeferredMessage
+ {
+ ProcessAt = DateTime.Now.AddSeconds(10)
+ });
}
void StartSaga(string tennant = "")
@@ -59,9 +59,6 @@ public void SetHeader(object message, string key, string value)
if (message == null)
throw new InvalidOperationException("Cannot set headers on a null object");
- if (message == ExtensionMethods.CurrentMessageBeingHandled)
- throw new InvalidOperationException("Cannot change headers on the message being processed.");
-
if (messageHeaders == null)
messageHeaders = new Dictionary<object, IDictionary<string, string>>();

0 comments on commit 0a42901

Please sign in to comment.