public virtual void SubscribeAsync <T>(string subscriptionId, Func <T, Task> onMessage, Action <ISubscriptionConfiguration <T> > configure) { Preconditions.CheckNotNull(subscriptionId, "subscriptionId"); Preconditions.CheckNotNull(onMessage, "onMessage"); var configuration = new SubscriptionConfiguration <T>(); configure(configuration); var queueName = GetQueueName <T>(subscriptionId); var exchangeName = GetExchangeName <T>(); var queue = Queue.DeclareDurable(queueName, configuration.Arguments); var exchange = Exchange.DeclareTopic(exchangeName); var topics = configuration.Topics.ToArray(); if (topics.Length == 0) { topics = new[] { "#" }; } queue.BindTo(exchange, topics); advancedBus.Subscribe <T>(queue, (message, messageRecievedInfo) => onMessage(message.Body)); }
public void SubscribeAsync <T>(string subscriptionId, Func <T, Task> onMessage, Action <ISubscriptionConfiguration <T> > configure) { if (subscriptionId == null) { throw new ArgumentNullException("subscriptionId"); } if (onMessage == null) { throw new ArgumentNullException("onMessage"); } var configuration = new SubscriptionConfiguration <T>(); configure(configuration); var queueName = GetQueueName <T>(subscriptionId); var exchangeName = GetExchangeName <T>(); var queue = Queue.DeclareDurable(queueName, configuration.Arguments); var exchange = Exchange.DeclareTopic(exchangeName); queue.BindTo(exchange, configuration.Topics.ToArray()); advancedBus.Subscribe <T>(queue, (message, messageRecievedInfo) => onMessage(message.Body)); }
public void Should_not_overwrite_correlation_id() { var autoResetEvent = new AutoResetEvent(false); const string expectedCorrelationId = "abc_foo"; var actualCorrelationId = ""; var queue = EasyNetQ.Topology.Queue.DeclareDurable("myqueue"); var exchange = EasyNetQ.Topology.Exchange.DeclareDirect("myexchange"); queue.BindTo(exchange, "#"); bus.Subscribe <MyMessage>(queue, (message, info) => Task.Factory.StartNew(() => { actualCorrelationId = message.Properties.CorrelationId; autoResetEvent.Set(); })); var messageToSend = new Message <MyMessage>(new MyMessage()); messageToSend.Properties.CorrelationId = expectedCorrelationId; using (var channel = bus.OpenPublishChannel()) { channel.Publish(exchange, "abc", messageToSend); } autoResetEvent.WaitOne(1000); actualCorrelationId.ShouldEqual(expectedCorrelationId); }
/// <summary> /// Binds the Admin Message Queue /// Starts listening to the incoming Admin Level messages /// </summary> private void SubscribeAdminMessageQueues() { try { if (Logger.IsInfoEnabled) { Logger.Info("Binding Admin Message Queue: " + _adminMessageQueue, _type.FullName, "SubscribeAdminMessageQueues"); } // Listening to Login Messages _advancedBus.Subscribe <string>( _adminMessageQueue, (msg, messageReceivedInfo) => Task.Factory.StartNew( () => { if (msg.Body.Contains("Logon")) { if (Logger.IsDebugEnabled) { Logger.Debug("Login message recieved: " + msg.Body, _type.FullName, "SubscribeAdminMessageQueues"); } if (LogonArrived != null) { LogonArrived(msg.Body); } } else if (msg.Body.Contains("Logout")) { if (Logger.IsDebugEnabled) { Logger.Debug("Logout Message recieved: " + msg.Body, _type.FullName, "SubscribeAdminMessageQueues"); } if (LogoutArrived != null) { LogoutArrived(msg.Body); } } })); } catch (Exception exception) { Logger.Error(exception, _type.FullName, "SubscribeAdminMessageQueues"); } }
public void Should_not_throw_on_bus_reconnect() { var queue = Queue.DeclareTransient(); bus.Subscribe <MyMessage>(queue, (message, info) => Task.Factory.StartNew(() => Console.WriteLine("Got message: {0}", message.Body.Text))); // now, force close the connection Thread.Sleep(TimeSpan.FromMinutes(2)); }
public void Should_be_able_to_do_a_simple_publish_and_subscribe() { const string routingKey = "advanced_test_routing_key"; var autoResetEvent = new AutoResetEvent(false); var exchange = Exchange.DeclareDirect("advanced_test_exchange"); var queue = Queue.DeclareDurable("advanced_test_queue"); queue.BindTo(exchange, routingKey); advancedBus.Subscribe <MyMessage>(queue, (msg, messageReceivedInfo) => Task.Factory.StartNew(() => { Console.WriteLine("Got Message: {0}", msg.Body.Text); Console.WriteLine("ConsumerTag: {0}", messageReceivedInfo.ConsumerTag); Console.WriteLine("DeliverTag: {0}", messageReceivedInfo.DeliverTag); Console.WriteLine("Redelivered: {0}", messageReceivedInfo.Redelivered); Console.WriteLine("Exchange: {0}", messageReceivedInfo.Exchange); Console.WriteLine("RoutingKey: {0}", messageReceivedInfo.RoutingKey); autoResetEvent.Set(); })); var myMessage = new MyMessage { Text = "Hello from the publisher" }; var message = new Message <MyMessage>(myMessage); message.Properties.AppId = "my_app_id"; message.Properties.ReplyTo = "my_reply_queue"; using (var channel = advancedBus.OpenPublishChannel()) { channel.Publish(exchange, routingKey, message); } // give the test time to complete autoResetEvent.WaitOne(1000); }
private string SubscribeToResponse <TResponse>(Action <TResponse> onResponse, IDictionary <string, object> arguments) { var queue = Queue.DeclareTransient("easynetq.response." + Guid.NewGuid().ToString(), arguments).SetAsSingleUse(); advancedBus.Subscribe <TResponse>(queue, (message, messageRecievedInfo) => { var tcs = new TaskCompletionSource <object>(); try { onResponse(message.Body); tcs.SetResult(null); } catch (Exception exception) { tcs.SetException(exception); } return(tcs.Task); }); return(queue.Name); }