Example #1
0
        public void Subscribe <TEvent>(string subscriptionId, Action <TEvent> handler, string topic = "#")
        {
            Preconditions.Require(subscriptionId, "subscriptionId");
            Preconditions.Require(topic, "topic");

            var messageTypeUri = registry.GetUri <TEvent>();

            var exchangeName = messageTypeUri.ToExchangeName();

            var routingKey = exchangeName + "." + subscriptionId;

            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(exchangeName, "topic", true);
                channel.QueueDeclare(routingKey, true, false, false, null);
                channel.QueueBind(routingKey, exchangeName, topic);
            }

            var qs = new QueueSubscriber(routingKey,
                                         registry,
                                         o => handler((dynamic)o));

            qs.StartConsuming(connection);

            queueSubscribers.Add(routingKey, qs);
        }
Example #2
0
        public void Receive <TCommand>(string queue, Action <TCommand> handler)
        {
            Preconditions.Require(queue, "queue");
            Preconditions.Require(handler, "handler");

            if (queueSubscribers.ContainsKey(queue) == false)
            {
                var qs = new QueueSubscriber(queue, registry, o => commandQueue.Add(o));
                qs.StartConsuming(connection);

                queueSubscribers.Add(queue, qs);
            }

            commandHandlers.Add(typeof(TCommand), o => handler((dynamic)o));
        }