Inheritance: IEnvelopeDispatcher
Beispiel #1
0
 protected virtual void Raise_OnEnvelopeReceivedEvent(RabbitEnvelopeDispatcher dispatcher)
 {
     if (null != this.OnEnvelopeReceived)
     {
         Task.Factory.StartNew(() => this.OnEnvelopeReceived(dispatcher));
     }
 }
Beispiel #2
0
        public void Start(object manualResetEvent)
        {
            ManualResetEvent startEvent = manualResetEvent as ManualResetEvent;

            _log.Debug("Enter Start");
            _shouldContinue = true;

            using (IModel channel = _connection.CreateModel())
            {
                // first, declare the exchange and queue
                channel.ExchangeDeclare(_exchange.Name, _exchange.ExchangeType, _exchange.IsDurable, _exchange.IsAutoDelete, _exchange.Arguments);
                channel.QueueDeclare(_exchange.QueueName, _exchange.IsDurable, false, _exchange.IsAutoDelete, _exchange.Arguments);
                channel.QueueBind(_exchange.QueueName, _exchange.Name, _exchange.RoutingKey, _exchange.Arguments);

                // next, create a basic consumer
                QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);

                // and tell it to start consuming messages, storing the consumer tag
                string consumerTag = channel.BasicConsume(_exchange.QueueName, false, consumer);

                // signal the wait event that we've begun listening
                startEvent.Set();

                _log.Debug("Will now continuously listen for events using routing key: " + _exchange.RoutingKey);
                while (_shouldContinue)
                {
                    try
                    {
                        object result = null;

                        if (false == consumer.Queue.Dequeue(100, out result))
                        {
                            continue;
                        }
                        BasicDeliverEventArgs e = result as BasicDeliverEventArgs;
                        if (null == e)
                        {
                            continue;
                        }
                        else
                        {
                            this.LogMessage(e);
                        }

                        IBasicProperties props = e.BasicProperties;

                        Envelope env = new Envelope();
                        env.SetReceiptTime(DateTime.Now);
                        env.Payload = e.Body;
                        foreach (DictionaryEntry entry in props.Headers)
                        {
                            try
                            {
                                string key   = entry.Key as string;
                                string value = Encoding.UTF8.GetString((byte[])entry.Value);
                                _log.Debug("Adding header to envelope: {" + key + ":" + value + "}");

                                env.Headers.Add(key, value);
                            }
                            catch { }
                        }

                        if (this.ShouldRaiseEvent(_registration.Filter, env))
                        {
                            RabbitEnvelopeDispatcher dispatcher = new RabbitEnvelopeDispatcher(_registration, env, channel, e.DeliveryTag);
                            this.Raise_OnEnvelopeReceivedEvent(dispatcher);
                        }
                    }
                    catch (OperationInterruptedException)
                    {
                        // The consumer was removed, either through
                        // channel or connection closure, or through the
                        // action of IModel.BasicCancel().
                        _shouldContinue = false;
                    }
                    catch { }
                }
                _log.Debug("No longer listening for events");

                try { channel.BasicCancel(consumerTag); }
                catch (OperationInterruptedException) { }
            }

            _log.Debug("Leave Start");
        }
        public void Start(object manualResetEvent)
        {
            ManualResetEvent startEvent = manualResetEvent as ManualResetEvent;

            _log.Debug("Enter Start");
            _shouldContinue = true;

            using (IModel channel = _connection.CreateModel())
            {
                // first, declare the exchange and queue
                channel.ExchangeDeclare(_exchange.Name, _exchange.ExchangeType, _exchange.IsDurable, _exchange.IsAutoDelete, _exchange.Arguments);
                channel.QueueDeclare(_exchange.QueueName, _exchange.IsDurable, false, _exchange.IsAutoDelete, _exchange.Arguments);
                channel.QueueBind(_exchange.QueueName, _exchange.Name, _exchange.RoutingKey, _exchange.Arguments);

                // next, create a basic consumer
                QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);

                // and tell it to start consuming messages, storing the consumer tag
                string consumerTag = channel.BasicConsume(_exchange.QueueName, false, consumer);

                // signal the wait event that we've begun listening
                startEvent.Set();

                _log.Debug("Will now continuously listen for events using routing key: " + _exchange.RoutingKey);
                while (_shouldContinue)
                {
                    try
                    {
                        object result = null;

                        if (false == consumer.Queue.Dequeue(100, out result)) { continue; }
                        BasicDeliverEventArgs e = result as BasicDeliverEventArgs;
                        if (null == e) { continue; }
                        else { this.LogMessage(e); }

                        IBasicProperties props = e.BasicProperties;

                        Envelope env = new Envelope();
                        env.SetReceiptTime(DateTime.Now);
                        env.Payload = e.Body;
                        foreach (DictionaryEntry entry in props.Headers)
                        {
                            try
                            {
                                string key = entry.Key as string;
                                string value = Encoding.UTF8.GetString((byte[])entry.Value);
                                _log.Debug("Adding header to envelope: {" + key + ":" + value + "}");

                                env.Headers.Add(key, value);
                            }
                            catch { }
                        }

                        if (this.ShouldRaiseEvent(_registration.Filter, env))
                        {
                            RabbitEnvelopeDispatcher dispatcher = new RabbitEnvelopeDispatcher(_registration, env, channel, e.DeliveryTag);
                            this.Raise_OnEnvelopeReceivedEvent(dispatcher);
                        }
                    }
                    catch (OperationInterruptedException)
                    {
                        // The consumer was removed, either through
                        // channel or connection closure, or through the
                        // action of IModel.BasicCancel().
                        _shouldContinue = false;
                    }
                    catch { }
                }
                _log.Debug("No longer listening for events");

                try { channel.BasicCancel(consumerTag); }
                catch (OperationInterruptedException) { }
            }

            _log.Debug("Leave Start");
        }
 protected virtual void Raise_OnEnvelopeReceivedEvent(RabbitEnvelopeDispatcher dispatcher)
 {
     if (null != this.OnEnvelopeReceived)
     {
         Task.Factory.StartNew(() => this.OnEnvelopeReceived(dispatcher));
     }
 }