import java.io.IOException;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.MQSimpleConnectionManager;
import com.ibm.mq.constants.MQConstants;
public class MQ_JavaBaseUtil {
private static final MQSimpleConnectionManager CONN_CACH = new MQSimpleConnectionManager();
public static void putMessage( String qmgrName,String qName, String mText ) throws MQException{
MQQueueManager qmgr = null;
try {
qmgr = new MQQueueManager(qmgrName,CONN_CACH);
MQQueue queue = qmgr.accessQueue(qName, MQConstants.MQOO_OUTPUT+MQConstants.MQOO_FAIL_IF_QUIESCING);
MQPutMessageOptions options = new MQPutMessageOptions();
options.options += MQConstants.MQPMO_SYNCPOINT ;
MQMessage msg = new MQMessage();
msg.setStringProperty("str",mText);
queue.put(msg,options);
qmgr.commit();
} catch ( Exception e ){
qmgr.backout();
} finally {
if( qmgr != null ){
qmgr.disconnect();
}
}
}
public static Object getMessage( String qmgrName,String qName ) throws MQException, IOException{
MQQueueManager qmgr = null;
MQMessage msg = null;
try {
qmgr = new MQQueueManager(qmgrName,CONN_CACH);
MQQueue queue = qmgr.accessQueue(qName, MQConstants.MQOO_INPUT_AS_Q_DEF+MQConstants.MQOO_FAIL_IF_QUIESCING);
msg = new MQMessage();
MQGetMessageOptions options = new MQGetMessageOptions();
options.options += MQConstants.MQGMO_SYNCPOINT ;
queue.get(msg ,options);
qmgr.commit();
} catch ( Exception e ) {
qmgr.backout();
} finally {
qmgr.disconnect();
}
return msg.getStringProperty("str");
}
public static void main(String[] args) throws MQException, IOException{
putMessage("qm","q"," hello mq ");
String text = (String)getMessage("qm","q");
System.out.println(text);
}
}
websphere mq java调用mqi通信demo
最新推荐文章于 2023-03-10 17:19:23 发布