Пример #1
0
        public static Tuple <EventingBasicConsumer, IModel> SubscribeConsumer(Action <object?, BasicDeliverEventArgs> callback,
                                                                              string destination,
                                                                              IConnection rabbitConn,
                                                                              ILogger logger)
        {
            var channel          = rabbitConn.CreateModel();
            var finalDestination = string.Empty;

            // subscription setup
            channel.BasicQos(0, 1, false); // TODO: improve QoS setup

            if (DestinationParsingTools.IsConsumerTopicDestination(destination))
            {
                finalDestination = ApplySetupTopic(destination, channel, logger);
            }
            else
            {
                finalDestination = ApplySetupQueue(destination, channel, logger);
            }

            logger.LogInformation("=^.^=: Appending new consumer to the channel...");
            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += new EventHandler <BasicDeliverEventArgs>(callback);
            channel.BasicConsume(queue: finalDestination, consumer: consumer);

            logger.LogInformation("=^.^=: Waiting for messages...");
            return(new Tuple <EventingBasicConsumer, IModel>(consumer, channel));
        }
Пример #2
0
        public static string ApplySetupTopic(string destination, IModel channel, ILogger logger)
        {
            logger.LogInformation("=^.^=: Applying setup for topics...");

            var(consumerQueueName, topicName) = DestinationParsingTools
                                                .ParseConsumerTopicDestination(destination);

            // requires a fanout exchange creation
            channel.ExchangeDeclare(topicName, ExchangeType.Fanout, true);
            channel.QueueDeclare(consumerQueueName, true, false, false);
            channel.QueueBind(consumerQueueName, topicName, topicName);

            // for topic consumers -> destination is changed to /topicQ/TopicName/ConsumerName-
            return(consumerQueueName);
        }
Пример #3
0
 public static void Send(byte[] body, IBasicProperties headers, string destination, IModel channel)
 {
     if (DestinationParsingTools.IsTopicDestination(destination))
     {
         channel.ExchangeDeclare(destination, ExchangeType.Fanout, true);
         channel.BasicPublish(
             exchange: destination, // fanout for the queue
             routingKey: "",        // no routing key for fanout exchanges
             mandatory: true,
             basicProperties: headers,
             body: body);
     }
     else
     {
         channel.QueueDeclare(destination, true, false, false);
         channel.BasicPublish(exchange: "",  // queues -> default exchange handles it
                              routingKey: destination,
                              mandatory: true,
                              basicProperties: headers,
                              body: body);
     }
 }