コード例 #1
0
        public void Enqueue <T>(IEnumerable <T> queueDataModels, string queueName) where T : class, new()
        {
            try
            {
                _connection = _rabbitMqServices.GetConnection();
                using (_channel = _rabbitMqServices.GetModel(_connection))
                {
                    _channel.QueueDeclare(queue: queueName,
                                          durable: true,     // ile in-memory mi yoksa fiziksel olarak mı saklanacağı belirlenir.
                                          exclusive: false,  // yalnızca bir bağlantı tarafından kullanılır ve bu bağlantı kapandığında sıra silinir - özel olarak işaretlenirse silinmez
                                          autoDelete: false, // en son bir abonelik iptal edildiğinde en az bir müşteriye sahip olan kuyruk silinir
                                          arguments: null);  // isteğe bağlı; eklentiler tarafından kullanılır ve TTL mesajı, kuyruk uzunluğu sınırı, vb.

                    var properties = _channel.CreateBasicProperties();
                    properties.Persistent = true;
                    properties.Expiration = RabbitMqConsts.MessagesTTL.ToString();

                    foreach (var queueDataModel in queueDataModels)
                    {
                        var body = Encoding.UTF8.GetBytes(_objectDataConverter.ObjectToJson(queueDataModel));
                        _channel.BasicPublish(exchange: "",
                                              routingKey: queueName,
                                              mandatory: false,
                                              basicProperties: properties,
                                              body: body);
                    }
                }
            }
            catch (Exception ex)
            {
                //loglama yapılabilir
                //throw new Exception(ex.InnerException.Message.ToString());
            }
        }
コード例 #2
0
        public async Task Start()
        {
            try
            {
                _semaphore  = new SemaphoreSlim(RabbitMqConsts.ParallelThreadsCount);
                _connection = _rabbitMqServices.GetConnection();
                _channel    = _rabbitMqServices.GetModel(_connection);
                _channel.QueueDeclare(
                    queue: RabbitMqConsts.RabbitMqConstsList.DominosLocationFileOptionQueue.ToString(),
                    durable: true,
                    exclusive: false,
                    autoDelete: false,
                    arguments: null
                    );

                _channel.BasicQos(0, RabbitMqConsts.ParallelThreadsCount, false);
                _consumer           = new EventingBasicConsumer(_channel);
                _consumer.Received += Consumer_Received;
                await Task.FromResult(
                    _channel.BasicConsume(queue: RabbitMqConsts.RabbitMqConstsList.DominosLocationFileOptionQueue.ToString(),
                                          autoAck: false,
                                          consumer: _consumer));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.InnerException.Message.ToString());
            }
        }