Ejemplo n.º 1
0
        private void WriteMessageToDisk(string routingKey, ServiceEventMessage message, string path)
        {
            string filename = Path.Combine(path, string.Concat(routingKey, "_", message.MessageID, ".lck"));

            lock (this)
            {
                string messageFilename = Path.ChangeExtension(filename, ".msg");
                if (!File.Exists(messageFilename))
                {
                    using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, FileOptions.None))
                    {
                        MessageFormatter messageFormatter = new MessageFormatter(stream);
                        messageFormatter.Serialise(message);
                        stream.Flush();
                        stream.Close();
                    }
                    File.Move(filename, messageFilename);
                }
            }
        }
Ejemplo n.º 2
0
        private void WriteQueueToDisk(Queue <QueuedMessage> queue, string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            MessageFormatter messageFormatter = new MessageFormatter();

            while (queue.Count > 0)
            {
                QueuedMessage queuedMessage;
                lock (queue)
                {
                    queuedMessage = queue.Dequeue();
                }
                string routingKey           = queuedMessage.RoutingKey;
                ServiceEventMessage message = queuedMessage.Message;
                if (message.MessageID == Guid.Empty)
                {
                    message.MessageID = Guid.NewGuid();
                }
                if (message.TimeStamp == 0)
                {
                    message.TimeStamp = DateTime.UtcNow.Ticks;
                }
                string filename        = Path.Combine(path, string.Format("{0}_{1}.lck", routingKey, message.MessageID));
                string messageFilename = Path.ChangeExtension(filename, ".msg");
                if (!File.Exists(messageFilename))
                {
                    using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, FileOptions.None))
                    {
                        messageFormatter.Serialise(stream, message);
                    }
                    File.Move(filename, messageFilename);
                }
            }
        }
Ejemplo n.º 3
0
 private void WriteQueueToDisk(Queue<QueuedMessage> queue, string path)
 {
     if (!Directory.Exists(path))
         Directory.CreateDirectory(path);
     MessageFormatter messageFormatter = new MessageFormatter();
     while (queue.Count > 0)
     {
         QueuedMessage queuedMessage;
         lock (queue)
         {
             queuedMessage = queue.Dequeue();
         }
         string routingKey = queuedMessage.RoutingKey;
         ServiceEventMessage message = queuedMessage.Message;
         if (message.MessageID == Guid.Empty)
             message.MessageID = Guid.NewGuid();
         if (message.TimeStamp == 0)
             message.TimeStamp = DateTime.UtcNow.Ticks;
         string filename = Path.Combine(path, string.Format("{0}_{1}.lck", routingKey, message.MessageID));
         string messageFilename = Path.ChangeExtension(filename, ".msg");
         if (!File.Exists(messageFilename))
         {
             using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, FileOptions.None))
             {
                 messageFormatter.Serialise(stream, message);
             }
             File.Move(filename, messageFilename);
         }
     }
 }
Ejemplo n.º 4
0
 private void WriteMessageToDisk(string routingKey, ServiceEventMessage message, string path)
 {
     string filename = Path.Combine(path, string.Concat(routingKey, "_", message.MessageID, ".lck"));
     lock (this)
     {
         string messageFilename = Path.ChangeExtension(filename, ".msg");
         if (!File.Exists(messageFilename))
         {
             using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, FileOptions.None))
             {
                 MessageFormatter messageFormatter = new MessageFormatter(stream);
                 messageFormatter.Serialise(message);
                 stream.Flush();
                 stream.Close();
             }
             File.Move(filename, messageFilename);
         }
     }
 }
Ejemplo n.º 5
0
        private void PublishFireAndForgetMessages()
        {
            bool connectionError = false;
            MemoryStream stream = new MemoryStream(32768);
            while (!_Terminate)
            {
                try
                {
                    connectionError = false;
                    RabbitMQConnection server = _Servers[_CurrentHostIndex];
                    _CurrentHostIndex = (_CurrentHostIndex + 1) % _ServerCount;
                    ConnectionFactory connectionFactory = new ConnectionFactory();
                    connectionFactory.uri = server.Uri;
                    if (!string.IsNullOrEmpty(server.Username))
                        connectionFactory.UserName = server.Username;
                    if (!string.IsNullOrEmpty(server.Password))
                        connectionFactory.Password = server.Password;
                    string hostname = server.Uri.DnsSafeHost;

                    MessageFormatter messageFormatter = new MessageFormatter(stream);
                    using (IConnection connection = connectionFactory.CreateConnection())
                    {
                        using (IModel asyncChannel = connection.CreateModel())
                        {
                            //asynchronousChannel.BasicReturn += new RabbitMQ.Client.Events.BasicReturnEventHandler(Channel_BasicReturn);
                            asyncChannel.ExchangeDeclare(EXCHANGE_NAME, ExchangeType.Topic, true, false, null);
                            while (!_Terminate)
                            {
                                _TriggerPublishFireAndForgetMessages.Reset();
                                int skipCount = 0;
                                TimeSpan delay = TimeSpan.MaxValue;
                                Dictionary<Guid, Object> skippedMessages = new Dictionary<Guid, object>();
                                while (_FireAndForgetMessages.Count > skipCount)
                                {
                                    QueuedMessage queuedMessage;
                                    lock (_FireAndForgetMessages)
                                    {
                                        queuedMessage = _FireAndForgetMessages.Dequeue();
                                    }
                                    if (queuedMessage.Message.QueueAfterTime.HasValue && (queuedMessage.Message.QueueAfterTime.Value > DateTime.UtcNow))
                                    {
                                        TimeSpan messageDelay = queuedMessage.Message.QueueAfterTime.Value.Subtract(DateTime.UtcNow);
                                        if (messageDelay < delay)
                                            delay = messageDelay;
                                        lock (_FireAndForgetMessages)
                                        {
                                            _FireAndForgetMessages.Enqueue(queuedMessage);
                                        }
                                        if (skippedMessages.ContainsKey(queuedMessage.Message.MessageID))
                                        {
                                            skippedMessages.Add(queuedMessage.Message.MessageID, null);
                                            skipCount++;
                                        }
                                        continue;
                                    }
                                    stream.Position = 0;
                                    messageFormatter.Serialise(queuedMessage.Message);
                                    stream.SetLength(stream.Position);
                                    stream.Position = 0;
                                    byte[] data = stream.ToArray();
                                    IBasicProperties properties = asyncChannel.CreateBasicProperties();
                                    properties.CorrelationId = hostname;
                                    properties.DeliveryMode = 2;
                                    properties.MessageId = queuedMessage.Message.MessageID.ToString();
                                    properties.ContentType = messageFormatter.ContentType;

                                    //Transaction was only about ~14 messages a second whereas non is ~1300
                                    if (queuedMessage.Requeued)
                                        asyncChannel.BasicPublish(string.Empty, queuedMessage.RoutingKey, properties, data);
                                    else
                                        asyncChannel.BasicPublish(EXCHANGE_NAME, queuedMessage.RoutingKey, properties, data);
                                }
                                if (!_Terminate)
                                {
                                    if (delay == TimeSpan.MaxValue)
                                        _TriggerPublishFireAndForgetMessages.WaitOne();
                                    else
                                        _TriggerPublishFireAndForgetMessages.WaitOne(delay);
                                }
                            }
                            asyncChannel.Close(200, "Goodbye");
                        }
                        connection.Close();
                    }
                }
                catch (ThreadAbortException)
                {
                    break;
                }
                catch (Exception ex)
                {
                    connectionError = true;
                    ApplicationEventLog.WriteEntry("Flow", string.Format("DALRabbitMQ::PublishMessages - Exception\n{0}", ex), System.Diagnostics.EventLogEntryType.Error);
                }
                if (connectionError)
                    Thread.Sleep(500);
            }
        }
Ejemplo n.º 6
0
        private void PublishFireAndForgetMessages()
        {
            bool         connectionError = false;
            MemoryStream stream          = new MemoryStream(32768);

            while (!_Terminate)
            {
                try
                {
                    connectionError = false;
                    RabbitMQConnection server = _Servers[_CurrentHostIndex];
                    _CurrentHostIndex = (_CurrentHostIndex + 1) % _ServerCount;
                    ConnectionFactory connectionFactory = new ConnectionFactory();
                    connectionFactory.uri = server.Uri;
                    if (!string.IsNullOrEmpty(server.Username))
                    {
                        connectionFactory.UserName = server.Username;
                    }
                    if (!string.IsNullOrEmpty(server.Password))
                    {
                        connectionFactory.Password = server.Password;
                    }
                    string hostname = server.Uri.DnsSafeHost;

                    MessageFormatter messageFormatter = new MessageFormatter(stream);
                    using (IConnection connection = connectionFactory.CreateConnection())
                    {
                        using (IModel asyncChannel = connection.CreateModel())
                        {
                            //asynchronousChannel.BasicReturn += new RabbitMQ.Client.Events.BasicReturnEventHandler(Channel_BasicReturn);
                            asyncChannel.ExchangeDeclare(EXCHANGE_NAME, ExchangeType.Topic, true, false, null);
                            while (!_Terminate)
                            {
                                _TriggerPublishFireAndForgetMessages.Reset();
                                int      skipCount = 0;
                                TimeSpan delay     = TimeSpan.MaxValue;
                                Dictionary <Guid, Object> skippedMessages = new Dictionary <Guid, object>();
                                while (_FireAndForgetMessages.Count > skipCount)
                                {
                                    QueuedMessage queuedMessage;
                                    lock (_FireAndForgetMessages)
                                    {
                                        queuedMessage = _FireAndForgetMessages.Dequeue();
                                    }
                                    if (queuedMessage.Message.QueueAfterTime.HasValue && (queuedMessage.Message.QueueAfterTime.Value > DateTime.UtcNow))
                                    {
                                        TimeSpan messageDelay = queuedMessage.Message.QueueAfterTime.Value.Subtract(DateTime.UtcNow);
                                        if (messageDelay < delay)
                                        {
                                            delay = messageDelay;
                                        }
                                        lock (_FireAndForgetMessages)
                                        {
                                            _FireAndForgetMessages.Enqueue(queuedMessage);
                                        }
                                        if (skippedMessages.ContainsKey(queuedMessage.Message.MessageID))
                                        {
                                            skippedMessages.Add(queuedMessage.Message.MessageID, null);
                                            skipCount++;
                                        }
                                        continue;
                                    }
                                    stream.Position = 0;
                                    messageFormatter.Serialise(queuedMessage.Message);
                                    stream.SetLength(stream.Position);
                                    stream.Position = 0;
                                    byte[]           data       = stream.ToArray();
                                    IBasicProperties properties = asyncChannel.CreateBasicProperties();
                                    properties.CorrelationId = hostname;
                                    properties.DeliveryMode  = 2;
                                    properties.MessageId     = queuedMessage.Message.MessageID.ToString();
                                    properties.ContentType   = messageFormatter.ContentType;

                                    //Transaction was only about ~14 messages a second whereas non is ~1300
                                    if (queuedMessage.Requeued)
                                    {
                                        asyncChannel.BasicPublish(string.Empty, queuedMessage.RoutingKey, properties, data);
                                    }
                                    else
                                    {
                                        asyncChannel.BasicPublish(EXCHANGE_NAME, queuedMessage.RoutingKey, properties, data);
                                    }
                                }
                                if (!_Terminate)
                                {
                                    if (delay == TimeSpan.MaxValue)
                                    {
                                        _TriggerPublishFireAndForgetMessages.WaitOne();
                                    }
                                    else
                                    {
                                        _TriggerPublishFireAndForgetMessages.WaitOne(delay);
                                    }
                                }
                            }
                            asyncChannel.Close(200, "Goodbye");
                        }
                        connection.Close();
                    }
                }
                catch (ThreadAbortException)
                {
                    break;
                }
                catch (Exception ex)
                {
                    connectionError = true;
                    ApplicationEventLog.WriteEntry("Flow", string.Format("DALRabbitMQ::PublishMessages - Exception\n{0}", ex), System.Diagnostics.EventLogEntryType.Error);
                }
                if (connectionError)
                {
                    Thread.Sleep(500);
                }
            }
        }