[rabbitmq-discuss] Simple Sender and Receiver in java

Zhihui Jiao jzhihui521 at gmail.com
Mon Jul 23 02:37:02 BST 2012


 channel.basicConsume(QUEUE_NAME, true, consumer);

The second parameter says that autoAck=true, that means the server consider
the message as acked when it send it out to the cosumer.

 In your test scenario, rabbit will send all the two message to your consumer, but
you only consume one, the other are simple lost(or discarded) because of the 
temination of your consumer.  

------------------				 
Zhihui Jiao
2012-07-23

-------------------------------------------------------------
On 2012-07-23 Praveena Kumara <praveena at cumulations.com> wrote:

>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.
>
>
>
>_______________________________________________
>rabbitmq-discuss mailing list
>rabbitmq-discuss at lists.rabbitmq.com
>https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>



More information about the rabbitmq-discuss mailing list