Receiving messages

You can receive messages either synchronously or asynchronously. To receive messages synchronously (get all the messages at one time), call the receive method for the message consumer. This code sample gets all the messages from the queue, then, for each message, it prints a message receipt and acknowledges the message:

Message[] seq = mq.receive(0, DEFAULT_TIMEOUT.value);

for (int m = 0; m < seq.lengeth; m++;)
{
   Message msg = seq[m];
   System.out.println(“Received message: “ + msg.text);
   mq.acknowledge(msg.key);
}

To receive messages asynchronously, implement a message listener and install it on the message consumer, either a topic or a queue. See “Implementing and installing message listeners”.