今天在做MQ的一个例子程序的时候,遇到了CCSID问题,还好弄了半天终于搞定了 :)
今天把MQ文档中的例子拿来做了一下,在调试的过程中遇到了CCSID问题。
下面先把程序总体思路说一下,软硬件的环境是:我使用一台PC机做服务器,并在这台机器上安装WebSphere MQ的服务器。然后把另外一台PC机作为客户端,并且在这台机器上安装WebSphere MQ的Windows 版客户端。服务器端的操作系统是Windows 2000 Professional,客户端是Windows XP。
我做的这个例子是通过客户端的方式来向服务器传递消息。服务器上的MQ中的参数为:
hostname:neu
MQM name: QM_guo
Channel name:ch_server
Q name:Q_guo
PtpSender.java(用于向服务器端传递消息) PtpReceiver.java(用于从服务器端得到消息)
程序代码如下:
/**/ /* * Created on 2005-1-18 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** */ /** * @author Ralph * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ import com.ibm.mq. * ; public class PtpSender ... { public static void main(String[] args) ...{ try...{ String hostName="neu"; String channel="ch_server"; String qManager="QM_guo"; String qName="Q_guo"; //set up the MQEnvironment properties for the client MQEnvironment.hostname=hostName; MQEnvironment.channel=channel; MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,MQC.TRANSPORT_MQSERIES); MQEnvironment.CCSID=1381; //connetion to Q Manager MQQueueManager qMgr=new MQQueueManager(qManager); //set up the open options int openOptions=MQC.MQOO_OUTPUT|MQC.MQOO_FAIL_IF_QUIESCING; //open the Q MQQueue queue=qMgr.accessQueue(qName,openOptions,null,null,null); //set the put message options,will use the default settings MQPutMessageOptions pmo=new MQPutMessageOptions(); //build a message and write data MQMessage outMsg=new MQMessage(); //prepare message with the user data String msgString="Test Message from PtpSender program"; outMsg.writeUTF(msgString); //Now we put the message on the Q queue.put(outMsg,pmo); //commit the transaction qMgr.commit(); System.out.println("The message has been sussesfully put #####"); //close the Q and QManager objects queue.close(); qMgr.disconnect(); } catch(MQException ex)...{ System.out.println("completion code:"+ex.completionCode+" Reason code:"+ex.reasonCode); ex.printStackTrace(); } catch(Exception e)...{ e.printStackTrace(); } }} import com.ibm.mq.MQC; import com.ibm.mq.MQEnvironment; import com.ibm.mq.MQQueue; import com.ibm.mq.MQQueueManager; import com.ibm. * ; import com.ibm.mq. * ; /**/ /* * Created on 2005-1-18 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** */ /** * @author Ralph * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates */ public class PtpReceiver ... { public static void main(String[] args) ...{ try ...{ String hostName = "neu"; String channel = "ch_server"; String qManager = "QM_guo"; String qName = "Q_guo"; // set up the MQEnvironment properties for the client MQEnvironment.hostname = hostName; MQEnvironment.channel = channel; MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES); MQEnvironment.CCSID = 1381; // connetion to Q Manager MQQueueManager qMgr = new MQQueueManager(qManager); // set up the open options int openOptions = MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING; // open the Q MQQueue queue = qMgr.accessQueue(qName, openOptions, null, null, null); // set get message options MQGetMessageOptions gmo = new MQGetMessageOptions(); gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT; gmo.options = gmo.options + MQC.MQGMO_WAIT; gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING; gmo.waitInterval = 3000; // build mssage MQMessage inMsg = new MQMessage(); // get the message from Q queue.get(inMsg, gmo); // read the data from the message String msgString = inMsg.readUTF(); System.out.println("The Message from Q is :" + msgString); // commit the trasaction qMgr.commit(); // close the Q and connection queue.close(); qMgr.disconnect(); } catch (MQException ex) ...{ System.out.println("Completion code is:" + ex.completionCode + " reason code is:" + ex.reasonCode); ex.printStackTrace(); } catch (Exception e) ...{ e.printStackTrace(); } }}
文档中的源程序没有红色部分的代码,这是我在调试的过程中出现提示:队列管理器没有打开CCSID功能的提示。经过在网上找资料和查看文档,最后知道是由于Windows 2000与Windows XP CCSID不同导致的,所以在代码中对CCSID的值进行显示的负值后问题解决了。
CCSID的值在AIX上一般设为1383,如果要支持GBK则设为1386,在WIN上设为1381。