[rabbitmq-discuss] Quick question: Writing to multiple queues

Demiss Zike habtdemis at gmail.com
Sat Jul 9 20:07:12 BST 2011


Micheal, I looked at the links you sent me and based my new approach on
them. And, I did the following (and no errors but it's not doing what i
wanted):

I declared two queues q1 and q2 and bind them to a fanout exchange. Then I
published the message to the exchange.

On the receiving end of my application, I declared another queue (q3) and
bind it to the same exchange. Then when I consume messages I consume from q3
via the exchange I declared. But I don't expect to get any messages since I
send the messages to queues q1 and q2 but requesting a message from q3. The
problem is, I still get messages. I could be asking a lot but please bear
with me. Please look at the code I have and point me to the right direction:

//EmitLog.cs
using System;
using RabbitMQ.Client;

class EmitLog {
    public static void Main(string[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.HostName = "localhost";
        using (IConnection connection = factory.CreateConnection())
        using (IModel channel = connection.CreateModel()) {
            channel.ExchangeDeclare("logs", "fanout");
            channel.QueueDeclare("q1", false, false, false, null);
            channel.QueueDeclare("q2", false, false, false, null);
            channel.QueueBind("q1", "logs", "");
            channel.QueueBind("q2", "logs", "");

            string message = (args.Length > 0) ? string.Join(" ", args)
                                               : "info: Hello World!";
            byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
            channel.BasicPublish("logs", "", null, body);
            Console.WriteLine(" [x] Sent {0}", message);
        }
    }
}



//ReceiveLogs.cs
using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

class ReceiveLogs {
    public static void Main(string[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.HostName = "localhost";
        using (IConnection connection = factory.CreateConnection())
        using (IModel channel = connection.CreateModel()) {
            channel.ExchangeDeclare("logs", "fanout");

            //string queue_name = channel.QueueDeclare();
            //string queue_name2 = channel.QueueDeclare();
            //channel.QueueDeclare("q1", false, false, false, null);
            channel.QueueDeclare("q3", false, false, false, null);

            //channel.QueueBind("q2", "logs", "");
            channel.QueueBind("q3", "logs", "");
            QueueingBasicConsumer consumer = new
QueueingBasicConsumer(channel);
            //channel.BasicConsume("q2", true, consumer);
            channel.BasicConsume("q3", true, consumer);

            Console.WriteLine(" [*] Waiting for logs." +
                              "To exit press CTRL+C");
            while(true) {
                BasicDeliverEventArgs ea =
                    (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                byte[] body = ea.Body;
                string message = System.Text.Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] {0}", message);
            }
        }
    }
}

Thanks,

On Sat, Jul 9, 2011 at 1:00 PM, Michael Klishin <michael.s.klishin at gmail.com
> wrote:

>
>
> 2011/7/9 Demiss Zike <habtdemis at gmail.com>
>
>> However, I am getting CS1501: No overload for method 'QueueDeclare' takes
>> 2 arguments.
>
>
> According to
> http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.5.1/rabbitmq-dotnet-client-2.5.1-user-guide.pdf,
> QueueDeclare takes at least 4 arguments: name, durability, exclusivity and
> auto-deletion setting. Why don't you just follow examples from that user
> guide, it seems to be pretty extensive and includes code examples.
>
> My question is 1) can i declare multiple queues to then use fanout exchange
>> and publish a message to multiple queues?
>
>
> Yes.
>
>
>> 2) what is the last argument for QueueBind?
>
>
> Binding arguments. More about bindings can be found in AMQP 0.9.1 spec
> document (introductory chapters are a pretty easy read)
> http://bit.ly/hw2ELX.
>
>
> --
> MK
>
> http://github.com/michaelklishin
> http://twitter.com/michaelklishin
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20110709/9b8cd6f2/attachment.htm>


More information about the rabbitmq-discuss mailing list