Ejemplo n.º 1
0
 public SubscriptionInfo(SubscriptionAction subscriptionAction, MessageBusConsumer consumer, MessageCallback callback, bool modelIsSingleUse, IModel channel)
 {
     SubscriptionAction = subscriptionAction;
     Consumer           = consumer;
     Callback           = callback;
     ModelIsSingleUse   = modelIsSingleUse;
     Channel            = channel;
 }
        public virtual void Subscribe(IQueue queue, Func <Byte[], MessageProperties, MessageReceivedInfo, Task> onMessage)
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");

            if (disposed)
            {
                throw new Exception("This bus has been disposed");
            }

            var subscriptionAction = new SubscriptionAction(queue.IsSingleUse);

            subscriptionAction.Action = () =>
            {
                var channel = PersistentConnection.CreateModel();
                channel.ModelShutdown += (model, reason) => Logger.Debug(string.Format("Model Shutdown for queue: '{0}'", queue.Name));

                queue.Visit(new TopologyBuilder(channel));

                //channel.BasicQos(0, connectionConfiguration.PrefetchCount, false);
                channel.BasicQos(0, 50, false);

                var consumer = ConsumerFactory.CreateConsumer(subscriptionAction, channel, queue.IsSingleUse,
                                                              (consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body) =>
                {
                    var messageRecievedInfo = new MessageReceivedInfo
                    {
                        ConsumerTag = consumerTag,
                        DeliverTag  = deliveryTag,
                        Redelivered = redelivered,
                        Exchange    = exchange,
                        RoutingKey  = routingKey
                    };
                    MessageProperties messageProperties = new MessageProperties();
                    properties.CopyTo(messageProperties);
                    return(onMessage(body, messageProperties, messageRecievedInfo));
                });

                channel.BasicConsume(
                    queue.Name,             // queue
                    NoAck,                  // noAck
                    consumer.ConsumerTag,   // consumerTag
                    consumer);              // consumer

                Logger.Debug(string.Format("Declared Consumer. queue='{0}', prefetchcount={1}",
                                           queue.Name,
                                           _connectionConfiguration.PrefetchCount));
            };

            AddSubscriptionAction(subscriptionAction);
        }
Ejemplo n.º 3
0
        public DefaultBasicConsumer CreateConsumer(
            SubscriptionAction subscriptionAction,
            IModel model,
            bool modelIsSingleUse,
            MessageCallback callback)
        {
            var consumer    = new MessageBusConsumer(model, queue);
            var consumerTag = Guid.NewGuid().ToString();

            consumer.ConsumerTag = consumerTag;
            subscriptions.Add(consumerTag, new SubscriptionInfo(subscriptionAction, consumer, callback, modelIsSingleUse, model));

            return(consumer);
        }
        private void AddSubscriptionAction(SubscriptionAction subscriptionAction)
        {
            if (subscriptionAction.IsMultiUse)
            {
                _subscribeActions.Add(subscriptionAction);
            }

            try
            {
                subscriptionAction.Action();
            }
            catch (OperationInterruptedException)
            {
            }
            catch (Exception)
            {
                // Looks like the channel closed between our IsConnected check
                // and the subscription action. Do nothing here, when the
                // connection comes back, the subcription action will be run then.
            }
        }
 public SubscriptionInfo(SubscriptionAction subscriptionAction, MessageBusConsumer consumer, MessageCallback callback, bool modelIsSingleUse, IModel channel)
 {
     SubscriptionAction = subscriptionAction;
     Consumer = consumer;
     Callback = callback;
     ModelIsSingleUse = modelIsSingleUse;
     Channel = channel;
 }
        public DefaultBasicConsumer CreateConsumer(
            SubscriptionAction subscriptionAction,
            IModel model,
            bool modelIsSingleUse,
            MessageCallback callback)
        {
            var consumer = new MessageBusConsumer(model, queue);
            var consumerTag = Guid.NewGuid().ToString();
            consumer.ConsumerTag = consumerTag;
            subscriptions.Add(consumerTag, new SubscriptionInfo(subscriptionAction, consumer, callback, modelIsSingleUse, model));

            return consumer;
        }