当前位置: 首页>>代码示例>>C#>>正文


C# IMessageHandler.HandleMessage方法代码示例

本文整理汇总了C#中IMessageHandler.HandleMessage方法的典型用法代码示例。如果您正苦于以下问题:C# IMessageHandler.HandleMessage方法的具体用法?C# IMessageHandler.HandleMessage怎么用?C# IMessageHandler.HandleMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IMessageHandler的用法示例。


在下文中一共展示了IMessageHandler.HandleMessage方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConsumeMessages

        public async Task ConsumeMessages(IMessageHandler handler, CancellationToken cancellationToken)
        {
            var concurrencyLevel = ConcurrencyLevel;
            var messagesInProcess = new Dictionary<Task, DeliveredRabbitMessage>();
            do
            {
                if (messagesInProcess.Any())
                {
                    var completedMessage = await Task.WhenAny(messagesInProcess.Keys);
                    var originalMessage = messagesInProcess[completedMessage];
                    messagesInProcess.Remove(completedMessage);
                    try
                    {
                        await completedMessage;
                        _messageSource.Acknowledge(originalMessage);
                    }
                    catch (OperationCanceledException)
                    {
                        _messageSource.Cancel(originalMessage);
                    }
                    catch (Exception exception)
                    {
                        _messageSource.Failure(originalMessage, exception);
                    }
                }

                DeliveredRabbitMessage nextMessage;
                while (!cancellationToken.IsCancellationRequested 
                    && messagesInProcess.Count < concurrencyLevel 
                    && _messageSource.TryGetNextMessage(out nextMessage))
                {
                    try
                    {
                        var task = handler.HandleMessage(nextMessage, cancellationToken);
                        messagesInProcess.Add(task, nextMessage);
                    }
                    catch (Exception exception)
                    {
                        _messageSource.Failure(nextMessage, exception);
                    }
                }
            } while (messagesInProcess.Any());
        }
开发者ID:BrianZell,项目名称:RabbitExtensions,代码行数:43,代码来源:AsyncMessageConsumer.cs

示例2: SendMessageToHandler

 /// <summary>
 /// Convenience method available for subclasses. Returns 'true' unless a
 /// "Selective Consumer" throws a <see cref="MessageRejectedException"/>.
 /// </summary>
 /// <param name="message">the message to handle</param>
 /// <param name="handler">the messagehandler</param>
 /// <returns></returns>
 protected bool SendMessageToHandler(IMessage message, IMessageHandler handler)
 {
     try {
         handler.HandleMessage(message);
         return true;
     }
     catch(MessageRejectedException ex) {
         #region logging
         if(logger.IsDebugEnabled) {
             logger.Debug("Handler '" + handler + "' rejected Message, continuing with other handlers if available.", ex);
         }
         #endregion
     }
     return false;
 }
开发者ID:rlxrlxrlx,项目名称:spring-net-integration,代码行数:22,代码来源:AbstractDispatcher.cs

示例3: StartReceivingMessagesFor

        /// <summary>
        /// Infinite message loop : start listening for incoming Http messages on stdin.
        /// Each message received is passed to an IMessageHandler on the main thread of the loop.
        /// This method blocks the current thread until ShutdownAfterNextMessage() is called. 
        /// </summary>
        public void StartReceivingMessagesFor(IMessageHandler messageHandler)
        {
            logWriter.WriteLine(String.Format("{0} -- Server startup", DateTime.Now));

            // Infinite message loop
            char[] buffer = new char[BUFFER_SIZE];
            for (;;)
            {
                // Receive and handle one message
                try
                {
                    // Read Http message headers
                    int contentLength = 0;
                    string headerLine = Console.ReadLine();
                    while (!String.IsNullOrEmpty(headerLine))
                    {
                        int headerSeparatorIndex = headerLine.IndexOf(':');
                        if (headerSeparatorIndex > 0)
                        {
                            // Ignore all headers but Content-Length
                            string headerName = headerLine.Substring(0, headerSeparatorIndex);
                            if (headerName == "Content-Length" && headerSeparatorIndex < (headerLine.Length - 2))
                            {
                                // Try to parse Content-Length
                                string headerValue = headerLine.Substring(headerSeparatorIndex + 2);
                                Int32.TryParse(headerValue, out contentLength);
                            }
                        }
                        headerLine = Console.ReadLine();
                    }

                    // If the server could not find the content length of the message
                    // it is impossible to detect where the message ends : write a fatal
                    // error message and exit the loop
                    if (contentLength == 0)
                    {
                        logWriter.WriteLine(String.Format("{0} !! Fatal error : message without Content-Length header", DateTime.Now));
                        break;
                    }
                    else
                    {
                        // Log the size of the message received
                        if (logLevel >= ServerLogLevel.Message)
                        {
                            logWriter.WriteLine(String.Format("{0} >> Message received : Content-Length={1}", DateTime.Now, contentLength));
                        }
                    }

                    // Read Http message body
                    StringBuilder sbMessage = new StringBuilder();
                    while (contentLength > 0)
                    {
                        int nbCharsToRead = contentLength > buffer.Length ? buffer.Length : contentLength;
                        int nbCharsRead = Console.In.Read(buffer, 0, nbCharsToRead);
                        sbMessage.Append(buffer, 0, nbCharsRead);
                        contentLength -= nbCharsRead;
                    }
                    string message = sbMessage.ToString();
                    if(logLevel == ServerLogLevel.Protocol)
                    {
                        logWriter.WriteLine(message);
                        logWriter.WriteLine("----------");
                    }

                    // Handle incoming message and optionnaly send reply
                    messageHandler.HandleMessage(message, this);
                }
                catch (Exception e)
                {
                    logWriter.WriteLine(String.Format("{0} !! Exception : {1}", DateTime.Now, e.Message));
                }

                // Exit the loop after message handling if a shutdown of the server has been requested
                if (shutdownAfterNextMessage)
                {
                    break;
                }
            }

            logWriter.WriteLine(String.Format("{0} -- Server shutdown", DateTime.Now));
        }
开发者ID:osmedile,项目名称:TypeCobol,代码行数:86,代码来源:StdioHttpServer.cs

示例4: TestSenderMessage

 private static void TestSenderMessage( IMessageHandler handler, Message0 msg, MethodId expectedHandler )
 {
     handler.HandleMessage( msg );
     CheckExpectedHandler( msg, expectedHandler );
 }
开发者ID:johann-gambolputty,项目名称:robotbastards,代码行数:5,代码来源:TestMessages.cs


注:本文中的IMessageHandler.HandleMessage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。