[rabbitmq-discuss] Initiating queue monitoring, but returning to main method with indication of success

Simon MacMullen simon at rabbitmq.com
Wed Feb 22 12:00:39 GMT 2012


Please keep the mailing list CCed.

Rather than pass a QueueingConsumer to basicConsume() and pulling 
deliveries out of it with nextDelivery(), you can override 
handleDelivery() on DefaultConsumer to do whatever you need to to when 
receiving a message. The handleDelivery() callback will then get invoked 
in another thread, although this is invisible to you, so you don't need 
to enter any while loop.

Does this answer your question? It's not clear what is unclear to you :)

Cheers, Simon

On 22/02/12 11:52, McMahon, James S (TASC) wrote:
>       Thank you for the reply and guidance, Simon. Being new to rabbitMQ, I don't have an understanding of what you mean, though. Is there a link to an example or reference to which you can direct me?
>
>       Thanks again for your help.
>
> Jim
> ________________________________________
> From: Simon MacMullen [simon at rabbitmq.com]
> Sent: Wednesday, February 22, 2012 6:42 AM
> To: McMahon, James S (TASC)
> Cc: rabbitmq-discuss at lists.rabbitmq.com
> Subject: Re: [rabbitmq-discuss] Initiating queue monitoring, but returning to main method with indication of success
>
> Hi James. You could either fire up a new thread, or (probably simpler)
> use a DefaultConsumer and override its handleDelivery() method rather
> than use QueueingConsumer.
>
> Cheers, Simon
>
> On 21/02/12 20:33, McMahon, James S (TASC) wrote:
>> The rabbitMQ examples all show a use case where the receiver enters into
>> a while( true ) loop that is only terminated when the coder hits Ctrl-C
>> at the command line.
>>
>> In my case, I call a method in a Listener class from a main java method
>> in my primary class, Driver. As a result, control passes to
>> callListener() in Listener.java and loops there endlessly waiting for
>> messages it gets from the direct exchange. In my main() method of
>> Driver.java, it makes a call right now like this:
>>
>> callListener();
>>
>> and inside my callListener method I implement similar to that that which
>> was shown in the tutorials:
>>
>> ConnectionFactory factory = new ConnectionFactory();
>> factory.setHost("localhost");
>> Connection connection = factory.newConnection();
>> Channel channel = connection.createChannel();
>>
>> channel.exchangeDeclare(EXCHANGE_NAME, "topic");
>> String queueName = channel.queueDeclare().getQueue();
>>
>> for(String bindingKey : argv){
>> channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
>> }
>>
>> System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
>>
>> QueueingConsumer consumer = new QueueingConsumer(channel);
>> channel.basicConsume(queueName, true, consumer);
>>
>> while (true) {
>> QueueingConsumer.Delivery delivery = consumer.nextDelivery();
>> String message = new String(delivery.getBody());
>> String routingKey = delivery.getEnvelope().getRoutingKey();
>>
>> // ...my other method calls to process the messages
>> }
>>
>> I lose program control at that point, because this does not return me to
>> my main() in Driver, where I need to accomplish other actions while this
>> listener is monitoring the queue. What I need is for that call to
>> initiate the while loop and listen to the queue, and if it does so
>> successfully return me to my calling program with some indication of
>> success even as the while loop sits there listening to the queue for
>> messages.
>>
>> How can I implement this?
>>
>> Thanks very much.
>>
>> CONFIDENTIALITY NOTICE: This message and any attachments or files
>> transmitted with it (collectively, the "Message") are intended only for
>> the addressee and may contain information that is privileged,
>> proprietary and/or prohibited from disclosure by law or contract. If you
>> are not the intended recipient: (a) please do not read, copy or
>> retransmit the Message; (b) permanently delete and/or destroy all
>> electronic and hard copies of the Message; (c) notify us by return
>> email; and (d) you are hereby notified that any dissemination,
>> distribution or copying of the Message is strictly prohibited.
>>
>>
>>
>> _______________________________________________
>> rabbitmq-discuss mailing list
>> rabbitmq-discuss at lists.rabbitmq.com
>> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>
>
> --
> Simon MacMullen
> RabbitMQ, VMware
> CONFIDENTIALITY NOTICE: This message and any attachments or files transmitted with it (collectively, the "Message") are intended only for the addressee and may contain information that is privileged, proprietary and/or prohibited from disclosure by law or contract. If you are not the intended recipient: (a) please do not read, copy or retransmit the Message; (b) permanently delete and/or destroy all electronic and hard copies of the Message; (c) notify us by return email; and (d) you are hereby notified that any dissemination, distribution or copying of the Message is strictly prohibited.


-- 
Simon MacMullen
RabbitMQ, VMware


More information about the rabbitmq-discuss mailing list