public MSMQMessageSource(string queueName, Msmq.IMessageFormatter messageFormater) { Condition.Requires(queueName, "queueName").IsNotNull(); Condition.Requires(messageFormater, "messageFormater").IsNotNull(); QueueName = queueName; MessageFormater = messageFormater; Initialize(); }
public TransactionAction HandleFailedMessage(msmq.Message message, msmq.MessageQueueTransaction transaction) { Trace.WriteLine("Message discarded"); return TransactionAction.COMMIT; }
public TransactionAction HandleFailedMessage(msmq.Message message, msmq.MessageQueueTransaction transaction) { return TransactionAction.ROLLBACK; }
public TransactionAction HandleFailedMessage(msmq.Message message, msmq.MessageQueueTransaction transaction) { Logger.LogWarning("Sending message to retry queue. Message Id: " + message.Id); // Create/Get queue if only the queue address was passed into the ctor if (retryQueue == null) { if (!msmq.MessageQueue.Exists(retryQueueAddress)) { msmq.MessageQueue.Create(retryQueueAddress, true); } retryQueue = new msmq.MessageQueue(retryQueueAddress); } retryQueue.Send(message, transaction); return TransactionAction.COMMIT; }
public SeparateRetryQueueHandler(msmq.MessageQueue retryQueue, ILogger logger) { this.retryQueue = retryQueue; this.Logger = logger; }
public TransactionAction HandleFailedMessage(msmq.Message message, msmq.MessageQueueTransaction transaction) { message.Priority = msmq.MessagePriority.Lowest; message.AppSpecific++; if (message.AppSpecific > MAX_RETRIES) { Logger.LogInfo("Sending to dead-letter queue. Message Id: " + message.Id); // Create/Get queue if only the queue address was passed into the ctor if (deadLetterQueue == null) { if (!msmq.MessageQueue.Exists(deadLetterQueueAddress)) { msmq.MessageQueue.Create(deadLetterQueueAddress, true); } deadLetterQueue = new msmq.MessageQueue(deadLetterQueueAddress); } deadLetterQueue.Send(message, transaction); } else { Logger.LogInfo("Sending to back of retry queue. Message Id: " + message.Id); message.DestinationQueue.Send(message, transaction); } return TransactionAction.COMMIT; }
/// <summary> /// Pass in existing queue to hold open a connection to the deadLetterQueue when needed for the life of the object. /// /// </summary> /// <param name="deadLetterQueue"></param> public SendToBackHandler(msmq.MessageQueue deadLetterQueue, ILogger logger) { this.deadLetterQueue = deadLetterQueue; Logger = logger; }
public TransactionAction HandleFailedMessage(msmq.Message message, msmq.MessageQueueTransaction transaction) { if (message.Id != lastMessageId) { retries = 0; lastMessageId = message.Id; } retries++; if (retries > MAX_RETRIES) { Logger.LogInfo("Sending to dead-letter queue"); // Create/Get queue if only the queue address was passed into the ctor if (deadLetterQueue == null) { if (!msmq.MessageQueue.Exists(deadLetterQueueAddress)) { msmq.MessageQueue.Create(deadLetterQueueAddress, true); } deadLetterQueue = new msmq.MessageQueue(deadLetterQueueAddress); } deadLetterQueue.Send(message, transaction); return TransactionAction.COMMIT; } else { Logger.LogInfo("Returning message to queue for retry: " + retries); return TransactionAction.ROLLBACK; } }
/// <summary> /// Pass in existing queue to hold open a connection to the deadLetterQueue when needed for the life of the object. /// </summary> /// <param name="deadLetterQueue"></param> public RetrySendToDeadLetterQueueHandler(msmq.MessageQueue deadLetterQueue, ILogger logger) { this.deadLetterQueue = deadLetterQueue; this.Logger = logger; }
/// <summary> /// Receive message wrapped with a transaction to handle failures based on handler passed into ctor /// </summary> /// <param name="failedMessageHandler">Determines what implementation should be taken in the event of poisoned messages.</param> private bool ReceiveMessage(msmq.MessageQueue incomingQueue, IFailedMessageHandler failedMessageHandler) { incomingQueue.MessageReadPropertyFilter.SetAll(); using (var transaction = new MessageQueueTransaction()) { transaction.Begin(); Message message = null; try { message = incomingQueue.Receive(TimeSpan.Zero, transaction); Logger.LogDebug("Received message ID: " + message.Id); ProcessMessage(message); transaction.Commit(); Logger.LogDebug("Message processed OK"); return true; } catch (MessageQueueException msgQueueError) { if (msgQueueError.Message == "Time out for the requested operation has expired.") { // No message found to process before TimeSpan expired // {gulp} Swallow exception return false; } } catch (Exception e) { Logger.LogDebug("Message failed"); TransactionAction transactionAction = TransactionAction.ROLLBACK; if (message == null) { Logger.LogDebug("Message couldn't be received: " + e.Message); } else { try { transactionAction = failedMessageHandler.HandleFailedMessage(message, transaction); } catch (Exception failureHandlerException) { Logger.LogDebug("Error during failure handling: " + failureHandlerException.Message); } } if (transactionAction == TransactionAction.ROLLBACK) { transaction.Abort(); Logger.LogDebug("Transaction rolled back"); } else { transaction.Commit(); Logger.LogDebug("Transaction committed - message removed from queue"); } } return false; } }
private int ProcessMessages(msmq.MessageQueue queue, string retryQueueAddress, int messageMaxToProcessAtOneTime, IFailedMessageHandler failedMsgHandler) { int messageCount = 0; while (queue.CanRead && messageCount < messageMaxToProcessAtOneTime) { bool msgReceivedSuccess = ReceiveMessage(queue, failedMsgHandler); if (msgReceivedSuccess) { messageCount++; } else { return messageCount; } } return messageCount; }