Пример #1
0
 public SubscriptionInfo(SubscriptionAction subscriptionAction, EasyNetQConsumer consumer, MessageCallback callback, bool modelIsSingleUse, IModel channel)
 {
     SubscriptionAction = subscriptionAction;
     Consumer           = consumer;
     Callback           = callback;
     ModelIsSingleUse   = modelIsSingleUse;
     Channel            = channel;
 }
Пример #2
0
        public virtual void Subscribe(IQueue queue, Func <Byte[], MessageProperties, MessageReceivedInfo, Task> onMessage)
        {
            if (queue == null)
            {
                throw new ArgumentNullException("queue");
            }
            if (onMessage == null)
            {
                throw new ArgumentNullException("onMessage");
            }
            if (disposed)
            {
                throw new EasyNetQException("This bus has been disposed");
            }

            var subscriptionAction = new SubscriptionAction(queue.IsSingleUse);

            subscriptionAction.Action = () =>
            {
                var channel = connection.CreateModel();
                channel.ModelShutdown += (model, reason) => logger.DebugWrite("Model Shutdown for queue: '{0}'", queue.Name);

                queue.Visit(new TopologyBuilder(channel));

                channel.BasicQos(0, connectionConfiguration.PrefetchCount, 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
                    };
                    var messsageProperties = new MessageProperties(properties);
                    return(onMessage(body, messsageProperties, messageRecievedInfo));
                });

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

                logger.DebugWrite("Declared Consumer. queue='{0}', prefetchcount={1}",
                                  queue.Name,
                                  connectionConfiguration.PrefetchCount);
            };

            AddSubscriptionAction(subscriptionAction);
        }
Пример #3
0
        private void AddSubscriptionAction(SubscriptionAction subscriptionAction)
        {
            if (!subscriptionAction.IsExclusive)
            {
                if (!subscribeActions.TryAdd(subscriptionAction.Id, subscriptionAction))
                {
                    throw new EasyNetQException("Failed to store subscription action");
                }
            }

            subscriptionAction.ExecuteAction(true);
        }
        public DefaultBasicConsumer CreateConsumer(
            SubscriptionAction subscriptionAction,
            IModel model,
            bool modelIsSingleUse,
            MessageCallback callback)
        {
            var consumer    = new EasyNetQConsumer(model, queue);
            var consumerTag = Guid.NewGuid().ToString();

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

            return(consumer);
        }
Пример #5
0
        public DefaultBasicConsumer CreateConsumer(
            SubscriptionAction subscriptionAction,
            IModel model,
            bool modelIsSingleUse,
            MessageCallback callback)
        {
            var consumer = new EasyNetQConsumer(model, queue)
            {
                ConsumerTag = subscriptionAction.Id
            };

            if (subscriptions.ContainsKey(consumer.ConsumerTag))
            {
                logger.DebugWrite("Removing existing subscription with ConsumerTag: " + consumer.ConsumerTag);
                subscriptions.Remove(consumer.ConsumerTag);
            }

            subscriptions.Add(consumer.ConsumerTag, new SubscriptionInfo(subscriptionAction, consumer, callback, modelIsSingleUse, model));
            return(consumer);
        }
Пример #6
0
        private void AddSubscriptionAction(SubscriptionAction subscriptionAction)
        {
            if (subscriptionAction.IsMultiUse)
            {
                subscribeActions.Add(subscriptionAction);
            }

            try
            {
                subscriptionAction.Action();
            }
            catch (OperationInterruptedException)
            {
            }
            catch (EasyNetQException)
            {
                // 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.
            }
        }
Пример #7
0
        public virtual void Consume(IQueue queue, Func <Byte[], MessageProperties, MessageReceivedInfo, Task> onMessage)
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");

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

            var newConsumerTag     = conventions.ConsumerTagConvention();
            var subscriptionAction = new SubscriptionAction(newConsumerTag, logger, queue.IsSingleUse, queue.IsExclusive);

            subscriptionAction.Action = (isNewConnection) =>
            {
                // recreate channel if current channel is no longer open or connection was dropped and reconnected (to survive server restart)
                if (subscriptionAction.Channel == null || subscriptionAction.Channel.IsOpen == false || isNewConnection)
                {
                    subscriptionAction.Channel = CreateChannel(queue);
                }

                var channel = subscriptionAction.Channel;

                channel.BasicQos(0, connectionConfiguration.PrefetchCount, 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
                    };
                    var messsageProperties = new MessageProperties(properties);
                    return(onMessage(body, messsageProperties, messageRecievedInfo));
                });

                var cancelNotifications = consumer as IConsumerCancelNotifications;
                if (cancelNotifications != null)
                {
                    cancelNotifications.BasicCancel += OnBasicCancel;
                }

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

                logger.DebugWrite("Declared Consumer. queue='{0}', consumer tag='{1}' prefetchcount={2}",
                                  queue.Name,
                                  consumer.ConsumerTag,
                                  connectionConfiguration.PrefetchCount);
            };



            AddSubscriptionAction(subscriptionAction);
        }