Reading from Queue using Java



import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;


public class MQMessageReader {
private MQQueueManager qMgr; 
private int openOptions;
private MQQueue replyQueue; 
MQMessage replyMsg = new MQMessage();
public MQMessageReader()
{}

public MQMessageReader(String qManager, String aRequestQueue) {
try {
MQEnvironment.hostname = "localhost";
MQEnvironment.port = 1414;
MQEnvironment.channel = "SYSTEM.ADMIN.SVRCONN";   
//MQEnvironment.enableTracing(1, System.out);           //  Set MQ tracing on

try{
qMgr = new MQQueueManager(qManager);
}catch(MQException e){
Object o = e.exceptionSource;
}
openOptions = MQC.MQOO_OUTPUT;
// Now specify the queue that we wish to open, and the open options...
replyQueue = qMgr.accessQueue(aRequestQueue, openOptions,null,null,null);             
}catch (MQException ex){
//System.exit(1);
}
catch (Exception e){
//System.exit(1);
}
}
public static void main(String arg[])
{
MQMessageReader messageReader = new MQMessageReader("QM","SEND");
messageReader.receive("AMQ QM          MÈñN à ", "SEND", 18000); // should be messageID,queueName & waitTime
}
public String receive(String messageId, String receiveFromQueue, int waitTime) {
String ssoResponse = null;
try {
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT
| MQC.MQOO_INQUIRE | MQC.MQOO_BROWSE;
MQQueue queue = qMgr.accessQueue(
receiveFromQueue, openOptions, null, null,
null);
MQException.log = null;
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = MQC.MQGMO_WAIT | MQC.MQGMO_CONVERT;
gmo.waitInterval = waitTime;
byte byteArray[] = messageId.getBytes();
queue.get(replyMsg, gmo);
ssoResponse = replyMsg.readStringOfByteLength(replyMsg.getTotalMessageLength());
System.out.println(ssoResponse);
} catch (Exception e) {
e.printStackTrace();
}
return ssoResponse;
}
}

2 comments: