コード例 #1
0
        private TRequest TranslateMessage(Message message)
        {
            if (_messageMapper == null)
            {
                throw new ConfigurationException(string.Format("No message mapper found for type {0} for message {1}.", typeof(TRequest).FullName, message.Id));
            }

            if (Logger != null)
            {
                Logger.DebugFormat("MessagePump: Translate message {0} on thread # {1}", message.Id, Thread.CurrentThread.ManagedThreadId);
            }

            TRequest request;

            try
            {
                request = _messageMapper.MapToRequest(message);
            }
            catch (Exception exception)
            {
                throw new MessageMappingException(string.Format("Failed to map message {0} using message mapper {1} for type {2} ", message.Id, _messageMapper.GetType().FullName, typeof(TRequest).FullName), exception);
            }

            return(request);
        }
コード例 #2
0
ファイル: MessagePump.cs プロジェクト: wangkai2014/Brighter
        protected TRequest TranslateMessage(Message message)
        {
            if (_messageMapper == null)
            {
                throw new ConfigurationException($"No message mapper found for type {typeof(TRequest).FullName} for message {message.Id}.");
            }

            _logger.Value.DebugFormat("MessagePump: Translate message {0} on thread # {1}", message.Id, Thread.CurrentThread.ManagedThreadId);

            TRequest request;

            try
            {
                request = _messageMapper.MapToRequest(message);
            }
            catch (Exception exception)
            {
                throw new MessageMappingException($"Failed to map message {message.Id} using message mapper {_messageMapper.GetType().FullName} for type {typeof(TRequest).FullName} ", exception);
            }

            return(request);
        }
コード例 #3
0
ファイル: MessagePump.cs プロジェクト: danbarua/Paramore
        /// <summary>
        /// Runs the message loop
        /// </summary>
        /// <exception cref="System.Exception">Could not receive message. Note that should return an MT_NONE from an empty queue on timeout</exception>
        public void Run()
        {
            do
            {
                if (Logger != null)
                {
                    Logger.DebugFormat("MessagePump: Receiving messages for {1} on thread # {0}", Thread.CurrentThread.ManagedThreadId, messageMapper.GetType().ToString());
                }
                Message message = null;
                try
                {
                    message = Channel.Receive(TimeoutInMilliseconds);
                }
                catch (ChannelFailureException)
                {
                    if (Logger != null)
                    {
                        Logger.WarnFormat("MessagePump: ChannelFailureException messages for {1} on thread # {0}", Thread.CurrentThread.ManagedThreadId, messageMapper.GetType().ToString());
                    }
                    continue;
                }
                catch (Exception exception)
                {
                    if (Logger != null)
                    {
                        Logger.ErrorFormat("MessagePump: Exception receiving messages for {1} on thread # {0} because of {2}", Thread.CurrentThread.ManagedThreadId, messageMapper.GetType().ToString(), exception);
                    }
                }

                if (message == null)
                {
                    throw new Exception("Could not receive message. Note that should return an MT_NONE from an empty queue on timeout");
                }

                // empty queue
                if (message.Header.MessageType == MessageType.MT_NONE)
                {
                    Task.Delay(500).Wait();
                    continue;
                }

                // failed to parse a message from the incoming data
                if (message.Header.MessageType == MessageType.MT_UNACCEPTABLE)
                {
                    if (Logger != null)
                    {
                        Logger.WarnFormat("MessagePump: Failed to parse a message from the incoming message with id {1} for {2} on thread # {0}", Thread.CurrentThread.ManagedThreadId, message.Id, messageMapper.GetType().ToString());
                    }
                    AcknowledgeMessage(message);
                    continue;
                }

                // QUIT command
                if (message.Header.MessageType == MessageType.MT_QUIT)
                {
                    if (Logger != null)
                    {
                        Logger.DebugFormat("MessagePump: Quit receiving messages for {1} on thread # {0}", Thread.CurrentThread.ManagedThreadId, messageMapper.GetType().ToString());
                    }
                    Channel.Dispose();
                    break;
                }

                // Serviceable message
                try
                {
                    DispatchRequest(message.Header.MessageType, TranslateMessage(message));
                }
                catch (ConfigurationException configurationException)
                {
                    if (Logger != null)
                    {
                        Logger.DebugFormat("MessagePump: {0} Stopping receiving of messages for {2} on thread # {1} because of {3}",
                                           configurationException.Message,
                                           Thread.CurrentThread.ManagedThreadId,
                                           messageMapper.GetType().ToString(),
                                           configurationException);
                    }
                    break;
                }
                catch (RequeueException)
                {
                    RequeueMessage(message);
                }
                catch (AggregateException aggregateException)
                {
                    bool stop = false;
                    foreach (var exception in aggregateException.InnerExceptions)
                    {
                        if (exception is RequeueException)
                        {
                            RequeueMessage(message);
                            continue;
                        }
                        else if (exception is ConfigurationException)
                        {
                            if (Logger != null)
                            {
                                Logger.DebugFormat("MessagePump: {0} Stopping receiving of messages for {2} on thread # {1} because of {3}", exception.Message, Thread.CurrentThread.ManagedThreadId, messageMapper.GetType().ToString(), exception);
                            }
                            stop = true;
                            break;
                        }
                    }

                    if (stop)
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    if (Logger != null)
                    {
                        Logger.ErrorFormat("MessagePump: Failed to dispatch message for {1} on thread # {0} because of {3}", Thread.CurrentThread.ManagedThreadId, messageMapper.GetType().ToString(), e);
                    }
                }

                AcknowledgeMessage(message);
            } while (true);

            if (Logger != null)
            {
                Logger.DebugFormat("MessagePump: Finished running message loop, no longer receiving messages for {0} on thread # {1}", messageMapper.GetType().ToString(), Thread.CurrentThread.ManagedThreadId);
            }
        }