mq连接的乱码问题

[复制链接]
查看11 | 回复3 | 2009-2-20 09:44:20 | 显示全部楼层 |阅读模式
本人写了一个delphi连接mq server6.0的程序,并读取一个远程队列的信息(xml信息),我在本机使用mq资源管理器在该远程队列中放入测试消息后,使用delphi可以读取出正确的消息。但如果是远程客户写入的消息,我读出来只显示"MDE "。该消息使用mq资源管理器中的浏览消息可以正常看到,而且如果将该消息copy出来,再使用本地放入测试消息也可以读出。请问一下是什么问题?是字符集不对吗?如果是,应该如何查看对方的字符集,本地程序要如何设置?本地读信息程序如下:
// ***********************************************************************
// Step 1 - connect to connection manager
// ***********************************************************************
IF Qmanager='' THEN
fSetMsg('Opening connection to default connection manager',3)
ELSE
fSetMsg('Opening connection to '+QMANAGER+'default connection manager',3);
MQCONN(Pchar(QMANAGER),// Connection manager name
HConn,// Connection Handle
CompCode, // Completition Code
CReason); // Reason
if (CompCodeMQCC_OK) then begin
fSetMsg('MQCONN failed with CompCode:'+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
Exit;
end
else begin
fSetMsg('Connection manager connection opened',3);
end;
// ***********************************************************************
// Step 2 - Open Queue
// ***********************************************************************
// reset object descriptor structure to defaults
SetMQOD_DEFAULT(od);
// copy queue name string to object structure
StrPLCopy(od.ObjectName, QueueName, SizeOf(od.ObjectName));
// Set connection options
O_options := MQOO_INPUT_AS_Q_DEF // open queue for input- read, get

+ MQOO_OUTPUT
// open queue for output - write, put

+ MQOO_FAIL_IF_QUIESCING; // but not if Message Queue Manager is in stopping state
// Finally open queue
fSetMsg('Opening queue: "'+QueueName+'"',3);
MQOPEN(Hconn,
// connection handle

od,
// object descriptor for queue

O_options, // open options

Hobj,
// object handle

OpenCode,// completion code

Reason); // reason code
// Check the results of openning action
if (ReasonMQRC_NONE) then begin
fSetMsg('MQOPEN ended with reason code CompCode:'+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
//WriteCommonError('MQOPEN ended with reason code ', OpenCode, Reason);
Exit;
end;
if (OpenCode = MQCC_FAILED) then begin
fSetMsg('Unable to open queue for input or output '+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
Exit;
end;
fSetMsg('Queue opened',3);
// ***********************************************************************
// Step 4 - Read messages from queue in loop
// ***********************************************************************
fSetMsg('Receive messages in loop',3);
fSetMsg('If programm can not read message in 15 seconds, loop is finished',3);
// reset message descriptor structure to defaults
SetMQMD_DEFAULT(md);
// reset Get Message Option structure to defaults
SetMQGMO_DEFAULT(gmo);
//gmo.Version = MQGMO_VERSION_2;// Avoid need to reset Message
//gmo.MatchOptions = MQMO_NONE; // ID and Correlation ID after

// every MQGET
gmo.Options := MQGMO_WAIT // wait for new messages

+ MQGMO_CONVERT; // convert if necessary
gmo.WaitInterval :=5000;
//strtoint(ucommon.ReadxtmisIniString('INIT','waitInterval','5000'));// 15 seconds limit for waiting

// assume that everything is OK with - see loop condition
CompCode := MQCC_OK;
// how much bytes my receive buffer can handle
// note - in this application my send and receive buffers are the same
buffer:='';
buflen :=8190;
messlen:=8190;
// enter loop in which programm receives messages from queue
while (CompCodeMQCC_FAILED) do begin
// before message is received you always must
// reset this fields in Messsage Descriptor structure
move(MQMI_NONE, md.MsgId, SizeOf(md.MsgId));
move(MQCI_NONE, md.CorrelId, SizeOf(md.CorrelId));
// md.Encoding := MQENC_NATIVE;
md.Encoding :=ucommon.ReadxtmisIniInteger('INIT','Encoding',546);//1381;546; --这个地代码是根据对方发送过来的消息进行了修改,但不起作用。
// md.CodedCharSetId := MQCCSI_Q_MGR;
md.CodedCharSetId := ucommon.ReadxtmisIniInteger('INIT','CodedCharSetId',1381);//1381; --这个地代码是根据对方发送过来的消息进行了修改,但不起作用。

MQGET(Hconn,
// connection handle

Hobj,
// object handle

md,
// message descriptor

gmo,
// get message options

buflen,
// buffer length

@buffer,
// message buffer

messlen,
// message length

CompCode, // completion code

Reason);
// reason code
if (CompCodeMQCC_FAILED) then
begin
fSetMsg('Received message: '+buffer,3); ---在这个地方输出buffer=MDE 
//导入相应的文件数据
if buffer='' then
begin
fsetMsg('无数据!',1);
// exit;
end
else
Begin
doc := CreateOleObject('Microsoft.XMLDOM') as IXMLDomDocument;
doc.loadXML(buffer);
//增加数据luxx
TRY

acXmlToDb.OnExecute(sender);

i:=i+1;
EXCEPT

doc.save(ExtractFilePath(application.ExeName)+'\errData\err'+formatdatetime('yyyymmddhnnss',now)+'.xml');

fSetMsg('增加数据失败!!'+buffer,1);
END;
//
end;
end
else begin
if (Reason = MQRC_NO_MSG_AVAILABLE) then begin
fSetMsg('No more messages'+inttostr(CompCode)+'Reason:'+inttostr(Reason),3);
end
else if (ReasonMQRC_NONE) then begin
fSetMsg('Get message failed! CompCode:'+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
Exit;
end;
end;
end;
// ***********************************************************************
// Step 5 - Close my connection to queue
// ***********************************************************************
if (OpenCodeMQCC_FAILED) then begin
C_options := 0;
// no close options
MQCLOSE(Hconn,
// connection handle

Hobj,
// object handle

C_options,
// close options

CompCode,
// completion code

Reason);
// reason code
if (ReasonMQRC_NONE) then begin
fSetMsg('MQCLOSE ended with reason code '+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
end
else begin
fSetMsg('Queue closed',3);
end;
end;
// ***********************************************************************
// Step 6 - Close my connection to queue manager
// ***********************************************************************
MQDISC(Hconn,
// connection handle
CompCode,
// completion code
Reason);
// reason code
if (ReasonMQRC_NONE) then begin
fSetMsg('MQDISC ended with reason code '+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
end
else begin
fSetMsg('Connection to Queue Manager closed',3);
end;
fSetMsg('共导入'+InttoStr(i)+'条数据记录!',3);
[ 本帖最后由 luxx 于 2009-4-29 15:48 编辑 ]
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
自己解决.原来是接收的数据格式设置不正确.应该在get消息之前,设置一下md.version 和gmo.version ,同时以组消息的形式进行接收.代码如下:
SetMQGMO_DEFAULT(gmo);
md.Version :=MQMD_VERSION_2;
gmo.Version := MQGMO_VERSION_2;// Avoid need to reset Message
// gmo.MatchOptions := MQMO_NONE; // ID and Correlation ID after

// every MQGET
// gmo.Options := MQGMO_WAIT // wait for new messages

// + MQGMO_CONVERT; // convert if necessary
gmo.Options := MQGMO_FAIL_IF_QUIESCING

+ MQGMO_SYNCPOINT

+ MQGMO_WAIT

+ MQGMO_ALL_MSGS_AVAILABLE

+ MQGMO_LOGICAL_ORDER ;
gmo.WaitInterval :=5000;
gmo.MatchOptions := MQMO_MATCH_GROUP_ID;
[ 本帖最后由 luxx 于 2009-5-13 20:24 编辑 ]
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
呵呵,赞一个
回复

使用道具 举报

千问 | 2009-2-20 09:44:20 | 显示全部楼层
我使用的c#,一样的错误,但根据你的提示,改了下,居然也成了,非常感谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行