Пример #1
0
        private void StartBasicConsume()
        {
            var consumer = new AsyncEventingBasicConsumer(ConsumerChannel);

            consumer.Received += Consumer_Received;
            ConsumerChannel.BasicQos(0, 100, false);
            ConsumerChannel.BasicConsume(
                queue: QueueDeclare.QueueName,
                autoAck: false,
                consumer: consumer);
        }
        protected override void StartBasicConsume()
        {
            Logger.LogTrace("Starting RabbitMQ basic consume");

            if (ConsumerChannel != null)
            {
                var consumer = new AsyncEventingBasicConsumer(ConsumerChannel);

                consumer.Received += ConsumerReceived;

                ConsumerChannel.BasicConsume(
                    queue: QueueName,
                    autoAck: false,
                    consumer: consumer);
            }
            else
            {
                Logger.LogError("StartBasicConsume can't call on ConsumerChannel == null");
            }
        }
Пример #3
0
        private void Connect()
        {
            var logString = $"hostname: {_consumerSettings.Host}, username: {_consumerSettings.Username}, password: {_consumerSettings.Password}, exchangeName: {_consumerSettings.Exchange}, " +
                            $"queueName: {_consumerSettings.Queue}, isDurable: {_consumerSettings.IsDurable}, isAutodelete: {_consumerSettings.AutoDelete}, routingKey: {_consumerSettings.RoutingKey}";


            _logger.LogInformation("Connect: Connecting for {0}", logString);

            _consumer = new EventingBasicConsumer(ConsumerChannel);

            _consumer.Received += (model, ea) =>
            {
                try
                {
                    _logger.LogInformation("Consume: Calling handler for {0}", logString);

                    IBasicProperties props = ea.BasicProperties;

                    // get body
                    byte[] body = ea.Body;

                    var json = Encoding.UTF8.GetString(body);

                    var msg = JsonConvert.DeserializeObject <QMessage>(json, _consumerSerializationSettings);

                    msg.Properties = new QProperties()
                    {
                        AppId           = props.AppId,
                        ClusterId       = props.ClusterId,
                        ContentEncoding = props.ContentEncoding,
                        ContentType     = props.ContentType,
                        CorrelationId   = props.CorrelationId,
                        DeliveryMode    = props.DeliveryMode,
                        Expiration      = props.Expiration,
                        MessageId       = props.MessageId,
                        Priority        = props.Priority,
                        ReplyTo         = props.ReplyTo,
                        Type            = props.Type,
                        UserId          = props.UserId
                    };

                    if (MessageRecieved != null)
                    {
                        MessageRecieved(model, msg);
                    }

                    //callBack(model, msg); // return byte array
                    ConsumerChannel.BasicAck(ea.DeliveryTag, false);
                    _logger.LogInformation("Consume: Acknowledged for {0}", logString);
                }
                catch (Exception ex)
                {
                    _logger.LogError("Consume: Error {0}, Stack: {1}", ex.Message, ex.StackTrace);
                }
            };


            ConsumerChannel.BasicConsume(queue: _consumerSettings.Queue, autoAck: false,
                                         consumer: _consumer);

            _logger.LogInformation("Consume: Connected for {0}", logString);
        }