Exemple #1
0
        private QueueItem CreateQueueItem(Queue queue, Message msg)
        {
            var itm = new QueueItem(queue);

            itm.DisplayName = msg.Label;
            itm.Id          = msg.Id;
            itm.ArrivedTime = msg.ArrivedTime;
            //itm.Content = ReadMessageStream(msg.BodyStream);

            itm.Headers = new Dictionary <string, string>();
            if (msg.Extension.Length > 0)
            {
                var stream = new MemoryStream(msg.Extension);
                var transportMessageHeaders = TransportMessageHeaders.Create(stream.ToArray());
                //var o = headerSerializer.Deserialize(stream);

                var contentType       = transportMessageHeaders["Content-Type"];
                var originalMessageId = transportMessageHeaders["Original-Message-Id"];

                if (contentType != null)
                {
                    itm.Headers.Add("Content-Type", contentType);
                }

                if (originalMessageId != null)
                {
                    itm.Headers.Add("Original-Message-Id", originalMessageId);
                }
            }


            return(itm);
        }
        public void Receive(Func <IReceiveContext, Action <IReceiveContext> > lookupSinkChain, TimeSpan timeout)
        {
            var handler = new TimeoutHandler(timeout);

            handler.Run((timedOut) => _connectionHandler.Use(connection =>
            {
                foreach (var message in connection.Queue.Messages)
                {
                    if (timedOut())
                    {
                        break;
                    }

                    using (var body = new MemoryStream(message.Body))
                    {
                        var context = ReceiveContext.FromBodyStream(body, Address.IsTransactional);

                        context.SetMessageId("{0}".FormatWith(message.Id));
                        context.SetInputAddress(Address);

                        byte[] extension = message.Extension;
                        if (extension.Length > 0)
                        {
                            var headers = TransportMessageHeaders.Create(extension);

                            context.SetContentType(headers["Content-Type"]);
                            context.SetOriginalMessageId(headers["Original-Message-Id"]);
                        }

                        Action <IReceiveContext> receive = lookupSinkChain(context);


                        if (receive != null)
                        {
                            if (!connection.Queue.TryRemoveMessage(message))
                            {
                                continue;
                            }

                            receive(context);
                        }
                    }
                }
            }));
        }
Exemple #3
0
        protected bool EnumerateQueue(Func <IReceiveContext, Action <IReceiveContext> > receiver, TimeSpan timeout)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("The transport has been disposed: '{0}'".FormatWith(Address));
            }

            bool received = false;

            _connectionHandler.Use(connection =>
            {
                using (MessageEnumerator enumerator = connection.Queue.GetMessageEnumerator2())
                {
                    // if (_log.IsDebugEnabled)
                    // _log.DebugFormat("Enumerating endpoint: {0} ({1}ms)", Address, timeout);

                    while (enumerator.MoveNext(timeout))
                    {
                        if (enumerator.Current == null)
                        {
                            if (_log.IsDebugEnabled)
                            {
                                _log.DebugFormat("Current message was null while enumerating endpoint");
                            }

                            continue;
                        }

                        Message peekMessage = enumerator.Current;
                        using (peekMessage)
                        {
                            IReceiveContext context = ReceiveContext.FromBodyStream(peekMessage.BodyStream);
                            context.SetMessageId(peekMessage.Id);
                            context.SetInputAddress(_address);

                            byte[] extension = peekMessage.Extension;
                            if (extension.Length > 0)
                            {
                                TransportMessageHeaders headers = TransportMessageHeaders.Create(extension);

                                context.SetContentType(headers["Content-Type"]);
                            }

                            Action <IReceiveContext> receive = receiver(context);
                            if (receive == null)
                            {
                                if (_log.IsDebugEnabled)
                                {
                                    _log.DebugFormat("SKIP:{0}:{1}", Address, peekMessage.Id);
                                }

                                continue;
                            }

                            ReceiveMessage(enumerator, timeout, message =>
                            {
                                if (message == null)
                                {
                                    throw new TransportException(Address.Uri,
                                                                 "Unable to remove message from queue: " + context.MessageId);
                                }

                                if (message.Id != context.MessageId)
                                {
                                    throw new TransportException(Address.Uri,
                                                                 string.Format(
                                                                     "Received message does not match current message: ({0} != {1})",
                                                                     message.Id, context.MessageId));
                                }

                                if (_messageLog.IsDebugEnabled)
                                {
                                    _messageLog.DebugFormat("RECV:{0}:{1}:{2}", _address.InboundFormatName,
                                                            message.Label, message.Id);
                                }

                                receive(context);

                                received = true;
                            });
                        }
                    }
                }
            });

            return(received);
        }
Exemple #4
0
        protected void EnumerateQueue(Func <IReceiveContext, Action <IReceiveContext> > receiver, TimeSpan timeout)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("The transport has been disposed: '{0}'".FormatWith(Address));
            }

            _connectionHandler.Use(connection =>
            {
                try
                {
                    using (MessageEnumerator enumerator = connection.Queue.GetMessageEnumerator2())
                    {
                        while (enumerator.MoveNext(timeout))
                        {
                            if (enumerator.Current == null)
                            {
                                if (_log.IsDebugEnabled)
                                {
                                    _log.DebugFormat("Current message was null while enumerating endpoint");
                                }

                                continue;
                            }

                            Message peekMessage = enumerator.Current;
                            using (peekMessage)
                            {
                                IReceiveContext context = ReceiveContext.FromBodyStream(peekMessage.BodyStream,
                                                                                        _address.IsTransactional);
                                context.SetMessageId(peekMessage.Id);
                                context.SetInputAddress(_address);

                                byte[] extension = peekMessage.Extension;
                                if (extension.Length > 0)
                                {
                                    TransportMessageHeaders headers = TransportMessageHeaders.Create(extension);

                                    context.SetContentType(headers["Content-Type"]);
                                    context.SetOriginalMessageId(headers["Original-Message-Id"]);
                                }

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

                                ReceiveMessage(enumerator, timeout, message =>
                                {
                                    if (message == null)
                                    {
                                        throw new TransportException(Address.Uri,
                                                                     "Unable to remove message from queue: " + context.MessageId);
                                    }

                                    if (message.Id != context.MessageId)
                                    {
                                        throw new TransportException(Address.Uri,
                                                                     string.Format(
                                                                         "Received message does not match current message: ({0} != {1})",
                                                                         message.Id, context.MessageId));
                                    }

                                    receive(context);
                                });
                            }
                        }
                    }
                }
                catch (MessageQueueException ex)
                {
                    HandleInboundMessageQueueException(ex);
                }
            });
        }
        /// <summary>
        /// May throw a timeout exception if a message with the given id cannot be found.
        /// </summary>
        /// <param name="messageId"></param>
        public void ReturnMessageToSourceQueue(string messageId, IReceiveContext context)
        {
            try
            {
                var query      = context.DestinationAddress.Query;
                var errorQueue = context.DestinationAddress.OriginalString.Replace(query, "") + "_error";

                Uri fromUri = new Uri(errorQueue);
                Uri toUri   = context.DestinationAddress;

                IInboundTransport  fromTransport = Transports.GetInboundTransport(fromUri);
                IOutboundTransport toTransport   = Transports.GetOutboundTransport(toUri);

                fromTransport.Receive(receiveContext =>
                {
                    if (receiveContext.MessageId == messageId)
                    {
                        return(ctx =>
                        {
                            var moveContext = new MoveMessageSendContext(ctx);
                            toTransport.Send(moveContext);
                        });
                    }

                    return(null);
                }, 5.Seconds());

                Console.WriteLine("Success.");
            }
            catch (MessageQueueException ex)
            {
                if (ex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
                {
                    Console.WriteLine(NoMessageFoundErrorFormat, context.Id);

                    foreach (var m in queue.GetAllMessages())
                    {
                        var tm = TransportMessageHeaders.Create(m.Extension);

                        if (tm[""] != null)
                        {
                            //if (messageId != tm[""])
                            //    continue;

                            Console.WriteLine("Found message - going to return to queue.");

                            using (var tx = new TransactionScope())
                            {
                                using (var q = new MessageQueue(new EndpointAddress(tm[""]).Path))
                                    q.Send(m, MessageQueueTransactionType.Automatic);

                                queue.ReceiveByLookupId(MessageLookupAction.Current, m.LookupId,
                                                        MessageQueueTransactionType.Automatic);

                                tx.Complete();
                            }

                            Console.WriteLine("Success.");
                            //scope.Complete();

                            return;
                        }
                    }
                }
            }
            //}
        }
Exemple #6
0
        public void Should_reload_the_byte_array_into_a_headers_object()
        {
            var headers = TransportMessageHeaders.Create(_bytes);

            headers["Content-Type"].ShouldEqual("application/vnd.masstransit+json");
        }