Permalink
Browse files

Merge branch 'simpler-headers-interface'

  • Loading branch information...
2 parents 7a1acba + 47a6329 commit 8ff525d7b819902fa2649680d6a21e1410785c93 mnichols committed Jul 5, 2010
@@ -106,7 +106,7 @@
</Compile>
<Compile Include="BugWithLogTest.cs" />
<Compile Include="BusSubscriptionTests.cs" />
- <Compile Include="RhinoQueues\CanAddHeadersToMessage.cs" />
+ <Compile Include="RhinoQueues\CustomizingMessageConstruction.cs" />
<Compile Include="CanRouteMessageToConsumerThroughContainer.cs" />
<Compile Include="CanSendMsgsFromOneWayBus.cs" />
<Compile Include="CanSendMsgsFromOneWayBusUsingRhinoQueues.cs" />
@@ -1,70 +0,0 @@
-using System;
-using Castle.MicroKernel.Registration;
-using Castle.Windsor;
-using Rhino.Queues;
-using Rhino.ServiceBus.Impl;
-using Rhino.ServiceBus.RhinoQueues;
-using Xunit;
-
-namespace Rhino.ServiceBus.Tests.RhinoQueues
-{
- public class CanAddHeadersToMessage:IDisposable
- {
- private WindsorContainer container;
-
- public CanAddHeadersToMessage()
- {
- container = new WindsorContainer("RhinoQueues/RhinoQueues.config");
-
- container.Register(Component.For<IMessageBuilder<MessagePayload>>().ImplementedBy<CustomHeaderBuilder>());
- container.AddFacility("rhino.esb", new RhinoServiceBusFacility());
-
-
- }
-
-
- [Fact]
- public void it_should_add_custom_header_to_headers_collection()
- {
- var builder = container.Resolve<IMessageBuilder<MessagePayload>>();
- builder.Initialize(new Endpoint {Uri = RhinoQueuesOneWayBus.NullEndpoint});
- var msg = builder.BuildFromMessageBatch("somemsg");
- Assert.NotNull(msg);
- Assert.NotEqual(0,msg.Data.Length);
- Assert.Equal("mikey",msg.Headers["user-id"]);
-
- }
-
- public class CustomHeaderBuilder : IMessageBuilder<MessagePayload>
- {
- private IMessageBuilder<MessagePayload> inner;
-
- public CustomHeaderBuilder(IMessageBuilder<MessagePayload> inner)
- {
- this.inner = inner;
- }
-
- public MessagePayload BuildFromMessageBatch(params object[] msgs)
- {
- var payload = inner.BuildFromMessageBatch(msgs);
- Contextualize(payload);
- return payload;
- }
-
- public void Initialize(Endpoint source)
- {
- inner.Initialize(source);
- }
-
- private static void Contextualize(MessagePayload message)
- {
- message.Headers.Add("user-id","mikey");
- }
- }
-
- public void Dispose()
- {
- container.Dispose();
- }
- }
-}
@@ -0,0 +1,90 @@
+using System;
+using System.Collections.Specialized;
+using Castle.Core;
+using Castle.MicroKernel.Registration;
+using Castle.Windsor;
+using Rhino.Queues;
+using Rhino.ServiceBus.Impl;
+using Rhino.ServiceBus.Internal;
+using Rhino.ServiceBus.RhinoQueues;
+using Xunit;
+
+namespace Rhino.ServiceBus.Tests.RhinoQueues
+{
+ public class CustomizingMessageConstruction
+ {
+
+ [Fact]
+ public void it_should_add_custom_header_to_headers_collection_using_builder()
+ {
+ using( var container = new WindsorContainer("RhinoQueues/RhinoQueues.config"))
+ {
+ container.Register(Component.For<IMessageBuilder<MessagePayload>>().ImplementedBy<CustomHeaderBuilder>());//before facility
+ container.AddFacility("rhino.esb", new RhinoServiceBusFacility());
+
+ var builder = container.Resolve<IMessageBuilder<MessagePayload>>();
+ builder.Initialize(new Endpoint { Uri = RhinoQueuesOneWayBus.NullEndpoint });
+ var msg = builder.BuildFromMessageBatch("somemsg");
+ Assert.NotNull(msg);
+ Assert.NotEqual(0, msg.Data.Length);
+ Assert.Equal("mikey", msg.Headers["user-id"]);
+ }
+
+ }
+
+ [Fact]
+ public void it_should_add_custom_header_to_headers_collection_using_interface()
+ {
+ using (var container = new WindsorContainer("RhinoQueues/RhinoQueues.config"))
+ {
+ container.AddFacility("rhino.esb", new RhinoServiceBusFacility());
+ container.Register(Component.For<ICustomizeMessageHeaders>().ImplementedBy<AppIdentityCustomizer>().LifeStyle.Is(LifestyleType.Transient));
+
+ var builder = container.Resolve<IMessageBuilder<MessagePayload>>();
+ builder.Initialize(new Endpoint { Uri = RhinoQueuesOneWayBus.NullEndpoint });
+ var msg = builder.BuildFromMessageBatch("somemsg");
+ Assert.NotNull(msg);
+ Assert.NotEqual(0, msg.Data.Length);
+ Assert.Equal("mikey", msg.Headers["user-id"]);
+ }
+
+ }
+
+ public class CustomHeaderBuilder : IMessageBuilder<MessagePayload>
+ {
+ private IMessageBuilder<MessagePayload> inner;
+
+ public CustomHeaderBuilder(IMessageBuilder<MessagePayload> inner)
+ {
+ this.inner = inner;
+ }
+
+ public MessagePayload BuildFromMessageBatch(params object[] msgs)
+ {
+ var payload = inner.BuildFromMessageBatch(msgs);
+ Contextualize(payload);
+ return payload;
+ }
+
+ public void Initialize(Endpoint source)
+ {
+ inner.Initialize(source);
+ }
+
+ private static void Contextualize(MessagePayload message)
+ {
+ message.Headers.Add("user-id","mikey");
+ }
+ }
+ public class AppIdentityCustomizer : ICustomizeMessageHeaders
+ {
+ public void Customize(NameValueCollection headers)
+ {
+ headers.Add("user-id","mikey");
+ }
+ }
+
+ }
+
+
+}
@@ -0,0 +1,9 @@
+using System.Collections.Specialized;
+
+namespace Rhino.ServiceBus
+{
+ public interface ICustomizeMessageHeaders
+ {
+ void Customize(NameValueCollection headers);
+ }
+}
@@ -1,4 +1,4 @@
-namespace Rhino.ServiceBus
+namespace Rhino.ServiceBus.Internal
{
public interface IMessageBuilder<T>
{
@@ -1,5 +1,6 @@
using System.Messaging;
using Rhino.ServiceBus.Impl;
+using Rhino.ServiceBus.Internal;
namespace Rhino.ServiceBus.Msmq
{
@@ -97,6 +97,7 @@
<Compile Include="Hosting\DefaultHost.cs" />
<Compile Include="Hosting\RemoteAppDomainHost.cs" />
<Compile Include="Hosting\RemoteAppDomainLoadBalancerHost.cs" />
+ <Compile Include="ICustomizeMessageHeaders.cs" />
<Compile Include="Impl\EndpointRouter.cs" />
<Compile Include="Endpoint.cs" />
<Compile Include="Internal\IEndpointRouter.cs" />
@@ -217,7 +218,7 @@
<Compile Include="Messages\QueryReadyForWorkQueueUri.cs" />
<Compile Include="Messages\Reroute.cs" />
<Compile Include="Msmq\EndpointExtensions.cs" />
- <Compile Include="IMessageBuilder.cs" />
+ <Compile Include="Internal\IMessageBuilder.cs" />
<Compile Include="Msmq\MsmqMessageBuilder.cs" />
<Compile Include="Transport\MessageHandlingCompletion.cs" />
<Compile Include="Msmq\OpenedQueue.cs" />
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Specialized;
using System.IO;
using Rhino.Queues;
using Rhino.ServiceBus.Internal;
@@ -40,9 +41,19 @@ public MessagePayload BuildFromMessageBatch(params object[] msgs)
{"source", endpoint.Uri.ToString()},
}
};
+
+ TryCustomizeHeaders(payload.Headers);
return payload;
}
-
+
+ private void TryCustomizeHeaders(NameValueCollection headers)
+ {
+ if (MessageHeaders == null)
+ return;
+ MessageHeaders.Customize(headers);
+ }
+
+ public ICustomizeMessageHeaders MessageHeaders { get; set; }
public void Initialize(Endpoint source)
{
endpoint = source;

0 comments on commit 8ff525d

Please sign in to comment.