I have a below code snippet which reads a message from IBM MQ. The message is getting retrieved and also the current depth.
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueReceiver;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSession;
public class HelloConsumer {
/**
* @param args
*/
public HelloConsumer(){
try {
com.ibm.mq.MQQueue defaultLocalQueue;
MQQueueManager qManager=null;
MQEnvironment.hostname = "ctmq005";
MQEnvironment.channel = "CLIENTCONNECTION";
MQEnvironment.port = 1414;
String qMngrStr = "";
qManager = new MQQueueManager(qMngrStr);
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE | MQC.MQ_Q_NAME_LENGTH;
String queueName="test.q01";
System.out.println("accessing::"+queueName);
defaultLocalQueue = qManager.accessQueue(queueName, openOptions);
//set transport properties.
System.out.println("set MQ props");
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
System.out.println("new Queuemanager");
//MQMessage putMessage = new MQMessage();
//String msg = "hello";
// putMessage.writeUTF(msg);
//specify the message options...
MQPutMessageOptions pmo = new MQPutMessageOptions();
// accept
// put the message on the queue
//defaultLocalQueue.put(putMessage, pmo);
MQMessage getMessages = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
System.out.println("get messages::"+gmo.toString());
defaultLocalQueue.get(getMessages, gmo);
int depth = defaultLocalQueue.getCurrentDepth();
byte[] b = new byte[getMessages.getMessageLength()];
System.out.println(depth);
getMessages.readUTF();
System.out.println("Message got from MQ: "+new String(b));
}
catch(Exception jex){
jex.printStackTrace();
}
}
public static void main(String[] args) {
new HelloConsumer();
}
This code reads only one message at a time from the Queue. I want to read multiple messages at a time from the Queue. For example, if there are 4 messages in the queue , I want to read all of them and so some processing for each message(processing code not attached here). Please suggest how to achieve this.