IBM MQ
IBM MQ调用相关API
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.baoshangbank2; import java.io.IOException; import java.util.stream.Collector.Characteristics; 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; import com.ibm.mq.constants.CMQC; public class MQClient2 { static MQQueueManager qmanager; static final String HOSTNAME = "198.1.242.72"; // MQ HOST static final String CHANNEL = "SVR_CHL_CORE"; // MQ通道 static final int CCSID = 819; // static final int PORT = 10011; // MQ端口 static final int MQ_EXPIRE = 600; static final String CORE_QMC = "QM_CORE_TO_TFBS"; // MQ资源管理器 static final String CORE_ONSEND_QU = "LQ_CORE_REQ"; // 发送队列 static final String ORE_ONRECV_QUE = "RQ_CORE_RES"; // 接收队列 static byte[] token; public static void main(String[] args) throws MQException, IOException, InterruptedException, ClassNotFoundException { conn(); sendMsg(); receiveMsg(); } /** * MQ 开启链接 * @throws MQException */ public static void conn() throws MQException { MQEnvironment.hostname = HOSTNAME; MQEnvironment.channel = CHANNEL; MQEnvironment.CCSID = CCSID; MQEnvironment.port = PORT; qmanager = new MQQueueManager(CORE_QMC); } /** * 发送消息 * @throws MQException * @throws IOException */ public static void sendMsg() throws MQException, IOException { MQQueue inputQueue = null; try { inputQueue = qmanager.accessQueue(CORE_ONSEND_QU, CMQC.MQOO_INQUIRE | CMQC.MQOO_OUTPUT | CMQC.MQOO_INPUT_AS_Q_DEF); MQMessage inputMQMessage = new MQMessage(); inputMQMessage.characterSet = CCSID; inputMQMessage.encoding = CCSID; inputMQMessage.expiry = MQ_EXPIRE; MQPutMessageOptions mqp = new MQPutMessageOptions(); System.out.println("Flags:"+inputMQMessage.messageFlags); inputMQMessage.writeUTF(getXML()); inputQueue.put(inputMQMessage,mqp); }finally { if(inputQueue != null) { inputQueue.close(); } } } /** * 接收消息 * @throws MQException * @throws IOException * @throws ClassNotFoundException */ public static void receiveMsg() throws MQException, IOException, ClassNotFoundException { MQQueue receiveQueue = null; try { receiveQueue = qmanager.accessQueue(CORE_ONSEND_QU, CMQC.MQOO_INQUIRE | CMQC.MQOO_OUTPUT | CMQC.MQOO_INPUT_AS_Q_DEF); //MQGetMessageOptions gmo = new MQGetMessageOptions(); MQMessage outputMsg = new MQMessage(); //outputMsg.messageId = token; outputMsg.expiry = MQ_EXPIRE; System.err.println("队列深度:"+receiveQueue.getCurrentDepth()); if(receiveQueue.getCurrentDepth() != 0) { receiveQueue.get(outputMsg); System.out.println(outputMsg.readUTF()); } }finally { if(receiveQueue != null) { receiveQueue.close(); } } } public static String getXML() { String xml = "<?xml version=1.0 encoding=GB18030?>"; xml +="<CUST_NAME>MQ测试用户</CUST_NAME>"; xml +="<CUST_GENDER>1</CUST_GENDER>"; xml +="<ID_TYPE>10100</ID_TYPE>"; xml +="<ID_NO>130983200001011357</ID_NO>"; xml +="<DUE_DATE>30000101</DUE_DATE>"; xml +="<ID_INST>北京朝阳公安局</ID_INST>"; xml +="<NATION>156</NATION>"; xml +="<ETHNIC>01</ETHNIC>"; xml +="<OCCUPATION>00000</OCCUPATION>"; xml +="<CUST_MOBILE>13800138000</CUST_MOBILE>"; return xml; } }