[rabbitmq-discuss] helloworld java and .net

David O'Brien davidjohnobrien at gmail.com
Mon Mar 7 22:47:28 GMT 2011


I am attempting to follow
http://www.rabbitmq.com/tutorial-one-java.html but using the .NET
client.

My sender appears to work fine:
private static String QUEUE_NAME = "hello";

        static void Main(string[] args)
        {
            ConnectionFactory factory = new ConnectionFactory();
            factory.HostName = "localhost";

            IConnection connection = factory.CreateConnection();
            IModel channel = connection.CreateModel();

            channel.QueueDeclare(QUEUE_NAME, false, false, false, null);

            string message = "Hello from .NET";
            channel.BasicPublish("", QUEUE_NAME, null,
Encoding.UTF8.GetBytes(message));

            Console.WriteLine(" [x] Sent '" + message + "'");

            channel.Close();
            connection.Close();
        }


My receiver works fine except after the first pass through the try
block it closes the SharedQueue resulting in an exception the next
time a message is received

private static String QUEUE_NAME = "hello";

        static void Main(string[] args)
        {
            ConnectionFactory factory = new ConnectionFactory();
            factory.HostName = "localhost";

            IConnection connection = factory.CreateConnection();
            connection.ConnectionShutdown += new
RabbitMQ.Client.Events.ConnectionShutdownEventHandler(connection_ConnectionShutdown);

            IModel channel = connection.CreateModel();

            channel.QueueDeclare(QUEUE_NAME, false, false, false, null);
            Console.WriteLine(" [*] Waiting for messages. To exit
press CTRL+C");

            QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
            channel.BasicConsume(QUEUE_NAME, true, consumer);

            while (true)
            {
                try
                {
                    BasicDeliverEventArgs e =
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
                    IBasicProperties props = e.BasicProperties;

                    byte[] body = e.Body;
                    String message = Encoding.UTF8.GetString(body);
                    Console.WriteLine(" [x] Received '" + message + "'");
                    // ... process the message
                    channel.BasicAck(e.DeliveryTag, false);
                }
                catch (OperationInterruptedException ex)
                {
                    // The consumer was removed, either through
                    // channel or connection closure, or through the
                    // action of IModel.BasicCancel().
                    break;
                }


                //QueueingBasicConsumer.Delivery delivery =
consumer.nextDelivery();
                //String message = new String(delivery.getBody());

            }

How can set up the .NET client to operate as the Java one does ie it
continues to listen for new messages?


More information about the rabbitmq-discuss mailing list