private void PublishMessage(ProducerChannel channel, DaJetMessage message)
        {
            string mapping = null;

            if (Settings.MessageTypeRouting != null)
            {
                _ = Settings.MessageTypeRouting.TryGetValue(message.MessageType, out mapping);
            }

            string routingKey = string.Empty;

            if (mapping == null)
            {
                routingKey = message.MessageType;
            }

            string exchangeName = CreateExchangeName(mapping);

            byte[] messageBytes = Encoding.UTF8.GetBytes(message.MessageBody);

            channel.Properties.Type      = message.MessageType;
            channel.Properties.MessageId = message.Uuid.ToString();
            SetOperationTypeHeader(message, channel.Properties);

            channel.Channel.BasicPublish(exchangeName, routingKey, channel.Properties, messageBytes);
        }
 private void WaitForConfirms(ProducerChannel channel)
 {
     try
     {
         bool confirmed = channel.Channel.WaitForConfirms(TimeSpan.FromSeconds(Settings.ConfirmationTimeout), out bool timedout);
         if (!confirmed)
         {
             if (timedout)
             {
                 SendingExceptions.Enqueue(new OperationCanceledException(PUBLISHER_CONFIRMATION_TIMEOUT_MESSAGE));
             }
             else
             {
                 SendingExceptions.Enqueue(new OperationCanceledException(PUBLISHER_CONFIRMATION_ERROR_MESSAGE));
             }
             SendingCancellation.Cancel();
         }
     }
     catch (OperationInterruptedException rabbitError)
     {
         SendingExceptions.Enqueue(rabbitError);
         if (string.IsNullOrWhiteSpace(rabbitError.Message) || !rabbitError.Message.Contains("NOT_FOUND"))
         {
             SendingCancellation.Cancel();
         }
     }
     catch (Exception error)
     {
         SendingExceptions.Enqueue(error);
         SendingCancellation.Cancel();
     }
 }
        private int PublishMessagesInBackground(object id)
        {
            int channelId = (int)id;

            ProducerChannel producerChannel = ProducerChannels[channelId];

            int messagesSent = 0;

            foreach (Queue <DaJetMessage> queue in producerChannel.Queues)
            {
                while (queue.TryDequeue(out DaJetMessage message))
                {
                    if (SendingCancellation.IsCancellationRequested)
                    {
                        return(0);
                    }

                    try
                    {
                        PublishMessage(producerChannel, message);
                        messagesSent++;
                    }
                    catch (Exception error)
                    {
                        SendingExceptions.Enqueue(error);
                        SendingCancellation.Cancel();
                    }
                }
            }

            if (messagesSent > 0)
            {
                WaitForConfirms(producerChannel);
            }

            return(messagesSent);
        }