Ejemplo n.º 1
0
 private void ReadQueueFromDisk(Queue <QueuedMessage> queue, string path)
 {
     if (Directory.Exists(path))
     {
         MessageFormatter messageFormatter = new MessageFormatter();
         foreach (string item in Directory.GetFiles(path, "*.msg"))
         {
             if (File.Exists(item))
             {
                 ServiceEventMessage message = null;
                 using (FileStream stream = File.OpenRead(item))
                 {
                     message = messageFormatter.Deserialise(stream);
                     stream.Close();
                 }
                 string filename   = Path.GetFileNameWithoutExtension(item);
                 string routingKey = string.Empty;
                 int    index      = filename.IndexOf('_');
                 if (index != -1)
                 {
                     routingKey = filename.Substring(0, index);
                 }
                 if (!string.IsNullOrEmpty(routingKey))
                 {
                     queue.Enqueue(new QueuedMessage()
                     {
                         RoutingKey = routingKey, Message = message
                     });
                 }
                 DeleteMessageFile(item);
             }
         }
     }
 }
Ejemplo n.º 2
0
 private void ReadQueueFromDisk(Queue<QueuedMessage> queue, string path)
 {
     if (Directory.Exists(path))
     {
         MessageFormatter messageFormatter = new MessageFormatter();
         foreach (string item in Directory.GetFiles(path, "*.msg"))
         {
             if (File.Exists(item))
             {
                 ServiceEventMessage message = null;
                 using (FileStream stream = File.OpenRead(item))
                 {
                     message = messageFormatter.Deserialise(stream);
                     stream.Close();
                 }
                 string filename = Path.GetFileNameWithoutExtension(item);
                 string routingKey = string.Empty;
                 int index = filename.IndexOf('_');
                 if (index != -1)
                 {
                     routingKey = filename.Substring(0, index);
                 }
                 if (!string.IsNullOrEmpty(routingKey))
                     queue.Enqueue(new QueuedMessage() { RoutingKey = routingKey, Message = message });
                 DeleteMessageFile(item);
             }
         }
     }
 }
Ejemplo n.º 3
0
        private TimeSpan PublishTransactionalMessages(IModel transactionalChannel, string hostname, string exchange, string path, bool requeue)
        {
            TimeSpan result = TimeSpan.MaxValue;
            MessageFormatter messageFormatter = new MessageFormatter();
            string[] files = Directory.GetFiles(path, "*.msg");
            int skipCount = 0;
            while (files.Length > skipCount)
            {
                skipCount = 0;
                foreach (string item in files)
                {
                    if (File.Exists(item))
                    {
                        byte[] data = null;
                        try
                        {
                            data = File.ReadAllBytes(item);
                        }
                        catch
                        {

                        }
                        if (data != null)
                        {
                            if (requeue)
                            {
                                ServiceEventMessage message = messageFormatter.Deserialise(new MemoryStream(data));
                                if ((message != null) && (message.QueueAfterTime.HasValue) && (message.QueueAfterTime.Value > DateTime.UtcNow))
                                {
                                    TimeSpan delay = message.QueueAfterTime.Value.Subtract(DateTime.UtcNow);
                                    if (delay < result)
                                        result = delay;
                                    skipCount++;
                                    continue;
                                }
                            }
                            string messageFilename = Path.ChangeExtension(item, ".trx");
                            if (File.Exists(messageFilename))
                                DeleteMessageFile(messageFilename);
                            File.Move(item, messageFilename);
                            IBasicProperties properties = transactionalChannel.CreateBasicProperties();
                            properties.CorrelationId = hostname;
                            properties.DeliveryMode = 2;
                            properties.ContentType = messageFormatter.ContentType;
                            string filename = Path.GetFileNameWithoutExtension(item);
                            string routingKey = string.Empty;
                            int index = filename.IndexOf('_');
                            if (index != -1)
                            {
                                routingKey = filename.Substring(0, index);
                                int nextIndex = filename.IndexOf('_', index + 1);
                                if (nextIndex == -1)
                                    properties.MessageId = filename.Substring(index + 1);
                                else
                                    properties.MessageId = filename.Substring(index + 1, nextIndex - index - 1);
                            }
                            transactionalChannel.TxSelect();
                            transactionalChannel.BasicPublish(exchange, routingKey, properties, data);
                            transactionalChannel.TxCommit();
                            DeleteMessageFile(messageFilename);
                        }
                    }
                }
                files = Directory.GetFiles(path, "*.msg");
            }
            return result;
        }
Ejemplo n.º 4
0
        private TimeSpan PublishTransactionalMessages(IModel transactionalChannel, string hostname, string exchange, string path, bool requeue)
        {
            TimeSpan         result           = TimeSpan.MaxValue;
            MessageFormatter messageFormatter = new MessageFormatter();

            string[] files     = Directory.GetFiles(path, "*.msg");
            int      skipCount = 0;

            while (files.Length > skipCount)
            {
                skipCount = 0;
                foreach (string item in files)
                {
                    if (File.Exists(item))
                    {
                        byte[] data = null;
                        try
                        {
                            data = File.ReadAllBytes(item);
                        }
                        catch
                        {
                        }
                        if (data != null)
                        {
                            if (requeue)
                            {
                                ServiceEventMessage message = messageFormatter.Deserialise(new MemoryStream(data));
                                if ((message != null) && (message.QueueAfterTime.HasValue) && (message.QueueAfterTime.Value > DateTime.UtcNow))
                                {
                                    TimeSpan delay = message.QueueAfterTime.Value.Subtract(DateTime.UtcNow);
                                    if (delay < result)
                                    {
                                        result = delay;
                                    }
                                    skipCount++;
                                    continue;
                                }
                            }
                            string messageFilename = Path.ChangeExtension(item, ".trx");
                            if (File.Exists(messageFilename))
                            {
                                DeleteMessageFile(messageFilename);
                            }
                            File.Move(item, messageFilename);
                            IBasicProperties properties = transactionalChannel.CreateBasicProperties();
                            properties.CorrelationId = hostname;
                            properties.DeliveryMode  = 2;
                            properties.ContentType   = messageFormatter.ContentType;
                            string filename   = Path.GetFileNameWithoutExtension(item);
                            string routingKey = string.Empty;
                            int    index      = filename.IndexOf('_');
                            if (index != -1)
                            {
                                routingKey = filename.Substring(0, index);
                                int nextIndex = filename.IndexOf('_', index + 1);
                                if (nextIndex == -1)
                                {
                                    properties.MessageId = filename.Substring(index + 1);
                                }
                                else
                                {
                                    properties.MessageId = filename.Substring(index + 1, nextIndex - index - 1);
                                }
                            }
                            transactionalChannel.TxSelect();
                            transactionalChannel.BasicPublish(exchange, routingKey, properties, data);
                            transactionalChannel.TxCommit();
                            DeleteMessageFile(messageFilename);
                        }
                    }
                }
                files = Directory.GetFiles(path, "*.msg");
            }
            return(result);
        }
Ejemplo n.º 5
0
 void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
 {
     try
     {
         if (_ProcessMessages)
         {
             Interlocked.Increment(ref _MessageHandlingCount);
             MessageFormatter    messageFormatter = new MessageFormatter();
             ServiceEventMessage message          = messageFormatter.Deserialise(new MemoryStream(body));
             lock (_Queues)
             {
                 if (_Queues.ContainsKey(consumerTag))
                 {
                     MessageQueue queue = _Queues[consumerTag];
                     message.DeliveryID = Guid.NewGuid();
                     AckMessageInfo ackMessageInfo = new AckMessageInfo();
                     ackMessageInfo.DeliveryID     = message.DeliveryID;
                     ackMessageInfo.QueueName      = queue.QueueName;
                     ackMessageInfo.DeliveryTag    = deliveryTag;
                     ackMessageInfo.TotalExpected  = queue.Handlers.Count;
                     ackMessageInfo.ChannelHandler = this;
                     queue.AddAckMessageInfo(message.DeliveryID, ackMessageInfo);
                     message.Queue = queue.QueueName;
                     for (int index = 0; index < queue.Handlers.Count; index++)
                     {
                         try
                         {
                             MessageArrivedEventHandler handler = queue.Handlers[index];
                             handler.BeginInvoke(_ConnectionFactory.HostName, message, _Subscription.InvokeCallBack, new MessageArrivedState(handler, message, queue, ackMessageInfo));
                         }
                         catch (Exception ex)
                         {
                             _Subscription.NackMessage(message);
                             ApplicationEventLog.WriteEntry("Flow", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
                         }
                     }
                     if (queue.Handlers.Count == 0)
                     {
                         Thread.Sleep(500);
                         _Model.BasicReject(deliveryTag, true);
                         Interlocked.Decrement(ref _MessageHandlingCount);
                         ApplicationEventLog.WriteEntry("Flow", string.Format("No handlers to process message {0}", consumerTag), System.Diagnostics.EventLogEntryType.Error);
                     }
                 }
                 else
                 {
                     Thread.Sleep(500);
                     _Model.BasicReject(deliveryTag, true);
                     Interlocked.Decrement(ref _MessageHandlingCount);
                     ApplicationEventLog.WriteEntry("Flow", string.Format("HandleBasicDeliver: Failed to locate queue {0}", consumerTag), System.Diagnostics.EventLogEntryType.Error);
                 }
             }
         }
     }
     catch (System.Runtime.Serialization.SerializationException)
     {
         string path = GetBadMessageDirectory();
         File.WriteAllBytes(Path.Combine(path, string.Concat(consumerTag, "_", Guid.NewGuid().ToString())), body);
         _Model.BasicReject(deliveryTag, false);
         Interlocked.Decrement(ref _MessageHandlingCount);
     }
     catch (Exception ex)
     {
         Thread.Sleep(500);
         try
         {
             if (_Model != null)
             {
                 _Model.BasicReject(deliveryTag, true);
             }
             Interlocked.Decrement(ref _MessageHandlingCount);
         }
         catch { }
         ApplicationEventLog.WriteEntry("Flow", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
     }
 }
			void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
			{
				try
				{
					if (_ProcessMessages)
					{
						Interlocked.Increment(ref _MessageHandlingCount);
                        MessageFormatter messageFormatter = new MessageFormatter();
                        ServiceEventMessage message = messageFormatter.Deserialise(new MemoryStream(body));
						lock (_Queues)
						{
							if (_Queues.ContainsKey(consumerTag))
							{
								MessageQueue queue = _Queues[consumerTag];
								message.DeliveryID = Guid.NewGuid();
								AckMessageInfo ackMessageInfo = new AckMessageInfo();
								ackMessageInfo.DeliveryID = message.DeliveryID;
								ackMessageInfo.QueueName = queue.QueueName;
								ackMessageInfo.DeliveryTag = deliveryTag;
								ackMessageInfo.TotalExpected = queue.Handlers.Count;
								ackMessageInfo.ChannelHandler = this;
								queue.AddAckMessageInfo(message.DeliveryID, ackMessageInfo);
								message.Queue = queue.QueueName;
								for (int index = 0; index < queue.Handlers.Count; index++)
								{
									try
									{
										MessageArrivedEventHandler handler = queue.Handlers[index];
										handler.BeginInvoke(_ConnectionFactory.HostName, message, _Subscription.InvokeCallBack, new MessageArrivedState(handler, message, queue, ackMessageInfo));
									}
									catch (Exception ex)
									{
										_Subscription.NackMessage(message);
										ApplicationEventLog.WriteEntry("Flow", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
									}
								}
								if (queue.Handlers.Count == 0)
								{
									Thread.Sleep(500);
									_Model.BasicReject(deliveryTag, true);
									Interlocked.Decrement(ref _MessageHandlingCount);
									ApplicationEventLog.WriteEntry("Flow", string.Format("No handlers to process message {0}", consumerTag), System.Diagnostics.EventLogEntryType.Error);
								}
							}
							else
							{
								Thread.Sleep(500);
								_Model.BasicReject(deliveryTag, true);
								Interlocked.Decrement(ref _MessageHandlingCount);
								ApplicationEventLog.WriteEntry("Flow", string.Format("HandleBasicDeliver: Failed to locate queue {0}", consumerTag), System.Diagnostics.EventLogEntryType.Error);
							}
						}
					}
				}
				catch (System.Runtime.Serialization.SerializationException)
				{
					string path = GetBadMessageDirectory();
					File.WriteAllBytes(Path.Combine(path,string.Concat(consumerTag,"_", Guid.NewGuid().ToString())),body);
					_Model.BasicReject(deliveryTag, false);
					Interlocked.Decrement(ref _MessageHandlingCount);
				}
				catch (Exception ex)
				{
					Thread.Sleep(500);
					try
					{
						if (_Model != null)
							_Model.BasicReject(deliveryTag, true);
						Interlocked.Decrement(ref _MessageHandlingCount);
					}
					catch { }
					ApplicationEventLog.WriteEntry("Flow", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
				}
			}