Writing to Queue using Java

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


public class MQMessageSender {
private MQQueueManager qMgr;
public MQMessageSender() {
MQEnvironment.hostname = "localhost";
MQEnvironment.port = 1414;
MQEnvironment.channel = "SYSTEM.ADMIN.SVRCONN";
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,MQC.TRANSPORT_MQSERIES);
}
public void sendMQMessage(String qName,String messageToPost) {
MQQueue queue = null;
try {
qMgr = new MQQueueManager("QM");
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
queue = qMgr.accessQueue(qName, openOptions, null, null, null);
MQMessage msg = new MQMessage();
msg.format = MQC.MQFMT_STRING;
msg.writeBytes(messageToPost);
MQPutMessageOptions pmo = new MQPutMessageOptions();
queue.put(msg, pmo);
} catch (MQException ex) {//...//}
                   catch (java.io.IOException ex) {//..//}
 finally {
try {
queue.close();
qMgr.disconnect();
} catch (MQException e) {
}
}
}
public static void main(String arg[])
{
MQMessageSender messageSender = new MQMessageSender();
messageSender.sendMQMessage("SEND","Hi Blog");
}
}

No comments:

Post a Comment