/// <summary> /// Starts the transport. /// </summary> public void Start() { CheckConfiguration(); CreateQueuesIfNecessary(); if (ErrorQueue != null) { errorQueue = new MessageQueue(MsmqUtilities.GetFullPath(ErrorQueue)); } if (!string.IsNullOrEmpty(InputQueue)) { _queue = PluginQueueFactory.Create(InputQueue); _uiCommandQueue = PluginQueueFactory.Create(UICommandInputQueue); if (PurgeOnStartup) { _queue.Purge(); _uiCommandQueue.Purge(); } for (var i = 0; i < numberOfWorkerThreads; i++) { AddWorkerThread(Process).Start(); } } }
/// <summary> /// May throw a timeout exception if a message with the given id cannot be found. /// </summary> /// <param name="messageId"></param> public void ReturnMessageToSourceQueue(string messageId) { using (var scope = new TransactionScope()) { var message = queue.ReceiveById(messageId, TimeoutDuration, MessageQueueTransactionType.Automatic); var tm = MsmqUtilities.Convert(message); if (!tm.Headers.ContainsKey(FaultsHeaderKeys.FailedQ)) { Console.WriteLine( "ERROR: Message does not have a header indicating from which queue it came. Cannot be automatically returned to queue."); return; } using ( var q = new MessageQueue(MsmqUtilities.GetFullPath(Address.Parse(tm.Headers[FaultsHeaderKeys.FailedQ]))) ) q.Send(message, MessageQueueTransactionType.Automatic); Console.WriteLine("Success."); scope.Complete(); } }
public void Init(Address address, bool transactional) { useTransactions = transactional; if (address == null) { throw new ArgumentException("Input queue must be specified"); } var machine = address.Machine; if (machine.ToLower() != Environment.MachineName.ToLower()) { throw new InvalidOperationException(string.Format("Input queue [{0}] must be on the same machine as this process [{1}].", address, Environment.MachineName.ToLower())); } myQueue = new MessageQueue(MsmqUtilities.GetFullPath(address)); if (useTransactions && !QueueIsTransactional()) { throw new ArgumentException("Queue must be transactional (" + address + ")."); } var mpf = new MessagePropertyFilter(); mpf.SetAll(); myQueue.MessageReadPropertyFilter = mpf; if (PurgeOnStartup) { myQueue.Purge(); } }
private void Init(string queue) { var path = MsmqUtilities.GetFullPath(queue); var mq = new MessageQueue(path); if (!mq.Transactional) { throw new Exception("Queue must be transactional."); } storageQueue = mq; storageQueue.Formatter = new XmlMessageFormatter(new[] { typeof(TimeoutData) }); storageQueue.GetAllMessages().ToList().ForEach( m => { var td = m.Body as TimeoutData; if (td == null) //get rid of message { storageQueue.ReceiveById(m.Id, MessageQueueTransactionType.Single); } else //put into lookup { sagaToMessageIdLookup[td.SagaId] = m.Id; } }); }
public PluginQueue(string queueName) { var q = new MessageQueue(MsmqUtilities.GetFullPath(queueName), false, true); _queue = InitializeQueue(q); _queueName = queueName; }
/// <summary> /// Signal that a worker is available to receive a dispatched message. /// </summary> /// <param name="address"> /// The address of the worker that will accept the dispatched message. /// </param> /// <param name="capacity">The number of messages that this worker is ready to process</param> public void WorkerAvailable(Address address, int capacity) { for (var i = 0; i < capacity; i++) { storageQueue.Send(new Message { ResponseQueue = new MessageQueue(MsmqUtilities.GetFullPath(address)) }, MessageQueueTransactionType.Automatic); } }
/// <summary> /// Initializes the object. /// </summary> public void Start() { var path = MsmqUtilities.GetFullPath(StorageQueueAddress); storageQueue = new MessageQueue(path); if ((!storageQueue.Transactional) && (SettingsHolder.Get <bool>("Transactions.Enabled"))) { throw new Exception(string.Format("Queue [{0}] must be transactional.", path)); } }
/// <summary> /// Initializes the object. /// </summary> public void Start() { var path = MsmqUtilities.GetFullPath(StorageQueue); storageQueue = new MessageQueue(path); if (!storageQueue.Transactional) { throw new Exception("Queue must be transactional."); } }
/// <summary> /// Signal that a worker is available to receive a dispatched message. /// </summary> /// <param name="address"> /// The address of the worker that will accept the dispatched message. /// </param> public void WorkerAvailable(string address) { lock (locker) { var msg = new Message { ResponseQueue = new MessageQueue(MsmqUtilities.GetFullPath(address)) }; storageQueue.Send(msg, MessageQueueTransactionType.Automatic); } }
/// <summary> /// Returns the queue whose process failed processing the given message /// by accessing the label of the message. /// </summary> /// <param name="m"></param> /// <returns></returns> public static string GetFailedQueue(Message m) { if (!m.Label.Contains(FAILEDQUEUE)) { return(null); } var startIndex = m.Label.IndexOf(string.Format("<{0}>", FAILEDQUEUE)) + FAILEDQUEUE.Length + 2; var count = m.Label.IndexOf(string.Format("</{0}>", FAILEDQUEUE)) - startIndex; return(MsmqUtilities.GetFullPath(m.Label.Substring(startIndex, count))); }
void ISendMessages.Send(TransportMessage message, Address address) { var queuePath = MsmqUtilities.GetFullPath(address); try { using (var q = new MessageQueue(queuePath, QueueAccessMode.Send)) { var toSend = MsmqUtilities.Convert(message); toSend.UseDeadLetterQueue = UseDeadLetterQueue; toSend.UseJournalQueue = UseJournalQueue; if (message.ReplyToAddress != null) { toSend.ResponseQueue = new MessageQueue(MsmqUtilities.GetReturnAddress(message.ReplyToAddress.ToString(), address.ToString())); } q.Send(toSend, GetTransactionTypeForSend()); message.Id = toSend.Id; } } catch (MessageQueueException ex) { string msg = string.Empty; if (ex.MessageQueueErrorCode == MessageQueueErrorCode.QueueNotFound) { if (address == null) { msg = "Failed to send message."; } else { msg = string.Format("Failed to send message to address: {0}@{1}", address.Queue, address.Machine); } } throw new QueueNotFoundException(address, msg, ex); } catch (Exception ex) { if (address == null) { throw new FailedToSendMessageException("Failed to send message.", ex); } else { throw new FailedToSendMessageException(string.Format("Failed to send message to address: {0}@{1}", address.Queue, address.Machine), ex); } } }
public void Setup() { MsmqUtilities.CreateQueueIfNecessary(errorQueueAdress); MsmqUtilities.CreateQueueIfNecessary(originQueueAdress); errorQueue = new MessageQueue(MsmqUtilities.GetFullPath(errorQueueAdress)); errorQueue.Purge(); originQueue = new MessageQueue(MsmqUtilities.GetFullPath(originQueueAdress)); originQueue.Purge(); messageStore = new MsmqFailedMessagesStore(errorQueueAdress); }
public static MessageQueue GetOrCreateMessageQueue(string name) { CheckedQueues.GetOrAdd(name, n => { MsmqUtilities.CreateQueueIfNecessary(name); return(null); }); var q = new MessageQueue(MsmqUtilities.GetFullPath(name), false, true); var filter = new MessagePropertyFilter(); filter.SetAll(); q.MessageReadPropertyFilter = filter; return(q); }
public void SendAMessageWithInvalidHeadersToTheAuditQueue() { // Generate a bad msg to test MSMQ Audit Queue Importer // This message should fail to parse to a transport message because of the bad header and so should end up in the Particular.ServiceControl.Errors queue var q = new MessageQueue(MsmqUtilities.GetFullPath(Address.Parse("audit")), false, true, QueueAccessMode.Send); using (var tx = new MessageQueueTransaction()) { tx.Begin(); var message = new Message("Message with invalid headers") { Extension = new byte[] { 1 } }; q.Send(message, tx); tx.Commit(); } }
public MsmqFailedMessagesStore(string adress) { this.adress = adress; var fullPath = MsmqUtilities.GetFullPath(adress); errorQueue = new MessageQueue(fullPath) { MessageReadPropertyFilter = { Id = true, Priority = true, SentTime = true, MessageType = true, Label = true, } }; }
void ISendMessages.Send(TransportMessage message, Address address) { var queuePath = MsmqUtilities.GetFullPath(address); try { using (var q = new MessageQueue(queuePath, false, true, QueueAccessMode.Send)) { using (Message toSend = MsmqUtilities.Convert(message)) { toSend.UseDeadLetterQueue = UseDeadLetterQueue; toSend.UseJournalQueue = UseJournalQueue; if (message.ReplyToAddress != null) { toSend.ResponseQueue = new MessageQueue(MsmqUtilities.GetReturnAddress(message.ReplyToAddress.ToString(), address.ToString())); } q.Send(toSend, GetTransactionTypeForSend()); message.Id = toSend.Id; } } } catch (MessageQueueException ex) { if (ex.MessageQueueErrorCode == MessageQueueErrorCode.QueueNotFound) { var msg = address == null ? "Failed to send message. Target address is null." : string.Format("Failed to send message to address: [{0}]", address); throw new QueueNotFoundException(address, msg, ex); } ThrowFailedToSendException(address, ex); } catch (Exception ex) { ThrowFailedToSendException(address, ex); } }
void ISubscriptionStorage.Init() { var path = MsmqUtilities.GetFullPath(Queue); q = new MessageQueue(path); bool transactional; try { transactional = q.Transactional; } catch (Exception ex) { throw new ArgumentException(string.Format("There is a problem with the subscription storage queue {0}. See enclosed exception for details.", Queue), ex); } if (!transactional && SettingsHolder.Get <bool>("Transactions.Enabled")) { throw new ArgumentException("Queue must be transactional (" + Queue + ")."); } var messageReadPropertyFilter = new MessagePropertyFilter { Id = true, Body = true, Label = true }; q.Formatter = new XmlMessageFormatter(new[] { typeof(string) }); q.MessageReadPropertyFilter = messageReadPropertyFilter; foreach (var m in q.GetAllMessages()) { var subscriber = Address.Parse(m.Label); var messageTypeString = m.Body as string; var messageType = new MessageType(messageTypeString); //this will parse both 2.6 and 3.0 type strings entries.Add(new Entry { MessageType = messageType, Subscriber = subscriber }); AddToLookup(subscriber, messageType, m.Id); } }
static MessageQueue CreateReceiver() { var queue = new MessageQueue(MsmqUtilities.GetFullPath(Settings.AuditQueue), QueueAccessMode.Receive); var messageReadPropertyFilter = new MessagePropertyFilter { Body = true, TimeToBeReceived = true, Recoverable = true, Id = true, ResponseQueue = true, CorrelationId = true, Extension = true, AppSpecific = true }; queue.MessageReadPropertyFilter = messageReadPropertyFilter; return(queue); }
public void Init() { string path = MsmqUtilities.GetFullPath(StorageQueue); var q = new MessageQueue(path); if (!q.Transactional) { throw new Exception("Queue must be transactional."); } q.Formatter = new XmlMessageFormatter { TargetTypes = new[] { typeof(ProxyData) } }; q.MessageReadPropertyFilter = new MessagePropertyFilter { Body = true, CorrelationId = true }; storageQueue = q; }
/// <summary> /// Signal that a worker is available to receive a dispatched message. /// </summary> /// <param name="address"> /// The address of the worker that will accept the dispatched message. /// </param> /// <param name="capacity">The number of messages that this worker is ready to process</param> public void WorkerAvailable(Address address, int capacity) { var returnAddress = new MessageQueue(MsmqUtilities.GetFullPath(address)); for (var i = 0; i < capacity; i++) { if (UnitOfWork.HasActiveTransaction()) { storageQueue.Send(new Message { ResponseQueue = returnAddress }, UnitOfWork.Transaction); } else { storageQueue.Send(new Message { ResponseQueue = returnAddress }, MessageQueueTransactionType.Automatic); } } }
void ISubscriptionStorage.Init() { string path = MsmqUtilities.GetFullPath(Queue); q = new MessageQueue(path); bool transactional; try { transactional = q.Transactional; } catch (Exception ex) { throw new ArgumentException(string.Format("There is a problem with the subscription storage queue {0}. See enclosed exception for details.", Queue), ex); } if (!transactional) { throw new ArgumentException("Queue must be transactional (" + Queue + ")."); } var mpf = new MessagePropertyFilter(); mpf.SetAll(); q.Formatter = new XmlMessageFormatter(new[] { typeof(string) }); q.MessageReadPropertyFilter = mpf; foreach (var m in q.GetAllMessages()) { var subscriber = m.Label; var messageType = m.Body as string; entries.Add(new Entry { MessageType = messageType, Subscriber = subscriber }); AddToLookup(subscriber, messageType, m.Id); } }
public void Start() { // Any messages that fail conversion to a transportmessage is sent to the particular.servicecontrol.errors queue using low level Api // The actual queue name is based on service name to support mulitple instances on same host (particular.servicecontrol.errors is the default) var serviceControlErrorQueueAddress = Address.Parse(string.Format("{0}.errors", Settings.ServiceName)); serviceControlErrorQueue = new MessageQueue(MsmqUtilities.GetFullPath(serviceControlErrorQueueAddress), false, true, QueueAccessMode.Send); if (!enabled) { return; } if (Settings.AuditQueue == Address.Undefined) { Logger.Info("No Audit queue has been configured. No audit import will be performed. To enable imports add the ServiceBus/AuditQueue appsetting and restart ServiceControl"); return; } if (TerminateIfForwardingIsEnabledButQueueNotWritable()) { return; } performanceCounters.Initialize(); queuePeeker = new MessageQueue(MsmqUtilities.GetFullPath(Settings.AuditQueue), QueueAccessMode.Peek); queuePeeker.MessageReadPropertyFilter.ClearAll(); queuePeeker.PeekCompleted += QueueOnPeekCompleted; enrichers = builder.BuildAll <IEnrichImportedMessages>().ToList(); Logger.InfoFormat("MSMQ Audit import is now started, feeding audit messages from: {0}", Settings.AuditQueue); countDownEvent.Idle += OnIdle; Logger.Debug("Ready to BeginPeek"); queuePeeker.BeginPeek(); }
/// <summary> /// Starts the transport. /// </summary> public void Start() { if (RoutableTransportMode == RoutableTransportMode.OnDemand) { int workersThreads; int ioThreads; ThreadPool.GetMaxThreads(out workersThreads, out ioThreads); ThreadPool.SetMaxThreads(100 * Environment.ProcessorCount, ioThreads); ThreadPool.SetMinThreads(50, 50); } CheckConfiguration(); CreateQueuesIfNecessary(); if (ErrorQueue != null) { _errorQueue = new MessageQueue(MsmqUtilities.GetFullPath(ErrorQueue)); } if (!string.IsNullOrEmpty(InputQueue)) { IPluginQueue inputQueue = PluginQueueFactory.Create(InputQueue); IPluginQueue commandQueue = PluginQueueFactory.Create(UiCommandInputQueue); if (PurgeOnStartup) { inputQueue.Purge(); commandQueue.Purge(); } Logger.Info(LoggerContext.New(inputQueue.Name), "starting..."); Logger.Info(LoggerContext.New(commandQueue.Name), "starting..."); var factory = new MsmqRouterFactory(Logger, TimeSpan.FromSeconds(SecondsToWaitForMessage), GetTransactionTypeForSend, GetTransactionTypeForReceive()); _inputQueueRouter = CreateAndStartMainMessageConsumer(factory); _uiQueueRouter = CreateAndStartUiMessageConsumer(factory); Logger.Info(LoggerContext.New(inputQueue.Name), "started."); Logger.Info(LoggerContext.New(commandQueue.Name), "started."); _queue = inputQueue; } }
void ISendMessages.Send(TransportMessage message, Address address) { var queuePath = MsmqUtilities.GetFullPath(address); using (var q = new MessageQueue(queuePath, QueueAccessMode.Send)) { var toSend = MsmqUtilities.Convert(message); toSend.UseDeadLetterQueue = UseDeadLetterQueue; toSend.UseJournalQueue = UseJournalQueue; if (message.ReplyToAddress != null) { toSend.ResponseQueue = new MessageQueue(MsmqUtilities.GetReturnAddress(message.ReplyToAddress.ToString(), address.ToString())); } try { q.Send(toSend, GetTransactionTypeForSend()); } catch (MessageQueueException ex) { if (ex.MessageQueueErrorCode == MessageQueueErrorCode.QueueNotFound) { throw new QueueNotFoundException { Queue = address } } ; throw; } message.Id = toSend.Id; } }
/// <summary> /// Sends a message to the specified destination. /// </summary> /// <param name="m">The message to send.</param> /// <param name="destination">The address of the destination to send the message to.</param> public void Send(TransportMessage m, string destination) { var address = MsmqUtilities.GetFullPath(destination); using (var q = new MessageQueue(address, false, true, QueueAccessMode.Send)) { var toSend = new Message(); if (m.Body == null && m.BodyStream != null) { toSend.BodyStream = m.BodyStream; } else { MessageSerializer.Serialize(m.Body, toSend.BodyStream); } if (m.CorrelationId != null) { toSend.CorrelationId = m.CorrelationId; } toSend.Recoverable = m.Recoverable; if (!string.IsNullOrEmpty(m.ReturnAddress)) { toSend.ResponseQueue = new MessageQueue(MsmqUtilities.GetFullPath(m.ReturnAddress), false, true); } FillLabel(toSend, m); if (m.TimeToBeReceived < MessageQueue.InfiniteTimeout) { toSend.TimeToBeReceived = m.TimeToBeReceived; } if (m.Headers != null && m.Headers.Count > 0) { var sourceOutputheader = m.Headers.FirstOrDefault(h => h.Key == TpUnicastBus.SourceQueue); if (sourceOutputheader != null) { m.Headers.Remove(sourceOutputheader); } using (var stream = new MemoryStream()) { headerSerializer.Serialize(stream, m.Headers); toSend.Extension = stream.GetBuffer(); } } toSend.AppSpecific = (int)m.MessageIntent; try { int attempt = 0; while (true) { try { q.Send(toSend, GetTransactionTypeForSend()); break; } catch (MessageQueueException sendingEx) { if (sendingEx.MessageQueueErrorCode == MessageQueueErrorCode.InsufficientResources && attempt < SendAttemptCount) { Thread.Sleep(SendAttemptSleepIfFault); attempt++; continue; } throw; } } } catch (MessageQueueException ex) { if (ex.MessageQueueErrorCode == MessageQueueErrorCode.QueueNotFound) { throw new ConfigurationErrorsException("The destination queue '" + destination + "' could not be found. " + "It may also be the case that the given queue just hasn't been created yet, or has been deleted." , ex); } throw; } m.Id = toSend.Id; } }
/// <summary> /// May throw a timeout exception if a message with the given id cannot be found. /// </summary> public void ReturnMessageToSourceQueue(string messageId) { using (var scope = new TransactionScope()) { try { var message = queue.ReceiveById(messageId, TimeoutDuration, MessageQueueTransactionType.Automatic); var headers = MsmqUtilities.ExtractHeaders(message); string failedQ; if (!headers.TryGetValue(Headers.FailedQ, out failedQ)) { Console.WriteLine("ERROR: Message does not have a header indicating from which queue it came. Cannot be automatically returned to queue."); return; } using (var q = new MessageQueue(MsmqUtilities.GetFullPath(MsmqAddress.Parse(failedQ)))) { q.Send(message, MessageQueueTransactionType.Automatic); } Console.WriteLine("Success."); scope.Complete(); } catch (MessageQueueException ex) { if (ex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout) { Console.WriteLine(NoMessageFoundErrorFormat, messageId); uint messageCount = 0; foreach (var m in queue.GetAllMessages()) { messageCount++; var headers = MsmqUtilities.ExtractHeaders(m); var originalId = GetOriginalId(headers); if (string.IsNullOrEmpty(originalId) || messageId != originalId) { if (messageCount % ProgressInterval == 0) { Console.Write("."); } continue; } Console.WriteLine(); Console.WriteLine("Found message - going to return to queue."); using (var tx = new TransactionScope()) { var failedQueue = headers[Headers.FailedQ]; using (var q = new MessageQueue(MsmqUtilities.GetFullPath(MsmqAddress.Parse(failedQueue)))) { q.Send(m, MessageQueueTransactionType.Automatic); } queue.ReceiveByLookupId(MessageLookupAction.Current, m.LookupId, MessageQueueTransactionType.Automatic); tx.Complete(); } Console.WriteLine("Success."); scope.Complete(); return; } Console.WriteLine(); Console.WriteLine(NoMessageFoundInHeadersErrorFormat, messageId); } } } }
/// <summary> /// Sends a message to the specified destination. /// </summary> /// <param name="m">The message to send.</param> /// <param name="destination">The address of the destination to send the message to.</param> public void Send(TransportMessage m, string destination) { var address = MsmqUtilities.GetFullPath(destination); using (var q = new MessageQueue(address, false, true, QueueAccessMode.Send)) { var toSend = new Message(); if (m.Body == null && m.BodyStream != null) { toSend.BodyStream = m.BodyStream; } else { MessageSerializer.Serialize(m.Body, toSend.BodyStream); } if (m.CorrelationId != null) { toSend.CorrelationId = m.CorrelationId; } toSend.Recoverable = m.Recoverable; if (!string.IsNullOrEmpty(m.ReturnAddress)) { toSend.ResponseQueue = new MessageQueue(MsmqUtilities.GetFullPath(m.ReturnAddress), false, true); } toSend.Label = new MessageLabel(m.WindowsIdentityName, m.IdForCorrelation).ToString(); if (m.TimeToBeReceived < MessageQueue.InfiniteTimeout) { toSend.TimeToBeReceived = m.TimeToBeReceived; } if (m.Headers != null && m.Headers.Count > 0) { using (var stream = new MemoryStream()) { _headerSerializer.Serialize(stream, m.Headers); toSend.Extension = stream.GetBuffer(); } } toSend.AppSpecific = (int)m.MessageIntent; try { int attempt = 0; while (true) { try { q.Send(toSend, GetTransactionTypeForSend()); break; } catch (MessageQueueException sendingEx) { if (sendingEx.MessageQueueErrorCode == MessageQueueErrorCode.InsufficientResources && attempt < SendAttemptCount) { Thread.Sleep(SendAttemptSleepIfFault); attempt++; continue; } throw; } } } catch (MessageQueueException ex) { if (ex.MessageQueueErrorCode == MessageQueueErrorCode.QueueNotFound) { throw new ConfigurationErrorsException("The destination queue '" + destination + "' could not be found. You may have misconfigured the destination for this kind of message (" + m.Body[0].GetType().FullName + ") in the MessageEndpointMappings of the UnicastBusConfig section in your configuration file." + "It may also be the case that the given queue just hasn't been created yet, or has been deleted." , ex); } throw; } m.Id = toSend.Id; } }
/// <summary> /// May throw a timeout exception if a message with the given id cannot be found. /// </summary> /// <param name="messageId"></param> public void ReturnMessageToSourceQueue(string messageId) { using (var scope = new TransactionScope()) { try { var message = queue.ReceiveById(messageId, TimeoutDuration, MessageQueueTransactionType.Automatic); var tm = MsmqUtilities.Convert(message); string failedQ; if (tm.Headers.ContainsKey(Faults.FaultsHeaderKeys.FailedQ)) { failedQ = tm.Headers[Faults.FaultsHeaderKeys.FailedQ]; } else // try to bring failedQ from label, v2.6 style. { failedQ = GetFailedQueueFromLabel(message); if (!string.IsNullOrEmpty(failedQ)) { message.Label = GetLabelWithoutFailedQueue(message); } } if (string.IsNullOrEmpty(failedQ)) { Console.WriteLine("ERROR: Message does not have a header (or label) indicating from which queue it came. Cannot be automatically returned to queue."); return; } using (var q = new MessageQueue(MsmqUtilities.GetFullPath(Address.Parse(failedQ)))) q.Send(message, MessageQueueTransactionType.Automatic); Console.WriteLine("Success."); scope.Complete(); } catch (MessageQueueException ex) { if (ex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout) { Console.WriteLine(NoMessageFoundErrorFormat, messageId); foreach (var m in queue.GetAllMessages()) { var tm = MsmqUtilities.Convert(m); string originalId = null; if (tm.Headers.ContainsKey("NServiceBus.OriginalId")) { originalId = tm.Headers["NServiceBus.OriginalId"]; } if (string.IsNullOrEmpty(originalId) && tm.Headers.ContainsKey("CorrId")) { originalId = tm.Headers["CorrId"]; } if (string.IsNullOrEmpty(originalId) || messageId != originalId) { continue; } Console.WriteLine("Found message - going to return to queue."); using (var tx = new TransactionScope()) { using (var q = new MessageQueue( MsmqUtilities.GetFullPath( Address.Parse(tm.Headers[Faults.FaultsHeaderKeys.FailedQ])))) q.Send(m, MessageQueueTransactionType.Automatic); queue.ReceiveByLookupId(MessageLookupAction.Current, m.LookupId, MessageQueueTransactionType.Automatic); tx.Complete(); } Console.WriteLine("Success."); scope.Complete(); return; } } } } }
public void Send(TransportMessage message, string destination) { var address = MsmqUtilities.GetFullPath(destination); using (var q = new MessageQueue(address, QueueAccessMode.Send)) { var toSend = new Message(); if (message.Body != null) { toSend.BodyStream = new MemoryStream(message.Body); } if (message.CorrelationId != null) { toSend.CorrelationId = message.CorrelationId; } toSend.Recoverable = message.Recoverable; toSend.UseDeadLetterQueue = UseDeadLetterQueue; toSend.UseJournalQueue = UseJournalQueue; if (!string.IsNullOrEmpty(message.ReturnAddress)) { toSend.ResponseQueue = new MessageQueue(MsmqUtilities.GetReturnAddress(message.ReturnAddress, destination)); } if (message.TimeToBeReceived < MessageQueue.InfiniteTimeout) { toSend.TimeToBeReceived = message.TimeToBeReceived; } if (message.Headers == null) { message.Headers = new Dictionary <string, string>(); } if (!message.Headers.ContainsKey(HeaderKeys.IDFORCORRELATION)) { message.Headers.Add(HeaderKeys.IDFORCORRELATION, null); } if (String.IsNullOrEmpty(message.Headers[HeaderKeys.IDFORCORRELATION])) { message.Headers[HeaderKeys.IDFORCORRELATION] = message.IdForCorrelation; } using (var stream = new MemoryStream()) { headerSerializer.Serialize(stream, message.Headers.Select(pair => new HeaderInfo { Key = pair.Key, Value = pair.Value }).ToList()); toSend.Extension = stream.GetBuffer(); } toSend.AppSpecific = (int)message.MessageIntent; try { q.Send(toSend, GetTransactionTypeForSend()); } catch (MessageQueueException ex) { if (ex.MessageQueueErrorCode == MessageQueueErrorCode.QueueNotFound) { throw new QueueNotFoundException { Queue = destination } } ; throw; } message.Id = toSend.Id; } }
static void Main(string[] args) { cmdLine = args; var timeOfMigration = DateTime.UtcNow; var storage = GetSetting("-storageQueue"); var inputStorageQueue = Address.Parse(storage); var destination = GetSetting("-destination"); newTimeoutManagerAddress = Address.Parse(destination); var minAge = GetSetting("-migrateOlderThan"); var minAgeOfTimeouts = TimeSpan.MinValue; if (!string.IsNullOrEmpty(minAge)) { minAgeOfTimeouts = TimeSpan.Parse(minAge); } bus = Configure.With() .DefaultBuilder() .XmlSerializer() .MsmqTransport() .UnicastBus() .SendOnly(); var path = MsmqUtilities.GetFullPath(inputStorageQueue); storageQueue = new MessageQueue(path) { MessageReadPropertyFilter = { LookupId = true } }; if (!storageQueue.Transactional) { throw new Exception(inputStorageQueue + " must be transactional."); } storageQueue.Formatter = new XmlMessageFormatter(new[] { typeof(TimeoutData) }); Console.WriteLine(string.Format("Parsing {0} to find timeouts to migrate", inputStorageQueue)); storageQueue.GetAllMessages().ToList().ForEach( m => { var timeoutData = m.Body as TimeoutData; if (timeoutData == null) //get rid of message { throw new InvalidOperationException("Failed to parse timeout data with id " + m.Id); } if (minAgeOfTimeouts != TimeSpan.MinValue && timeoutData.Time < (timeOfMigration + minAgeOfTimeouts)) { Console.WriteLine(string.Format("Timeout {0} has a expiry ({1}) less than the configured min age of {2} and will be ignored", m.Id, timeoutData.Time, minAgeOfTimeouts)); return; } timeoutsToBeMigrated.Add(new Tuple <TimeoutData, string>(timeoutData, m.Id)); }); Console.WriteLine(string.Format("{0} parsed, {1} timeouts found that will be migrated", inputStorageQueue, timeoutsToBeMigrated.Count())); timeoutsToBeMigrated.ForEach(t => MigrateMessage(t.Item1, t.Item2)); Console.WriteLine(string.Format("Migration completed successfully")); }