Advertisement
Guest User

Omer Mor

a guest
Apr 4th, 2010
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1.         private void HandleMessageCompletion(
  2.             Message message,
  3.             TransactionScope tx,
  4.             OpenedQueue messageQueue,
  5.             Exception exception,
  6.             Action<CurrentMessageInformation, Exception> messageCompleted,
  7.             Action<CurrentMessageInformation> beforeTransactionCommit)
  8.         {
  9.             beforeTransactionCommit = beforeTransactionCommit ?? (_ => { });
  10.             var transaction = new TxWrapper(tx, logger);
  11.  
  12.             try
  13.             {
  14.                 HandleErrors(message, messageQueue, exception, messageCompleted);
  15.                 HandleTransaction(beforeTransactionCommit, transaction);
  16.                 RaiseEventSafely(messageCompleted, currentMessageInformation, null);
  17.             }
  18.             catch (Exception ex)
  19.             {
  20.                 HandleErrors(message, messageQueue, ex, messageCompleted);
  21.             }
  22.         }
  23.  
  24.         private void HandleTransaction(Action<CurrentMessageInformation> beforeTransactionCommit, TxWrapper transaction)
  25.         {
  26.             try
  27.             {
  28.                 beforeTransactionCommit(currentMessageInformation);
  29.                 transaction.Complete();
  30.             }
  31.             finally
  32.             {
  33.                 transaction.Dispose();
  34.             }
  35.         }
  36.  
  37.         private void HandleErrors(Message message, OpenedQueue messageQueue, Exception ex, Action<CurrentMessageInformation, Exception> messageCompleted)
  38.         {
  39.             if (ex == null)
  40.                 return;
  41.             logger.Warn("Error mode", ex);
  42.             if (message == null)
  43.                 return;
  44.             RaiseEventSafely(messageCompleted, currentMessageInformation, ex);
  45.             RaiseEventSafely(MessageProcessingFailure, currentMessageInformation, ex);
  46.  
  47.             if (messageQueue.IsTransactional == false) // put the item back in the queue
  48.             {
  49.                 messageQueue.Send(message);
  50.             }
  51.         }
  52.  
  53.         private void RaiseEventSafely<T1, T2>(Action<T1, T2> @event, T1 arg1, T2 arg2)
  54.         {
  55.             try
  56.             {
  57.                 if (@event != null)
  58.                     @event(arg1, arg2);
  59.             }
  60.             catch (Exception e)
  61.             {
  62.                 logger.Error(
  63.                     "An error occured when raising an event, the error will NOT affect the message processing",
  64.                     e);
  65.             }
  66.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement