/// <inheritdoc /> public async Task Subscribe(Func <HttpResponseMessage, bool> callback) { await _communication.Init(); var declare = _communication.Declare(_exchange, AmqpCommunication.ClientResponseQueue, false); _communication.Declare(_exchange, AmqpCommunication.ClientDelayQueue, false); _communication.Declare(_exchange, AmqpCommunication.ClientErrorQueue, false); _communication.CreateBasicConsumer(declare.QueueName, async(sender, args) => { try { _log.LogTrace($"Queue: {declare.QueueName}. CorrelationId {args.BasicProperties.CorrelationId}. Message size {args.Body.LongLength}"); var correlationId = args.BasicProperties.CorrelationId; if (string.IsNullOrWhiteSpace(correlationId)) { _log.LogError($"Correlation not found. This request not be processed. Please set header {AmqpHeaders.CorrelationId}. Unique value"); SendError(args); return; } _log.LogTrace($"Parsing response {correlationId}"); var response = await _converter.TryParse(args); if (response == null) { _log.LogTrace($"Parsing error {correlationId}. Send error queue"); SendError(args); } callback(response); _communication.Channel.BasicAck(args.DeliveryTag, false); _log.LogTrace($"{correlationId}: Operation success"); } catch (Exception e) { _log.LogError(e, $"Exception by processing amqp message"); SendError(args); } }, (sender, args) => { _log.LogInformation("client consumer register"); }); }
/// <inheritdoc /> public async Task StartAsync(RequestDelegate requestDelegate, CancellationToken cancellationToken) { var host = _option.ServerName.Host; await _communication.Init(); var declare = _communication.Declare(host, AmqpCommunication.ResourceRequestQueue, false); _communication.Declare(host, AmqpCommunication.ResourceDelayQueue, false); _communication.Declare(host, AmqpCommunication.ResourceErrorQueue, false); _communication.CreateBasicConsumer(declare.QueueName, async(sender, args) => { try { _log.LogTrace("Очередь {queueName}. Обработка запроса", declare.QueueName); var context = await _converter.Parse(args, Features); if (context != null) { await requestDelegate(context); await SendResponse(args, context.Response); _communication.Channel.BasicAck(args.DeliveryTag, false); } else { SendError(args); } } catch (Exception e) { // Добавить отправку сообщения в очередь ожидания _log.LogError(e, $"Ошибка при обработке сообщения"); SendError(args); } }, (sender, args) => { _log.LogInformation($"Consumer {args.ConsumerTag} Registered"); }); }