[rabbitmq-discuss] Simple Sender and Receiver in java

Praveena Kumara praveena at cumulations.com
Sun Jul 22 18:46:10 BST 2012


hi 

 
  I am trying to understand the RabitMq server with a sender and a receiver 
program. Now the entire setup works well when the sender sends a single 
message and the same would be received by the receiver.

however when i send two messages( by running sender twice) and run the 
receiver program twice i get only the first message. 

Sender

  ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();


        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        String message = "He12!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println("Sent "+message);
        channel.close();
        connection.close();

Receiver

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, true, false, false, null);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, true, consumer);
    QueueingConsumer.Delivery delivery = consumer.nextDelivery();
   

    String message;
     if (delivery != null) {
        message = new String(delivery.getBody());
        System.out.println("Reciever .."+message);

    }

    channel.close();
    connection.close();


I dont have a  infinite while loop in the receiver code (as shown in the 
getting started page in the rabbitMq site ) because i want to expose  this 
receiver as a web service sending one message at a time.

I also noticed by executing the commond rabbitMqctl list_queues that the 
entire queue contents become zero as and when the control reaches 

    channel.basicConsume(QUEUE_NAME, true, consumer);


Kindly let me know if i am missing something.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20120722/31ab0b8f/attachment.htm>


More information about the rabbitmq-discuss mailing list