今天在做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();
}
}
}
* 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。