Created
February 6, 2012 20:26
-
-
Save kellabyte/1754606 to your computer and use it in GitHub Desktop.
Reproducing MassTransit subscription timing bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* BUG SUMMARY | |
| * | |
| * Initializing 2 endpoints at the same time and immediately | |
| * publishing a message doesn't always arrive to the subscriber. | |
| * | |
| * In this sample the expected output is: | |
| * | |
| * Initializing CommandHandler... | |
| * Done | |
| * Initializing Client... | |
| * Done | |
| * [Handler] Received: 3e65482f-33ce-4f41-a022-1908f5a1522c | |
| * [Client] Received: 715f3284-5726-4d28-9ad3-5a31421b3c0f Correlation: 3e65482f-33ce-4f41-a022-1908f5a1522c | |
| * | |
| * Actual output is: | |
| * | |
| * Initializing CommandHandler... | |
| * Done | |
| * Initializing Client... | |
| * Done | |
| * | |
| * OR | |
| * | |
| * Initializing CommandHandler... | |
| * Done | |
| * Initializing Client... | |
| * Done | |
| * [Handler] Received: 3e65482f-33ce-4f41-a022-1908f5a1522c | |
| * | |
| * It seems like the subscriptions have timing issues because the | |
| * the messages don't arrive to the destination queue. | |
| * | |
| * Pressing enter allows you to send the command manually and proves | |
| * the communication is working. | |
| * | |
| */ | |
| using System; | |
| using MassTransit; | |
| namespace MassTransitSubscriberTest | |
| { | |
| class Program | |
| { | |
| private static MassTransitBus<TestCommand> commandHandler; | |
| private static MassTransitBus<TestEvent> client; | |
| static void Main(string[] args) | |
| { | |
| commandHandler = new MassTransitBus<TestCommand>("msmq://localhost/TestCommandHandler", "msmq://localhost/mt_subscriptions", OnMessage); | |
| client = new MassTransitBus<TestEvent>("msmq://localhost/TestClient", "msmq://localhost/mt_subscriptions", OnMessage); | |
| Console.WriteLine("Initializing CommandHandler..."); | |
| commandHandler.Open(); | |
| Console.WriteLine("Done"); | |
| Console.WriteLine("Initializing Client..."); | |
| client.Open(); | |
| Console.WriteLine("Done"); | |
| while (true) | |
| { | |
| client.Publish(new TestCommand("Testing...")); | |
| Console.ReadKey(); | |
| } | |
| } | |
| private static void OnMessage(TestCommand cmd) | |
| { | |
| Console.WriteLine(string.Format("[Handler]\tReceived: {0}", | |
| cmd.Id)); | |
| commandHandler.Publish(new TestEvent(cmd.Id, cmd.Text)); | |
| } | |
| private static void OnMessage(TestEvent evt) | |
| { | |
| Console.WriteLine(string.Format("[Client]\tReceived: {0}\tCorrelation: {1}", | |
| evt.Id, evt.CorrelationId)); | |
| } | |
| } | |
| public class MassTransitBus<TMessage> where TMessage : class | |
| { | |
| private IServiceBus bus; | |
| private readonly string queue; | |
| private readonly string subscriptionQueue; | |
| private readonly Action<TMessage> handler; | |
| public MassTransitBus(string queue, string subscriptionQueue, Action<TMessage> handler) | |
| { | |
| this.queue = queue; | |
| this.subscriptionQueue = subscriptionQueue; | |
| this.handler = handler; | |
| } | |
| public void Open() | |
| { | |
| if (bus != null) | |
| { | |
| return; | |
| } | |
| bus = ServiceBusFactory.New(sbc => | |
| { | |
| sbc.VerifyMsmqConfiguration(); | |
| sbc.UseMsmq(); | |
| sbc.UseControlBus(); | |
| sbc.ReceiveFrom(queue); | |
| sbc.UseSubscriptionService(subscriptionQueue); | |
| }); | |
| bus.SubscribeHandler<TMessage>(handler); | |
| } | |
| public void Publish<T>(T msg) where T : class | |
| { | |
| bus.Publish<T>(msg); | |
| } | |
| } | |
| public class TestCommand | |
| { | |
| public Guid Id { get; set; } | |
| public string Text { get; set; } | |
| public TestCommand() | |
| { | |
| } | |
| public TestCommand(string text) | |
| { | |
| this.Id = Guid.NewGuid(); | |
| this.Text = text; | |
| } | |
| } | |
| public class TestEvent | |
| { | |
| public Guid Id { get; set; } | |
| public Guid CorrelationId { get; set; } | |
| public string Text { get; set; } | |
| public TestEvent() | |
| { | |
| } | |
| public TestEvent(Guid correlationId, string text) | |
| { | |
| this.Id = Guid.NewGuid(); | |
| this.CorrelationId = correlationId; | |
| this.Text = text; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment