Exemplo n.º 1
0
        public long Store(IRawMessage message, IQueueSettings queueSettings)
        {
            var retry = new MessageDocument
            {
                ApplicationId = queueSettings.ApplicationId
            };

            headerParser.AddHeaderInformation(message, retry);
            retry.Body = message.Body;
            var expiry = DateTime.UtcNow.AddHours(retry.IsError ? queueSettings.ErrorDocumentExpirationInHours : queueSettings.DocumentExpirationInHours);

            long originalId = -1;

            long.TryParse(message.Headers[Headers.Retry], out originalId);
            long documentIdToReturn = -1;

            //deal with rare transients that happen under load
            var policy = Policy.Handle <Exception>().WaitAndRetry(new TimeSpan[] { TimeSpan.FromMilliseconds(5), TimeSpan.FromMilliseconds(10) }, (exception, retryDelay, context) =>
            {
                logger.ErrorException($"Retrying storage of message document with delay {retryDelay} after exception", exception);
            });

            policy.Execute(() =>
            {
                documentIdToReturn = SaveDocument(queueSettings, originalId, retry, expiry);
            });

            return(documentIdToReturn);
        }
Exemplo n.º 2
0
        public void TempSave(string seller, List <ILot> lots, IQueueSettings queueSettings)
        {
            if (!Directory.Exists($"{queueSettings.SaveTempFolderPath}"))
            {
                DirectoryInfo folder = Directory.CreateDirectory($"{queueSettings.SaveTempFolderPath}");
                folder.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
            }

            string filePath = $"{queueSettings.SaveTempFolderPath}\\{queueSettings.TempFilePrefix} {seller}.csv";

            string[] data = new string[lots.Count];

            for (int i = 0; i < data.Length; i++)
            {
                data[i] = lots[i].GetData();
            }

            try
            {
                File.WriteAllLines(filePath, data, Encoding.UTF8);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Ошибка при сохранении временного файла {filePath}.\r\nError: {e.Message}");
            }
        }
Exemplo n.º 3
0
 public RmqBus(IQueueSettings settings, ISerializer serializer, IMemoryStreamPool memoryStreamPool)
 {
     Settings         = settings;
     Serializer       = serializer;
     MemoryStreamPool = memoryStreamPool;
     Connections      = new ConnectionPool();
     Subscriptions    = new RmqSubscriptions(this);
 }
Exemplo n.º 4
0
        internal QueuePoller(Guid key, IQueueSettings queueSettings)
        {
            Verify.RequireNotNull(key, "key");
            Verify.RequireNotNull(queueSettings, "queueSettings");

            QueueSettings = queueSettings;
            Key           = key;
        }
Exemplo n.º 5
0
 public void CreateQueueExchangeBinding(IQueueSettings queueSettings)
 {
     _queueChannelManager
     .GetChannel()
     .QueueBind(queueSettings.QueueName,
                queueSettings.ExchangeName,
                queueSettings.RoutingKey,
                null);
 }
 public IntegrationEventQueue(IQueueChannelManager responseQueueChannelManager,
                              IQueueSettingsResolver queueSettingsResolver,
                              IQueueFactory responseQueueFactory,
                              ILoggerFactory loggerFactory)
 {
     _logger = loggerFactory.GetLogger <IntegrationEventQueue>();
     _responseQueueChannelManager = responseQueueChannelManager;
     _settings = queueSettingsResolver.GetSettings(nameof(IntegrationEventQueue));
     responseQueueFactory.SetupQueue(_settings);
 }
Exemplo n.º 7
0
 public void CreateExchange(IQueueSettings queueSettings)
 {
     _queueChannelManager
     .GetChannel()
     .ExchangeDeclare(exchange: queueSettings.ExchangeName,
                      type: queueSettings.ExchangeType,
                      durable: queueSettings.IsQueueDurable,
                      autoDelete: queueSettings.DeleteQueueIfNoConsumers,
                      arguments: null);
 }
Exemplo n.º 8
0
 public TransactionProcessorIntegrationEventQueue(IQueueChannelManager responseQueueChannelManager,
                                                  IQueueSettingsResolver queueSettingsResolver,
                                                  IQueueFactory responseQueueFactory,
                                                  ILoggerFactory loggerFactory)
 {
     _logger = loggerFactory.GetLogger <IntegrationEventQueue>();
     _responseQueueChannelManager = responseQueueChannelManager;
     _settings = queueSettingsResolver.GetSettings(nameof(IntegrationEventQueue));
     responseQueueFactory.CreateExchange(_settings);
 }
 private void SaveDocument(IQueueSettings queueSettings, MessageDocument document, DateTime expiry)
 {
     using (var session = documentStore.OpenSessionForDefaultTenant())
     {
         session.Store(document);
         session.Advanced.GetMetadataFor(document)["Raven-Expiration-Date"] = new RavenJValue(expiry);
         session.SaveChanges();
         logger.Trace("Saved document for message with id {0} from {1}", document.Id, queueSettings.LogInfo);
     }
 }
Exemplo n.º 10
0
 public void CreateDeadLetterQueue(IQueueSettings queueSettings)
 {
     _queueChannelManager
     .GetChannel()
     .QueueDeclare(queue: queueSettings.DeadLetterQueueSettings.QueueName,
                   durable: queueSettings.DeadLetterQueueSettings.IsQueueDurable,
                   exclusive: queueSettings.DeadLetterQueueSettings.IsQueueExclusive,
                   autoDelete: queueSettings.DeadLetterQueueSettings.DeleteQueueIfNoConsumers,
                   arguments: queueSettings.DeadLetterQueueSettings.QueueArguments);
 }
Exemplo n.º 11
0
        public Queue(ISenderAgent senderAgent, IQueueEvents queueEvents, IMetaDataBusiness metaDataBusiness, IQueueSettings queueSettings)
        {
            _pointValidator = new PointValidator();
            queueEvents.OnDebugMessageEvent($"Preparing new queue with target {senderAgent.TargetDescription}.");
            _queueAction = new QueueAction(queueEvents, GetQueueInfo);

            _senderAgent      = senderAgent;
            _queueEvents      = queueEvents;
            _metaDataBusiness = metaDataBusiness;
            _queueSettings    = queueSettings;
        }
Exemplo n.º 12
0
        public void SetupQueue(IQueueSettings queueSettings)
        {
            CreateExchange(queueSettings);
            CreateQueue(queueSettings);
            CreateQueueExchangeBinding(queueSettings);

            if (queueSettings.DeadLetterQueueSettings != null)
            {
                CreateDeadLetterQueue(queueSettings);
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Configure bus based on QueueSettings.
 /// </summary>
 /// <param name="queueSettings">QueueSettings</param>
 /// <returns>IBusControl</returns>
 private static IBusControl ConfigureBus(IQueueSettings queueSettings)
 {
     try
     {
         return(BusConfigurator.ConfigureBus(queueSettings));
     }
     catch (Exception e)
     {
         throw new Exception("BasePublisher.ConfigureBus : " + e.Message);
     }
 }
        public async Task Configure(IQueueSettings settings)
        {
            Settings = settings;
            _client  = new QueueClient(Settings.ConnectionString, Settings.QueueName);

            Response queue = await _client.CreateIfNotExistsAsync();

            if (queue != null)
            {
                Console.WriteLine($"Fila {Settings.QueueName} foi criada");
            }
        }
Exemplo n.º 15
0
 public QueueListener(ILoggerFactory loggerFactory,
                      IQueueChannelManager queueChannelManager,
                      IQueueSettingsResolver queueSettingsResolver,
                      IQueueFactory queueFactory,
                      IServiceProvider serviceProvider)
 {
     _serviceProvider = serviceProvider;
     _logger          = loggerFactory.GetLogger <QueueListener>();
     _settings        = queueSettingsResolver.GetSettings(nameof(QueueListener));
     _channel         = queueChannelManager.GetChannel();
     _consumer        = new EventingBasicConsumer(_channel);
     queueFactory.SetupQueue(_settings);
 }
Exemplo n.º 16
0
 /// <summary>
 /// Build SendToUri based on QueueSettings.
 /// </summary>
 /// <param name="queueSettings">QueueSettings</param>
 /// <returns>string Uri</returns>
 private static string SendToUri(IQueueSettings queueSettings)
 {
     try
     {
         var uri = new StringBuilder(queueSettings.RabbitMqUri);
         uri.Append(string.IsNullOrEmpty(queueSettings.VirtualHost) ? "" : queueSettings.VirtualHost + "/");
         return(uri.ToString());
     }
     catch (Exception e)
     {
         throw new Exception("BasePublisher.SendToUri : " + e.Message);
     }
 }
Exemplo n.º 17
0
 public void CreateQueue(IQueueSettings queueSettings)
 {
     _queueChannelManager
     .GetChannel()
     .QueueDeclare(queue: queueSettings.QueueName,
                   durable: queueSettings.IsQueueDurable,
                   exclusive: queueSettings.IsQueueExclusive,
                   autoDelete: queueSettings.DeleteQueueIfNoConsumers,
                   arguments: new Dictionary <string, object>()
     {
         { "x-message-ttl", 3600000 },
         { "x-dead-letter-exchange", "" },
         { "x-dead-letter-routing-key", queueSettings.DeadLetterQueueSettings?.RoutingKey }
     });
 }
Exemplo n.º 18
0
        /// <summary>
        /// Send provided message to the specified queue.
        /// </summary>
        /// <typeparam name="T">Message Type</typeparam>
        /// <param name="queueSettings">QueueSettings ojbect</param>
        /// <param name="message">Message</param>
        /// <param name="auditConnectionStringName">string Name of Connection String</param>
        /// <param name="auditSchemaName">string Schema Name</param>
        /// <param name="auditTableName">string Table Name (AuditRecord)</param>
        /// <returns>Task object</returns>
        public static async Task SendMessage <T>(IQueueSettings queueSettings, T message, DateTime?scheduledTime = null, string auditConnectionStringName = null, string auditSchemaName = null, string auditTableName = null) where T : class
        {
            try
            {
                //Check if the exchange and queues exist
                if (ValidateExchange(queueSettings, message))
                {
                    // Configure bus
                    var bus = ConfigureBus(queueSettings);

                    // Audit
                    // TODO: Audit store not implemented in POC, but all that really needs to be done is add a connection string to the config
                    // and point it to a table that matches the MassTransit audit table schema, MassTransit EntityFrameworkAuditStore will take care of creating the connection and writing to the table
                    if (auditConnectionStringName != null)
                    {
                        bus.ConnectSendAuditObservers(new EntityFrameworkAuditStore(auditConnectionStringName, auditSchemaName + "." + auditTableName), null);
                    }

                    // Helper function to construct destination uri
                    var sendToUri = SendToUri(queueSettings);

                    // If a scheduled time is provided, set the queue endpoint to the scheduler queue name other set it to the message type
                    var endPoint = (scheduledTime.HasValue) ?
                                   await bus.GetSendEndpoint(new Uri(queueSettings.RabbitMqUri + queueSettings.VirtualHost + "/" + queueSettings.SchedulerQueueName))
                        : await bus.GetSendEndpoint(new Uri(sendToUri + queueSettings.QueueName));

                    //Start the bus
                    await bus.StartAsync();

                    // If a scheduled time is provided, MassTransit provides a ScheduleSend message to use, otherwise simply use Send
                    if (scheduledTime.HasValue)
                    {
                        await endPoint.ScheduleSend(new Uri(sendToUri + queueSettings.QueueName), scheduledTime.Value, message);
                    }
                    else
                    {
                        await endPoint.Send(message);
                    }

                    //Stop the bus
                    await bus.StopAsync();
                }
            }
            catch (Exception e)
            {
                throw new Exception("BasePublisher.SendMessage : " + e.Message);
            }
        }
 private void StartPollingQueue(IQueueSettings queueSettings)
 {
     queuePollers.Add(Task.Factory.StartNew(() =>
     {
         string queueLogInfo = string.Format("queue {0} in application {1}({2})", queueSettings.QueueName,
                                             queueSettings.ApplicationName, queueSettings.ApplicationId);
         try
         {
             var queuePoller = queuePollerFactory.Create(queueSettings, cancellationToken);
             queuePoller.Poll();
         }
         catch (Exception err)
         {
             logger.Error("Failed in queue poller for {0} with error {1}", queueLogInfo, err);
             throw;
         }
     }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default));
 }
Exemplo n.º 20
0
        private long SaveDocument(IQueueSettings queueSettings, long originalId, MessageDocument retry, DateTime expiry)
        {
            long documentIdToReturn = -1;

            using (var session = documentStore.OpenSessionForDefaultTenant())
            {
                MessageDocument originalMessage = null;
                if (originalId > 0)
                {
                    originalMessage = session.Load <MessageDocument>(originalId);
                }
                if (originalMessage != null)
                {
                    if (originalMessage.Retries == null)
                    {
                        originalMessage.Retries = new List <MessageDocument>();
                    }
                    originalMessage.Retries.Add(retry);
                    if (retry.IsError)
                    {
                        originalMessage.AdditionalErrorStatus = AdditionalErrorStatus.Unresolved;
                    }
                    else
                    {
                        originalMessage.AdditionalErrorStatus = AdditionalErrorStatus.Resolved;
                    }
                    session.Advanced.GetMetadataFor(originalMessage)["Raven-Expiration-Date"] = new RavenJValue(expiry);
                    documentIdToReturn = originalMessage.Id;
                    logger.Trace(
                        "Added retry information with additional error status {0} to existing document with id {1} from {2}",
                        retry.AdditionalErrorStatus, originalMessage.Id, queueSettings.LogInfo);
                }
                else
                {
                    session.Store(retry);
                    session.Advanced.GetMetadataFor(retry)["Raven-Expiration-Date"] = new RavenJValue(expiry);
                    documentIdToReturn = retry.Id;
                    logger.Trace("Saved new document for retry message with id {0} from {1}", retry.Id, queueSettings.LogInfo);
                }
                session.SaveChanges();
            }
            return(documentIdToReturn);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Configures the bus based on QueueSettings
        /// </summary>
        /// <param name="queueSettings">QueueSettings objeect</param>
        /// <param name="registrationAction"></param>
        /// <returns>IBusControl</returns>
        public static IBusControl ConfigureBus(IQueueSettings queueSettings, Action <IRabbitMqBusFactoryConfigurator, IRabbitMqHost> registrationAction = null)
        {
            return(Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                var host = cfg.Host(new Uri(queueSettings.RabbitMqUri + queueSettings.VirtualHost + "/"), hst =>
                {
                    hst.Username(queueSettings.UserName);
                    hst.Password(queueSettings.Password);
                });

                cfg.ConfigureSend(x => x.UseSendExecute(context =>
                {
                    context.Headers.Set("VirtualHost", queueSettings.VirtualHost);
                    context.Headers.Set("QueueName", queueSettings.QueueName);
                }));

                cfg.UseMessageScheduler(new Uri(queueSettings.RabbitMqUri + queueSettings.VirtualHost + "/" + queueSettings.SchedulerQueueName));

                registrationAction?.Invoke(cfg, host);
            }));
        }
Exemplo n.º 22
0
        public QueuePoller(IQueueSettings queueSettings, CancellationToken cancellationToken, IRabbitConnectionFactory rabbitConnectionFactory,
                           IHeaderParser headerParser, IDocumentStore documentStore, IActiveQueuePollers activeQueuePollers, IStoreMessagesFactory storeMessagesFactory)
        {
            Verify.RequireNotNull(queueSettings, "queueSettings");
            Verify.RequireNotNull(cancellationToken, "cancellationToken");
            Verify.RequireNotNull(headerParser, "headerParser");
            Verify.RequireNotNull(documentStore, "documentStore");
            Verify.RequireNotNull(rabbitConnectionFactory, "rabbitConnectionFactory");
            Verify.RequireNotNull(activeQueuePollers, "activeQueuePollers");
            Verify.RequireNotNull(storeMessagesFactory, "storeMessagesFactory");

            QueueSettings                = queueSettings;
            this.cancellationToken       = cancellationToken;
            this.rabbitConnectionFactory = rabbitConnectionFactory;
            this.headerParser            = headerParser;
            this.documentStore           = documentStore;
            this.activeQueuePollers      = activeQueuePollers;
            this.storeMessagesFactory    = storeMessagesFactory;
            Key = Guid.NewGuid();

            messageMeter = Metric.Meter(string.Format("RabbitOperations.QueuePoller.Messages.{0}.{1}", QueueSettings.ApplicationId, QueueSettings.QueueName), Unit.Items, TimeUnit.Seconds, tags: new MetricTags("QueuePoller"));
        }
Exemplo n.º 23
0
        /// <summary>
        /// Verify the exchange infrastructure exists.
        /// </summary>
        /// <typeparam name="T">Message Type</typeparam>
        /// <param name="queueSettings">QueueSettings</param>
        /// <param name="message">Message</param>
        /// <returns>bool</returns>
        private static bool ValidateExchange <T>(IQueueSettings queueSettings, T message)
        {
            try
            {
                //Setup RabbitMQ connection
                var factory = new ConnectionFactory()
                {
                    HostName    = queueSettings.Host,
                    VirtualHost = queueSettings.VirtualHost,
                    UserName    = queueSettings.UserName,
                    Password    = queueSettings.Password
                };

                //Connect to RabbitMQ, will throw an exception for various reasons such as invalid credentials or trying to connect to an invalid virtual host
                using (var connection = factory.CreateConnection())
                {
                    using (var model = connection.CreateModel())
                    {
                        ///PASSIVE QUEUE DECLARATION
                        ///Using the passive declare methods, an exception is thrown if the queue/exchange does not exist
                        ///This is important because we..
                        /// 1) Want to throw an appropriate exception if the queue doesnt exist
                        /// 2) Not allow automatic creation of queues based entirely on user input
                        /// 3) If the queue already exists, no exception is thrown
                        var messageContract = message.GetType().GetInterface("I" + message.GetType().Name);
                        model.ExchangeDeclarePassive(queueSettings.QueueName);
                        model.QueueDeclarePassive(queueSettings.QueueName);
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                throw new Exception("BasePublisher.VerifyExchangeExists : " + e.Message);
            }
        }
Exemplo n.º 24
0
 public QueueConsumerService(IQueueSettings queueSettings) : base(queueSettings)
 {
 }
Exemplo n.º 25
0
 public BaseQueueService(IQueueSettings queueSettings)
 {
     QueueSettings = queueSettings;
 }
Exemplo n.º 26
0
 /// <summary>
 /// Конструктор
 /// </summary>
 /// <param name="queueSettings"></param>
 public Queue(IQueueSettings queueSettings)
 {
     QueueSettings = queueSettings;
 }
 public AzureQueueReceiverService(IQueueSettings settings)
 {
     Configure(settings).Wait();
 }
Exemplo n.º 28
0
 public QueuePublisherService(IQueueSettings queueSettings) : base(queueSettings)
 {
     InitRabbitConnection();
     InitConnectionChannel();
 }