简体   繁体   中英

How can I consume RabbitMq messages with different type of consumers in .Net?

MessageType: "PublishX"

Consumers:

Type1ConsumerX

Type2ConsumerX

Type3ConsumerX

All of the consumers must catch messages immediately, but consume synchronously inside themselves..

For example there are 100 "PublishX" messages in the queue. Type1ConsumerX consumed 30 messages (synchronously), Type2ConsumerX consumed 50 messages(synchronously) , Type3ConsumerX consumed 100 messages(synchronously). How can I know the message is consumed by "all type of consumers" ?

  • Could RabbitMQ/MassTransit PUSH messages to consumers?

  • Could RabbitMQ/MassTransit push messages (merging them) with intervals (1s) for decrease network traffic?

  • Could RabbitMQ/MassTransit push same messages to the different type of Consumers?

If I've understood the question correctly you just need to set up a basic pub/sub pattern. This will allow you to deliver the same message to multiple consumers.

Example publisher:

public static void PublishMessageToFanout()
{
    var factory = new ConnectionFactory { HostName = "localhost" };

    using (var connection = factory.CreateConnection())
    using (var channel = connection.CreateModel())
    {
        channel.ExchangeDeclare("messages", "fanout");

        var message = new Message { Text = "This is a message to send" };

        var json = JsonConvert.SerializeObject(message);
        var body = Encoding.UTF8.GetBytes(json);

        channel.BasicPublish("messages", string.Empty, null, body);
    }
}

Example consumers:

SubscribeToMessages("sms-messages", (s) => Console.WriteLine("SMS Message: {0}", s));
SubscribeToMessages("email-messages", (s) => Console.WriteLine("Email Message: {0}", s));

public static void SubscribeToMessages(string queueName, Action<string> messageAction)
{
    var factory = new ConnectionFactory() { HostName = "localhost" };

    using (var connection = factory.CreateConnection())
    using (var channel = connection.CreateModel())
    {
        channel.ExchangeDeclare("messages", "fanout");
        channel.QueueDeclare(queueName, true, false, false, null);
        channel.QueueBind(queueName, "messages", string.Empty);

        var consumer = new QueueingBasicConsumer(channel);

        channel.BasicConsume(queueName, true, consumer);

        while (true)
        {
            var ea = consumer.Queue.Dequeue();

            var body = ea.Body;
            var message = Encoding.UTF8.GetString(body);
            messageAction(message);
        }
    }
}

If you run the SubscribeToMessages loops in separate processes or console apps you'll see that they both print out the message whenever you call the PublishMessageToFanout . You'll also see that both queues exist in RabbitMQ Management under Queues.

Regarding the MassTransit part of your question

  1. RabbitMQ/MassTransit PUSH messages to consumers? Yes, MassTransit publishes messages to the bus, and then a consumer processes them

  2. Could RabbitMQ/MassTransit push messages (merging them) with intervals (1s) for decrease network traffic? Don't know if there is a feature for this, you could write your own but you would have to be very careful about losing the messages.

  3. Could RabbitMQ/MassTransit push same messages to the different type of Consumers? Yes, multiple consumers can consume the same type of message.

I've written a simple hello world app that shows the basics - http://nodogmablog.bryanhogan.net/2015/04/mass-transit-with-rabbitmq-hello-world/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM