[rabbitmq-discuss] another approach to retrieve message from queue?

Rob Harrop rob at rabbitmq.com
Thu Oct 14 14:51:54 BST 2010


Using Channel.basicConsume along with QueueingConsumer (or another implementation of Consumer) is what I would recommend.

If you want to just grab a message of the queue as a one-shot action then you can use Channel.basicGet:

GetResponse r = channel.basicGet("thequeue", true); // note use of noAck = true

if(r != null) {
 // got a message
 byte[] contents = r.getBody();
 doSomethingWithBody(r);
}

Note the null check - this is important. Channel.basicGet does not block until a message becomes available, instead it just returns null if there is no message.

Hope this helps,

Rob

On 14 Oct 2010, at 07:35, sam_mis wrote:

> 
> Hi
> 
> i am using the following code to read an queue and extract the messages from
> queue. I want to know that there is another approach to retrieve message
> from queue? if there is another approach please describe the details
> information to get the messages from queue. 
> 
> while (runInfinite)
> {
>     QueueingConsumer.Delivery delivery;
>            try
>            {
>              delivery = consumer.nextDelivery();
>            } catch (InterruptedException ie)
>            {
>               continue;
>            }
>         System.out.println("Message received-"
> + new String(delivery.getBody()));
> 
> file included that contain the  code for reading and extracting message:
> --------------------------------------------------------------------------
> http://old.nabble.com/file/p29959643/Result.java Result.java 
> 
> Thanks
> 
> -- 
> View this message in context: http://old.nabble.com/another-approach-to-retrieve-message-from-queue--tp29959643p29959643.html
> Sent from the RabbitMQ mailing list archive at Nabble.com.
> 
> _______________________________________________
> 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