tags:

views:

74

answers:

4

Hi
I am doing a POC with the rabbitMQ and writing a program to add two numbers and getting the response.
The code that we wrote to retrieve the value from the queue is running infinite time( in a while loop) and a line( inside the while loop) waits for some data to be retrieved from the queue; until it gets something from queue it will not go for the next round of the while loop.
Means we are getting the value inside an infinite loop. And I want to use this value for my next processing.


while (true)
{                 
      QueueingConsumer.Delivery delivery1;
      try
      {
            delivery = consumer.nextDelivery();
//The above line waits until delivery get some value
            String result1 = new String(delivery1.getBody());
            System.out.println("Result received-"+ result1);
       }
       catch (InterruptedException ie)
       {
              continue;
       }
 }  // end while

Problem is that I am not able to return the value from the while loop( I want to run it infinite time).
How can I do that so the loop will continue and I will get the processed data outside loop too?

A: 

What do you mean by that exactly?

If you want to stay in the same thread, just call a function (work on the one message received and than read the next).

If you need concurrency (always read, regardless whether you a re processing a message or not) use a producer/ consumer pattern. Create one thread that

  • reads from the mq
  • posts into a (thread-safe) collection
  • signals that
  • goes back to read from the mq

Create at least one mor thread that

  • waits for the signal
  • reads (and removes) message from the (thread-safe) collection
  • process the message
  • goes back to wait for the signal

hth

Mario

Mario The Spoon
A: 

Make your return value having more visibility. So, you'll gain access to it's value

ykatchou
+1  A: 

If 'processing the result' is an operation that completes quickly, then just do it inline, e.g. by calling a separate function that does the actual processing:

void mainLoop()
{
    while (true)
    {                 
        QueueingConsumer.Delivery delivery1;
        try
        {
            delivery = consumer.nextDelivery();
            //The above line waits until delivery get some value
            String result1 = new String(delivery1.getBody());
            System.out.println("Result received-"+ result1);
            processResult(result1);
        }
        catch (InterruptedException ie)
        {
            continue;
        }
    } // end while
}

void processResult(String result)
{
    // Do whatever needs to be done with 'result'
}

If you need processing to happen concurrently with the loop, then you will need to work with multiple threads and the problem gets a bit more complicated.

Grodriguez
A: 

It sounds like you're referring to the yield feature which allows your function to return multiple values. As far as I know this is not supported out-of-the-box in Java but there are some projects available that implement this feature.

Jeroen Rosenberg