Created
February 3, 2015 23:51
-
-
Save phatboyg/3f72359c017ccef21449 to your computer and use it in GitHub Desktop.
In Order Message Test for RabbitMQ
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
namespace ConsoleApplication3 | |
{ | |
using System; | |
using MassTransit; | |
class Program | |
{ | |
const int limit = 1000; | |
static int lastIndex = 0; | |
static void Main() | |
{ | |
using (IServiceBus consumerBus = CreateConsumerBus()) | |
{ | |
using (IServiceBus producerBus = CreateProducerBus()) | |
{ | |
IEndpoint endpoint = producerBus.GetEndpoint(new Uri("rabbitmq://localhost/fifo_consumer")); | |
for (int i = 1; i <= limit; i++) | |
endpoint.Send(new InOrderMessage() {Index = i}); | |
Console.ReadLine(); | |
} | |
} | |
} | |
static IServiceBus CreateConsumerBus() | |
{ | |
return ServiceBusFactory.New(x => | |
{ | |
x.UseRabbitMq(); | |
x.ReceiveFrom("rabbitmq://localhost/fifo_consumer"); | |
x.SetConcurrentConsumerLimit(1); | |
x.Subscribe(s => s.Handler<InOrderMessage>(HandleMessage)); | |
}); | |
} | |
static IServiceBus CreateProducerBus() | |
{ | |
return ServiceBusFactory.New(x => | |
{ | |
x.UseRabbitMq(); | |
x.ReceiveFrom("rabbitmq://localhost/fifo_producer"); | |
}); | |
} | |
static void HandleMessage(IConsumeContext<InOrderMessage> context, InOrderMessage message) | |
{ | |
if (message.Index != lastIndex + 1) | |
Console.WriteLine("\rOut of order message received: {0}", message.Index); | |
lastIndex = message.Index; | |
if ((lastIndex & 100) == 0) | |
Console.Write("."); | |
if (lastIndex == limit) | |
Console.WriteLine("Done"); | |
} | |
} | |
public class InOrderMessage | |
{ | |
public int Index { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment