/// <summary> /// Send messages using underlying transport. /// Serializes message bodies, adds unique ids /// </summary> /// <param name="lst"></param> /// <param name="dbTran">External transaction to be used for sending messages (optional)</param> internal void SendMessages(IList <MessageContainer> lst, object dbTran) { if (lst == null || lst.Count == 0) { return; } object prevbody = null; string prevstr = null; foreach (MessageContainer mc in lst) { if (string.IsNullOrEmpty(mc.To)) { throw new Exception("Message destination missing"); } if (mc.Body == null) { throw new NullReferenceException("Message body is null"); } if (mc.From == null) { mc.From = MessageTransport.Endpoint; } if (mc.UniqueId == null || mc.UniqueId.Length == 0) { mc.UniqueId = CreateNewMessageUniqueId(); } if (mc.Body == prevbody) { mc.BodyStr = prevstr; } else { prevbody = mc.Body; StringWriter sw = new StringWriter(); MessageSerializer.Serialize(mc.Body, sw); //MessageSerializer.Serialize(mc.Body, sw); mc.BodyStr = sw.ToString(); prevstr = mc.BodyStr; } } if (Transaction.Current == null || BatchOutgoingMessagesInTransaction == false) { MessageTransport.SendBatch(lst, dbTran); } else { MessageBatchingRM rm = GetCreateMessageBatchForCurrentTransaction(); if (!rm.TransactionOpen) { throw new Exception("Transaction is not open: " + rm.TransactionId); } foreach (MessageContainer mw in lst) { rm.Messages.Add(mw); } } }
private void RollbackMessageBatch(MessageBatchingRM rm) { lock (this) { _messageBatches.Remove(rm.TransactionId); } log.Debug("Removed message batch {0}", rm.TransactionId); }
private MessageBatchingRM GetCreateMessageBatchForCurrentTransaction() { if (Transaction.Current == null) { throw new Exception("No transaction!"); } string tid = Transaction.Current.TransactionInformation.LocalIdentifier; MessageBatchingRM rm; lock (this) { if (_messageBatches.TryGetValue(tid, out rm)) { return(rm); } log.Debug("Enlisting resource manager for {0}", tid); rm = new MessageBatchingRM(r => MessageTransport.SendBatch(r.Messages, null), CommitMessageBatch, RollbackMessageBatch); rm.TransactionId = tid; _messageBatches[tid] = rm; } Transaction.Current.EnlistVolatile(rm, EnlistmentOptions.None); return(rm); }
private MessageBatchingRM GetCreateMessageBatchForCurrentTransaction() { if (Transaction.Current == null) throw new Exception("No transaction!"); string tid = Transaction.Current.TransactionInformation.LocalIdentifier; MessageBatchingRM rm; lock (this) { if (_messageBatches.TryGetValue(tid, out rm)) return rm; log.Debug("Enlisting resource manager for {0}", tid); rm = new MessageBatchingRM(r => MessageTransport.SendBatch(r.Messages, null), CommitMessageBatch, RollbackMessageBatch); rm.TransactionId = tid; _messageBatches[tid] = rm; } Transaction.Current.EnlistVolatile(rm, EnlistmentOptions.None); return rm; }