Permalink
Browse files

added spec demonstrating how to use custom message builder

  • Loading branch information...
1 parent 7b906b1 commit 8a6e3af788b4645afb1bff1e746bd7c1a0fc5a3d mnichols committed Jul 5, 2010
@@ -106,6 +106,7 @@
</Compile>
<Compile Include="BugWithLogTest.cs" />
<Compile Include="BusSubscriptionTests.cs" />
+ <Compile Include="RhinoQueues\CanAddHeadersToMessage.cs" />
<Compile Include="CanRouteMessageToConsumerThroughContainer.cs" />
<Compile Include="CanSendMsgsFromOneWayBus.cs" />
<Compile Include="CanSendMsgsFromOneWayBusUsingRhinoQueues.cs" />
@@ -0,0 +1,70 @@
+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();
+ }
+ }
+}
@@ -67,11 +67,7 @@ public void Initialize(Endpoint source)
this.endpoint = source;
}
- public void Contextualize(Message message)
- {
-
- }
-
+
protected static int GetAppSpecificMarker(object[] msgs)
{
var msg = msgs[0];
@@ -30,8 +30,7 @@ public MessagePayload BuildFromMessageBatch(params object[] msgs)
data = memoryStream.ToArray();
}
-
- return new MessagePayload
+ var payload=new MessagePayload
{
Data = data,
Headers =
@@ -41,8 +40,9 @@ public MessagePayload BuildFromMessageBatch(params object[] msgs)
{"source", endpoint.Uri.ToString()},
}
};
+ return payload;
}
-
+
public void Initialize(Endpoint source)
{
endpoint = source;
@@ -11,13 +11,13 @@ namespace Rhino.ServiceBus.RhinoQueues
public class RhinoQueuesOneWayBus : RhinoQueuesTransport,IOnewayBus
{
private MessageOwnersSelector messageOwners;
- private static readonly Uri NullEndpoint = new Uri("null://nowhere:24689/middle");
+ public static readonly Uri NullEndpoint = new Uri("null://nowhere:24689/middle");
public RhinoQueuesOneWayBus(MessageOwner[] messageOwners, IMessageSerializer messageSerializer,IMessageBuilder<MessagePayload> messageBuilder)
: base(NullEndpoint, new EndpointRouter(), messageSerializer, 1, Path.Combine(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory), "one_way.esent"), IsolationLevel.ReadCommitted,5,messageBuilder)
{
this.messageOwners = new MessageOwnersSelector(messageOwners, new EndpointRouter());
- base.Start();
+ Start();
}
public void Send(params object[] msgs)

0 comments on commit 8a6e3af

Please sign in to comment.