本文整理汇总了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());
}
示例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;
}
示例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));
}
示例4: TestSenderMessage
private static void TestSenderMessage( IMessageHandler handler, Message0 msg, MethodId expectedHandler )
{
handler.HandleMessage( msg );
CheckExpectedHandler( msg, expectedHandler );
}