コード例 #1
0
        public Task ConsumeAsync(AsyncMessageConsumer <byte[]> consumer,
                                 CancellationToken stoppingToken)
        {
            if (consumer.Registered)
            {
                throw new ApplicationException("This consumer is already registered");
            }

            if (!_connection.IsConnected)
            {
                _connection.TryConnect();
            }

            var channel = _connection.CreateModel();
            var eventingBasicConsumer = new AsyncEventingBasicConsumer(channel);
            var queue = channel.QueueDeclare().QueueName;
            var topic = consumer.Queue;

            channel.QueueBind(queue: queue, _options.Exchange, routingKey: topic);
            eventingBasicConsumer.Received += async(model, ea) =>
            {
                await consumer.InvokeAsync(ea.Body.ToArray());

                channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
            };
            consumer.OnClosing += x =>
            {
                channel.Close();
            };
            //7. 启动消费者
            channel.BasicConsume(queue: queue, autoAck: false, consumer: eventingBasicConsumer);

            return(Task.CompletedTask);
        }
コード例 #2
0
        public Task ConsumeAsync(AsyncMessageConsumer <byte[]> consumer,
                                 CancellationToken stoppingToken)
        {
            if (consumer.Registered)
            {
                throw new ApplicationException("This consumer is already registered");
            }

            if (!_connection.IsConnected)
            {
                _connection.TryConnect();
            }

            var channel       = _connection.CreateModel();
            var basicConsumer = new AsyncEventingBasicConsumer(channel);

            channel.QueueDeclare(consumer.Queue,
                                 true,
                                 false,
                                 true,
                                 null);
            channel.QueueBind(consumer.Queue, _options.Exchange, consumer.Queue);
            basicConsumer.Received += async(model, ea) =>
            {
                try
                {
                    await consumer.InvokeAsync(ea.Body.ToArray());
                }
                finally
                {
                    channel.BasicAck(ea.DeliveryTag, false);
                }
            };
            consumer.OnClosing += x =>
            {
                channel.Close();
            };
            //7. 启动消费者
            channel.BasicConsume(consumer.Queue, false, basicConsumer);

            return(Task.CompletedTask);
        }
コード例 #3
0
        public Task ConsumeAsync <TMessage>(AsyncMessageConsumer <TMessage> consumer,
                                            CancellationToken stoppingToken)
        {
            var topic     = consumer.Queue;
            var channel   = _modelDict.GetOrAdd(topic, CreateChannel);
            var consumer1 = new AsyncEventingBasicConsumer(channel);
            var queue     = channel.QueueDeclare().QueueName;

            channel.QueueBind(queue: queue, _options.Exchange, routingKey: topic);
            consumer1.Received += async(model, ea) =>
            {
                if (consumer is AsyncMessageConsumer <byte[]> bytesConsumer)
                {
                    await bytesConsumer.InvokeAsync(ea.Body);
                }
                else
                {
                    var message = (TMessage)await ea.Body.DeserializeAsync(stoppingToken);

                    if (message != null)
                    {
                        await consumer.InvokeAsync(message);
                    }
                }

                channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
            };

            consumer.OnClosing += () =>
            {
                channel.Close();
            };
            //7. 启动消费者
            channel.BasicConsume(queue: queue, autoAck: false, consumer: consumer1);

            return(Task.CompletedTask);
        }