protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); var randomQueuename = _model.Model.QueueDeclare().QueueName; _model.Model.QueueBind(queue: randomQueuename, exchange: Utility.EXCHANGE_NAME, routingKey: ""); _logger.LogInformation("waiting for logs"); var consumer = new AsyncEventingBasicConsumer(_model.Model); consumer.Received += async(model, ea) => { System.ReadOnlyMemory <byte> body = ea.Body; var message = Encoding.UTF8.GetString(body.ToArray()); _logger.LogInformation("logs recevied: {message}", message); await Task.CompletedTask; }; _model.Model.BasicConsume(randomQueuename, true, consumer); await Task.Delay(5000, stoppingToken); } }
public static void Received() { var factory = new ConnectionFactory() { HostName = "localhost" }; using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); channel.QueueDeclare(queue: Utility.QUEUE_NAME, durable: false, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { System.ReadOnlyMemory <byte> body = ea.Body; var message = Encoding.UTF8.GetString(body.ToArray()); WriteLine(" [x] Received {0}", message); }; channel.BasicConsume(queue: Utility.QUEUE_NAME, autoAck: true, consumer: consumer); WriteLine(" Press [enter] to exit."); ReadLine(); }
public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, System.ReadOnlyMemory <byte> body) { var message = Encoding.UTF8.GetString(body.ToArray()); var responseMessage = string.Empty; if (message == "carbonara") { System.Console.WriteLine("Creating Carbonara"); responseMessage = "Creating Carbonara"; } else { System.Console.WriteLine("We are out of products"); responseMessage = "We are out of products"; } var replyMessageProperties = this.Model.CreateBasicProperties(); replyMessageProperties.CorrelationId = properties.CorrelationId; System.Console.WriteLine(properties.ReplyTo); System.Console.WriteLine(properties.CorrelationId); this.Model.BasicPublish("", properties.ReplyTo, replyMessageProperties, Encoding.UTF8.GetBytes(responseMessage)); }
private async Task Consumer_Received(object sender, BasicDeliverEventArgs e) { System.ReadOnlyMemory <byte> body = e.Body; var message = Encoding.UTF8.GetString(body.ToArray()); var eventName = e.RoutingKey; await ProcessEvent(message, eventName).ConfigureAwait(false); ((AsyncEventingBasicConsumer)sender).Model.BasicAck(deliveryTag: e.DeliveryTag, multiple: false); }