分类: Delphi
2014-03-31 15:37:12
procedure TForm1.Button1Click(Sender: TObject);
var
CompCode, Reason: MQLONG;
QueueMgr: MQCHAR48;
MQmessage: string;
putBuffer: Pchar;
bufLen: MQLONG;
DataLen,getBufLen: MQLONG;
ConnectOpts:MQCNO;
begin
// 1. connect queueManage
QueueMgr:= 'BATMAN'; // queueManage name
ConnectOpts:= MQCNO_DEFAULT;
MQCONN(@QueueMgr, @HConn, @CompCode, @Reason);
// MQCONNX(@QueueMgr,@ConnectOpts,@HConn, @CompCode,@Reason);
if CompCode = MQCC_FAILED then
begin
memo1.Lines.Add('connect MQ server error !! Reason ' + inttostr(Reason));
exit;
end;
// 2. open queue
_od:= MQOD_DEFAULT;
_od.ObjectName:= 'TestQueue'; // queue name
O_options := MQOO_INPUT_AS_Q_DEF + MQOO_OUTPUT + MQOO_FAIL_IF_QUIESCING;
MQOPEN(Hconn, @_od, O_options, @Hobj, @CompCode, @Reason);
if CompCode = MQCC_FAILED then
begin
memo1.Lines.Add('open queue error !! Reason ' + inttostr(Reason));
exit;
end;
// 3. put message
MQmessage:= 'hello,delphi for MQ';
putBuffer:= Pchar(MQmessage);
bufLen:= length(MQmessage);
MsgDesc:= MQMD_Default;
PutMsgOptions:=MQPMO_DeFAULT;
MQPUT(Hconn,Hobj,@MsgDesc,@PutMsgOptions,bufLen,putBuffer,@Compcode,@Reason);
if CompCode = MQCC_FAILED then
memo1.Lines.Add('Put MQMessage error !! Reason ' + inttostr(Reason));
// 4. get message
{ MsgDesc:= MQMD_Default;
GetMsgOptions:= MQGMO_DeFAULT;
GetMsgOptions.Options := MQGMO_WAIT+ MQGMO_CONVERT;
GetMsgOptions.WaitInterval := 200;
getBufLen:= Sizeof(buffer)-1;
MQGET(Hconn,Hobj,@MsgDesc,@GetMsgOptions,getBufLen,buffer,@DataLen,@CompCode,@Reason);
if CompCode = MQCC_FAILED then
memo1.Lines.Add('Get MQMessage error !! Reason ' + inttostr(Reason))
else
memo1.Lines.Add(buffer);
// 5. close queue
MQCLOSE(Hconn, @Hobj, 0, @CompCode, @Reason);
if CompCode = MQCC_FAILED then
memo1.Lines.Add('close queue error !! Reason ' + inttostr(Reason));
// 6. disconnect queueManage
MQDISC(@Hconn, @CompCode, @Reason);
if CompCode = MQCC_FAILED then
memo1.Lines.Add('disconnect queueManage error !! Reason ' + inttostr(Reason)); }
end;