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

Simon MacMullen simon at rabbitmq.com
Wed Feb 22 11:42:16 GMT 2012


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


More information about the rabbitmq-discuss mailing list