private void ProcessQueue(IModel model, CancellationToken cancellation) { while (!cancellation.IsCancellationRequested) { LinkProducerMessage message; try { message = _messageQueue.Wait(cancellation); } catch (Exception ex) { if (cancellation.IsCancellationRequested) { continue; } _logger.Error($"Cannot read message from queue: {ex}"); return; } var seq = model.NextPublishSeqNo; var corellationId = _ackQueue.Add(message, seq); var properties = model.CreateBasicProperties(); properties.Extend(message.Properties); if (_configuration.SetUserId) { properties.UserId = _channel.Connection.UserId; } if (properties.Headers == null) { properties.Headers = new Dictionary <string, object>(); } properties.Headers[CorrelationHeader] = Encoding.UTF8.GetBytes(corellationId); try { model.BasicPublish( _exchange.Name, message.PublishProperties.RoutingKey ?? "", message.PublishProperties.Mandatory ?? false, properties, message.Body ); } catch (Exception ex) { _logger.Error($"Cannot publish message: {ex.Message}"); return; } if (!ConfirmsMode) { _ackQueue.Ack(seq, false); } } }
private void ProcessActionQueue(IModel model, CancellationToken cancellation) { while (!cancellation.IsCancellationRequested) { LinkConsumerMessageAction action; try { action = _actionQueue.Wait(cancellation); } catch (Exception ex) { if (cancellation.IsCancellationRequested) { continue; } _logger.Error($"Cannot read message from action queue: {ex}"); return; } try { switch (action.Strategy) { case LinkConsumerAckStrategy.Ack: model.BasicAck(action.Seq, false); break; case LinkConsumerAckStrategy.Nack: case LinkConsumerAckStrategy.Requeue: model.BasicNack(action.Seq, false, action.Strategy == LinkConsumerAckStrategy.Requeue); break; default: throw new NotImplementedException($"AckStrategy {action.Strategy} not supported"); } } catch (Exception ex) { _logger.Error($"Cannot publish message: {ex.Message}"); return; } } }