コード例 #1
0
        public void Receive(Func <IReceiveContext, Action <IReceiveContext> > callback, TimeSpan timeout)
        {
            AddManagementBinding();

            AddReceiverBinding();

            _connectionHandler.Use(connection =>
            {
                BrokeredMessage message = _receiver.Get(timeout);

                if (message == null)
                {
                    return;
                }

                using (var body = new MemoryStream(message.GetBody <MessageEnvelope>().ActualBody, false))
                {
                    ReceiveContext context = ReceiveContext.FromBodyStream(body);
                    context.SetMessageId(message.MessageId);
                    context.SetInputAddress(Address);
                    context.SetCorrelationId(message.CorrelationId);
                    context.SetContentType(message.ContentType);

                    if (_logger.IsDebugEnabled)
                    {
                        TraceMessage(context);
                    }

                    Action <IReceiveContext> receive = callback(context);
                    if (receive == null)
                    {
                        Address.LogSkipped(message.MessageId);
                        return;
                    }

                    receive(context);

                    try
                    {
                        message.Complete();
                    }
                    catch (MessageLockLostException ex)
                    {
                        if (_logger.IsErrorEnabled)
                        {
                            _logger.Error("Message Lock Lost on message Complete()", ex);
                        }
                    }
                    catch (MessagingException ex)
                    {
                        if (_logger.IsErrorEnabled)
                        {
                            _logger.Error("Generic MessagingException thrown", ex);
                        }
                    }
                }
            });
        }
コード例 #2
0
        public void Receive(Func <IReceiveContext, Action <IReceiveContext> > lookupSinkChain, TimeSpan timeout)
        {
            AddConsumerBinding();

            _connectionHandler.Use(connection =>
            {
                BasicGetResult result = null;
                try
                {
                    result = _consumer.Get();
                    if (result == null)
                    {
                        Thread.Sleep(10);
                        return;
                    }

                    using (var body = new MemoryStream(result.Body, false))
                    {
                        ReceiveContext context = ReceiveContext.FromBodyStream(body);
                        context.SetMessageId(result.BasicProperties.MessageId ?? result.DeliveryTag.ToString());
                        result.BasicProperties.MessageId = context.MessageId;
                        context.SetInputAddress(_address);

                        byte[] contentType = result.BasicProperties.IsHeadersPresent()
                                                     ? (byte[])result.BasicProperties.Headers["Content-Type"]
                                                     : null;
                        if (contentType != null)
                        {
                            context.SetContentType(Encoding.UTF8.GetString(contentType));
                        }

                        Action <IReceiveContext> receive = lookupSinkChain(context);
                        if (receive == null)
                        {
                            Address.LogSkipped(result.BasicProperties.MessageId);

                            _consumer.MessageSkipped(result);
                        }
                        else
                        {
                            receive(context);

                            _consumer.MessageCompleted(result);
                        }
                    }
                }
                catch (AlreadyClosedException ex)
                {
                    throw new InvalidConnectionException(_address.Uri, "Connection was already closed", ex);
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidConnectionException(_address.Uri, "Connection was closed", ex);
                }
                catch (OperationInterruptedException ex)
                {
                    throw new InvalidConnectionException(_address.Uri, "Operation was interrupted", ex);
                }
                catch (Exception ex)
                {
                    _log.Error("Failed to consume message from endpoint", ex);

                    if (result != null)
                    {
                        _consumer.MessageFailed(result);
                    }

                    throw;
                }
            });
        }
コード例 #3
0
        public void Receive(Func <IReceiveContext, Action <IReceiveContext> > callback, TimeSpan timeout)
        {
            int messageCount = Count;

            bool waited = false;

            if (messageCount == 0)
            {
                if (!_messageReady.WaitOne(timeout, true))
                {
                    return;
                }

                waited = true;
            }

            bool monitorExitNeeded = true;

            if (!Monitor.TryEnter(_messageLock, timeout))
            {
                return;
            }

            try
            {
                for (LinkedListNode <LoopbackMessage> iterator = _messages.First;
                     iterator != null;
                     iterator = iterator.Next)
                {
                    if (iterator.Value != null)
                    {
                        LoopbackMessage message = iterator.Value;
                        if (message.ExpirationTime.HasValue && message.ExpirationTime <= DateTime.UtcNow)
                        {
                            _messages.Remove(iterator);
                            return;
                        }

                        ReceiveContext context = ReceiveContext.FromBodyStream(message.Body);
                        context.SetMessageId(message.MessageId);
                        context.SetContentType(message.ContentType);
                        context.SetOriginalMessageId(message.OriginalMessageId);
                        if (message.ExpirationTime.HasValue)
                        {
                            context.SetExpirationTime(message.ExpirationTime.Value);
                        }

                        Action <IReceiveContext> receive = callback(context);
                        if (receive == null)
                        {
                            continue;
                        }

                        _messages.Remove(iterator);

                        using (message)
                        {
                            Monitor.Exit(_messageLock);
                            monitorExitNeeded = false;

                            receive(context);
                            return;
                        }
                    }
                }

                if (waited)
                {
                    return;
                }

                // we read to the end and none were accepted, so we are going to wait until we get another in the queue
                _messageReady.WaitOne(timeout, true);
            }
            finally
            {
                if (monitorExitNeeded)
                {
                    Monitor.Exit(_messageLock);
                }
            }
        }
        public void Receive(Func <IReceiveContext, Action <IReceiveContext> > callback, TimeSpan timeout)
        {
            AddConsumerBinding();

            _connectionHandler.Use(connection =>
            {
                BrokeredMessage message = _consumer.Get(timeout);
                if (message == null)
                {
                    return;
                }

                using (var stream = message.GetBody <Stream>())
                {
                    ReceiveContext context = ReceiveContext.FromBodyStream(stream);
                    context.SetMessageId(message.MessageId);
                    context.SetInputAddress(Address);
                    context.SetCorrelationId(message.CorrelationId);
                    context.SetContentType(message.ContentType);

                    Action <IReceiveContext> receive = callback(context);
                    if (receive == null)
                    {
                        Address.LogSkipped(message.MessageId);
                        return;
                    }

                    try
                    {
                        receive(context);
                    }
                    catch (Exception ex)
                    {
                        if (_logger.IsErrorEnabled)
                        {
                            _logger.Error("Consumer threw an exception", ex);
                        }

                        message.Abandon();
                    }

                    try
                    {
                        message.Complete();
                    }
                    catch (MessageLockLostException ex)
                    {
                        if (_logger.IsErrorEnabled)
                        {
                            _logger.Error("Message Lock Lost on message Complete()", ex);
                        }
                    }
                    catch (MessagingException ex)
                    {
                        if (_logger.IsErrorEnabled)
                        {
                            _logger.Error("Generic MessagingException thrown", ex);
                        }
                    }
                }
            });
        }