[rabbitmq-discuss] Simple Sender and Receiver in java
Praveena Kumara
praveena at cumulations.com
Mon Jul 23 09:09:08 BST 2012
The issue was solved for me when i modified the Receiver with no_Ack =
false and a call to BasicAck. Thanks to @robthewolf and @Card(by twitter)
for helping .
PFB the modified receiver now.
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, false, consumer);
QueueingConsumer.Delivery delivery = consumer.nextDelivery(10);
/*channel.basicCancel(consumer.getConsumerTag()); */
String message = null;
if (delivery != null) {
message = new String(delivery.getBody());
System.out.println("Reciever .."+message);
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
channel.close();
connection.close();
return message;
On Sunday, July 22, 2012 10:46:10 AM UTC-7, Praveena Kumara 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.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20120723/0fb178a0/attachment.htm>
More information about the rabbitmq-discuss
mailing list