public void Subscribe <T>(string name, Action <T> handler) where T : class { IMessageBus messageBus = new RabbitMQMessageBus(new RabbitMQMessageBusOptions { ConnectionString = "amqp://192.168.180.128", Topic = "aaa", ExchangeName = name }); messageBus.SubscribeAsync <T>(handler); }
public static void Main(string[] args) { Console.WriteLine("Waiting to receive messages...."); IMessageBus messageBus = new RabbitMQMessageBus("amqp://localhost", "FoundatioQueue", "FoundatioExchange"); messageBus.SubscribeAsync <string>(msg => { Console.WriteLine(msg); }).GetAwaiter().GetResult(); Console.ReadLine(); }
public static async Task Main(string[] args) { Console.WriteLine("Waiting to receive messages, press enter to quit..."); var tasks = new List <Task>(); var messageBuses = new List <IMessageBus>(); for (int i = 0; i < 3; i++) { var messageBus = new RabbitMQMessageBus(new RabbitMQMessageBusOptions { ConnectionString = "amqp://localhost:5672" }); messageBuses.Add(messageBus); tasks.Add(messageBus.SubscribeAsync <MyMessage>(msg => { Console.WriteLine($"Got subscriber {messageBus.MessageBusId} message: {msg.Hey}"); })); } await Task.WhenAll(tasks); Console.ReadLine(); foreach (var messageBus in messageBuses) { messageBus.Dispose(); } }
public async Task CanPersistAndNotLoseMessages() { Log.MinimumLevel = LogLevel.Trace; var messageBus1 = new RabbitMQMessageBus(o => o .ConnectionString("amqp://localhost:5673") .LoggerFactory(Log) .SubscriptionQueueName($"{_topic}-offline") .IsSubscriptionQueueExclusive(false) .SubscriptionQueueAutoDelete(false) .AcknowledgementStrategy(AcknowledgementStrategy.Automatic)); var countdownEvent = new AsyncCountdownEvent(1); var cts = new CancellationTokenSource(); await messageBus1.SubscribeAsync <SimpleMessageA>(msg => { _logger.LogInformation("[Subscriber1] Got message: {Message}", msg.Data); countdownEvent.Signal(); }, cts.Token); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Audit message 1" }); await countdownEvent.WaitAsync(TimeSpan.FromSeconds(5)); Assert.Equal(0, countdownEvent.CurrentCount); cts.Cancel(); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Audit message 2" }); cts = new CancellationTokenSource(); countdownEvent.AddCount(1); await messageBus1.SubscribeAsync <SimpleMessageA>(msg => { _logger.LogInformation("[Subscriber2] Got message: {Message}", msg.Data); countdownEvent.Signal(); }, cts.Token); await countdownEvent.WaitAsync(TimeSpan.FromSeconds(5)); Assert.Equal(0, countdownEvent.CurrentCount); cts.Cancel(); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Audit offline message 1" }); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Audit offline message 2" }); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Audit offline message 3" }); messageBus1.Dispose(); var messageBus2 = new RabbitMQMessageBus(o => o .ConnectionString("amqp://localhost:5673") .LoggerFactory(Log) .SubscriptionQueueName($"{_topic}-offline") .IsSubscriptionQueueExclusive(false) .SubscriptionQueueAutoDelete(false) .AcknowledgementStrategy(AcknowledgementStrategy.Automatic)); cts = new CancellationTokenSource(); countdownEvent.AddCount(4); await messageBus2.SubscribeAsync <SimpleMessageA>(msg => { _logger.LogInformation("[Subscriber3] Got message: {Message}", msg.Data); countdownEvent.Signal(); }, cts.Token); await messageBus2.PublishAsync(new SimpleMessageA { Data = "Another audit message 4" }); await countdownEvent.WaitAsync(TimeSpan.FromSeconds(5)); Assert.Equal(0, countdownEvent.CurrentCount); messageBus2.Dispose(); }