本文整理汇总了C#中IMessageContext.Get方法的典型用法代码示例。如果您正苦于以下问题:C# IMessageContext.Get方法的具体用法?C# IMessageContext.Get怎么用?C# IMessageContext.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IMessageContext
的用法示例。
在下文中一共展示了IMessageContext.Get方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReceiveMessage
/// <summary>
/// Receives a new message.
/// </summary>
/// <param name="context">The context.</param>
/// <returns></returns>
public IReceivedMessageInternal ReceiveMessage(IMessageContext context)
{
context.Commit += ContextOnCommit;
context.Rollback += ContextOnRollback;
context.Cleanup += context_Cleanup;
TimeSpan? timeout = null;
IMessageId messageId = null;
var rpc = context.Get(_headers.StandardHeaders.RpcContext);
if (rpc?.Timeout != null)
{
timeout = rpc.Timeout;
}
if (rpc?.MessageId != null)
{
messageId = rpc.MessageId;
}
using (
var workSub = rpc?.MessageId != null && rpc.MessageId.HasValue
? _workSubFactory.Create(rpc.MessageId)
: _workSubFactory.Create())
{
while (true)
{
if (_cancelWork.Tokens.Any(m => m.IsCancellationRequested))
{
return null;
}
var message = GetMessage(context, messageId);
if (message != null && !message.Expired)
{
return message.Message;
}
if (_cancelWork.Tokens.Any(m => m.IsCancellationRequested))
{
return null;
}
workSub.Reset();
message = GetMessage(context, messageId);
if (message != null && !message.Expired)
{
return message.Message;
}
if (message != null && message.Expired)
{
continue;
}
if (workSub.Wait(timeout))
{
continue;
}
return null;
}
}
}
示例2: GetMessage
/// <summary>
/// Returns the next message, if any.
/// </summary>
/// <param name="context">The context.</param>
/// <returns>
/// A message if one is found; null otherwise
/// </returns>
public IReceivedMessageInternal GetMessage(IMessageContext context)
{
//if stopping, exit now
if (_cancelToken.Tokens.Any(t => t.IsCancellationRequested))
{
return null;
}
//check for a specific MessageID to pull
IMessageId messageId = null;
var rpc = context.Get(_configuration.HeaderNames.StandardHeaders.RpcContext);
if (rpc?.MessageId != null && rpc.MessageId.HasValue)
{
messageId = rpc.MessageId;
}
//ask for the next message, or a specific message if we have a messageID
var receivedTransportMessage =
_receiveMessage.Handle(new ReceiveMessageQuery(messageId, _configuration.Routes));
//if no message (null) run the no message action and return
if (receivedTransportMessage == null)
{
return null;
}
//set the message ID on the context for later usage
context.MessageId = receivedTransportMessage.MesssageId;
return receivedTransportMessage;
}
示例3: RollbackForTransaction
/// <summary>
/// Rollbacks the specified message by rolling back the transaction
/// </summary>
/// <param name="context">The context.</param>
public void RollbackForTransaction(IMessageContext context)
{
var connection = context.Get(_headers.Connection);
//if transaction open, then just rollback the transaction
if (connection.NpgsqlConnection == null || connection.NpgsqlTransaction == null) return;
if (_configuration.Options().EnableStatusTable)
{
_setStatusCommandHandler.Handle(new SetStatusTableStatusCommand((long)context.MessageId.Id.Value, QueueStatuses.Waiting));
}
connection.NpgsqlTransaction.Rollback();
}
示例4: CommitForTransaction
/// <summary>
/// Commits the message, via the held transaction
/// </summary>
/// <param name="context">The context.</param>
public void CommitForTransaction(IMessageContext context)
{
var connection = context.Get(_headers.Connection);
//if transaction held
if (connection.NpgsqlConnection == null || connection.NpgsqlTransaction == null) return;
//delete the message, and then commit the transaction
_deleteTransactionalMessageCommand.Handle(new DeleteTransactionalMessageCommand((long)context.MessageId.Id.Value, context));
connection.NpgsqlTransaction.Commit();
connection.NpgsqlTransaction = null;
if (_configuration.Options().EnableStatusTable)
{
_deleteStatusCommandHandler.Handle(new DeleteStatusTableStatusCommand((long)context.MessageId.Id.Value));
}
}
示例5: Rollback
/// <summary>
/// Rollbacks the specified message by setting the status
/// </summary>
/// <param name="context">The context.</param>
public void Rollback(IMessageContext context)
{
if (context.MessageId == null || !context.MessageId.HasValue) return;
//there is nothing to rollback unless at least one of these options is enabled
if (_configuration.Options().EnableDelayedProcessing ||
_configuration.Options().EnableHeartBeat ||
_configuration.Options().EnableStatus)
{
DateTime? lastHeartBeat = null;
if (context.WorkerNotification?.HeartBeat?.Status?.LastHeartBeatTime != null)
{
lastHeartBeat = context.WorkerNotification.HeartBeat.Status.LastHeartBeatTime.Value;
}
var increaseDelay = context.Get(_headers.IncreaseQueueDelay).IncreaseDelay;
_rollbackCommand.Handle(new RollbackMessageCommand(lastHeartBeat,
(long)context.MessageId.Id.Value, increaseDelay));
}
}
示例6: GetMessage
/// <summary>
/// Returns the next message, if any.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="connection">The connection.</param>
/// <param name="noMessageFoundActon">The no message found action.</param>
/// <returns>
/// A message if one is found; null otherwise
/// </returns>
public IReceivedMessageInternal GetMessage(IMessageContext context, Connection connection,
Action<Connection> noMessageFoundActon)
{
//if stopping, exit now
if (_cancelToken.Tokens.Any(t => t.IsCancellationRequested))
{
noMessageFoundActon(connection);
return null;
}
//check for a specific MessageID to pull
IMessageId messageId = null;
var rpc = context.Get(_configuration.HeaderNames.StandardHeaders.RpcContext);
if (rpc?.MessageId != null && rpc.MessageId.HasValue)
{
messageId = rpc.MessageId;
}
//ask for the next message, or a specific message if we have a messageID
var receivedTransportMessage =
_receiveMessage.Handle(new ReceiveMessageQuery(connection.SqlConnection,
connection.SqlTransaction, messageId, _configuration.Routes));
//if no message (null) run the no message action and return
if (receivedTransportMessage == null)
{
noMessageFoundActon(connection);
return null;
}
//set the message ID on the context for later usage
context.MessageId = receivedTransportMessage.MesssageId;
//if we are holding open transactions, we need to update the status table in a separate call
//When not using held transactions, this is part of the de-queue statement and so not needed here
//TODO - we could consider using a task to update the status table
//the status table drives nothing internally, however it may drive external processes
//because of that, we are not returning the message until the status table is updated.
//we could make this a configurable option in the future?
if (_configuration.Options().EnableHoldTransactionUntilMessageCommited &&
_configuration.Options().EnableStatusTable)
{
_setStatusCommandHandler.Handle(
new SetStatusTableStatusCommand(
(long) receivedTransportMessage.MesssageId.Id.Value, QueueStatuses.Processing));
}
return receivedTransportMessage;
}
示例7: ReceiveMessageAsync
/// <summary>
/// Returns a message to process.
/// </summary>
/// <param name="context">The message context.</param>
/// <returns>
/// A message to process or null if there are no messages to process
/// </returns>
/// <exception cref="ReceiveMessageException">An error occurred while attempting to read messages from the queue</exception>
public async Task<IReceivedMessageInternal> ReceiveMessageAsync(IMessageContext context)
{
if (_configuration.Options().EnableHoldTransactionUntilMessageCommited)
{
_commitConnection = (c, b) => _handleMessage.CommitMessage.CommitForTransaction(context);
_rollbackConnection = (c, b) => _handleMessage.RollbackMessage.RollbackForTransaction(context);
}
try
{
if (_cancelWork.Tokens.Any(m => m.IsCancellationRequested))
{
return null;
}
if (_configuration.Options().QueueType == QueueTypes.RpcReceive)
{
var rpc = context.Get(_configuration.HeaderNames.StandardHeaders.RpcContext);
if (rpc.MessageId == null || !rpc.MessageId.HasValue)
{
return null;
}
}
var connection = GetConnectionAndSetOnContext(context);
try
{
return await _receiveMessages.GetMessageAsync(context, connection, connection1 => _disposeConnection(connection)).ConfigureAwait(false);
}
finally
{
if (!_configuration.Options().EnableHoldTransactionUntilMessageCommited)
{
_disposeConnection(connection);
}
}
}
catch (PoisonMessageException exception)
{
if (exception.MessageId != null && exception.MessageId.HasValue)
{
context.MessageId = exception.MessageId;
}
throw;
}
catch (Exception exception)
{
throw new ReceiveMessageException("An error occurred while attempting to read messages from the queue",
exception);
}
}