首页  编辑  

IBM 消息队列使用

Tags: /超级猛料/Message.消息和事件/   Date Created:

http://www.sql.ru/forum/actualthread.aspx?tid=570585

uses MQ;

// -------------------------

procedure TForm1.FormClick(Sender: TObject);

var

 Connect_options : MQCNO;

 ClientConn      : MQCD;

 od              : MQOD;

 QMName          : MQCHAR48;

 Hcon            : MQHCONN;

 Hobj            : MQHOBJ;

 CompCode        : MQLONG;

 OpenCode        : MQLONG;

 Reason          : MQLONG;

 CReason         : MQLONG;

 MsgDescript     : MQMD;

 PutOptions      : MQPMO;

 GetOptions      : MQGMO;

 BufLength       : MQLONG;

 MsgBuffer       : MQCHAR128;

 MsqLength       : MQLONG;

begin

 Connect_options := MQCNO_DEFAULT;

 ClientConn := MQCD_CLIENT_CONN_DEFAULT;

 od := MQOD_DEFAULT;

 QMName := '';  // 队列管理器名称, 默认为空, 你可以指定其他名字 QMName := 'mymq.manager'

 ClientConn.ConnectionName := '192.168.0.XX';

 ClientConn.ChannelName    := 'JAVA.CHANNEL'; // 通道名称

 Connect_options.ClientConnPtr := Addr(ClientConn);

 Connect_options.Version := MQCNO_VERSION_2;

 MQCONNX(@QMName, @Connect_options, @Hcon, @CompCode, @CReason);

 if CompCode = MQCC_FAILED then begin

   MessageBox(0, PAnsiChar(Format('MQCONNX ended with reason code %d', [CReason])), 'Error!', MB_ICONWARNING);

   Halt(CReason);

 end;

 od.ObjectType := MQOT_Q;

 od.ObjectName := 'QQQ';  // 队列名称

 MQOPEN(Hcon, @od, MQOO_OUTPUT + MQOO_FAIL_IF_QUIESCING, @Hobj, @OpenCode, @Reason);

 if Reason <> MQRC_NONE then

   MessageBox(0, PAnsiChar(Format('MQOPEN ended with reason code %d', [Reason])), 'Error!', MB_ICONWARNING);

 if OpenCode = MQCC_FAILED then

   MessageBox(0, 'Unable to open queue manager for inquire', 'Error!', MB_ICONWARNING)

 else begin

   MsgDescript := MQMD_DEFAULT;

   PutOptions  := MQPMO_DEFAULT;

   MQPUT(Hcon, Hobj, @MsgDescript, @PutOptions, 12, 'Hello World!', @CompCode, @Reason);

   if Reason = MQRC_NONE then

     MessageBox(0, 'MQPUT succesfull!', 'Done!', MB_ICONINFORMATION)

   else

     MessageBox(0, PAnsiChar(Format('MQPUT ended with reason code %d', [Reason])), 'Error!', MB_ICONWARNING);

   MQCLOSE(Hcon, @Hobj, MQCO_NONE, @CompCode, @Reason);

   if Reason <> MQRC_NONE then

     MessageBox(0, PAnsiChar(Format('MQCLOSE ended with reason code %d', [Reason])), 'Error!', MB_ICONWARNING);

 end;

 MQOPEN(Hcon, @od, MQOO_INPUT_SHARED + MQOO_FAIL_IF_QUIESCING, @Hobj, @OpenCode, @Reason);

 if Reason <> MQRC_NONE then

   MessageBox(0, PAnsiChar(Format('MQOPEN ended with reason code %d', [Reason])), 'Error!', MB_ICONWARNING);

 if OpenCode = MQCC_FAILED then

   MessageBox(0, 'Unable to open queue manager for inquire', 'Error!', MB_ICONWARNING)

 else begin

   MsgDescript := MQMD_DEFAULT;

   GetOptions  := MQGMO_DEFAULT;

   GetOptions.Options := MQGMO_NO_WAIT + MQGMO_NO_SYNCPOINT;

   MsgBuffer   := '';

   BufLength   := 100;

   MQGET(Hcon, Hobj, @MsgDescript, @GetOptions, BufLength, @MsgBuffer, @MsqLength, @CompCode, @Reason);

   if Reason = MQRC_NONE then

     MessageBox(0, PAnsiChar('MQGET succesfull! ' + MsgBuffer), 'Done!', MB_ICONINFORMATION)

   else

     MessageBox(0, PAnsiChar(Format('MQGET ended with reason code %d', [Reason])), 'Error!', MB_ICONWARNING);

   MQCLOSE(Hcon, @Hobj, MQCO_NONE, @CompCode, @Reason);

   if Reason <> MQRC_NONE then

     MessageBox(0, PAnsiChar(Format('MQCLOSE ended with reason code %d', [Reason])), 'Error!', MB_ICONWARNING);

 end;

 if CReason <> MQRC_ALREADY_CONNECTED then begin

   MQDISC(@Hcon, @CompCode, @Reason);

   if Reason <> MQRC_NONE then

     MessageBox(0, PAnsiChar(Format('MQDISC ended with reason code %d', [Reason])), 'Error!', MB_ICONWARNING);

 end;

 MessageBox(0, 'Sample MQCONNX end', 'Done!', MB_ICONINFORMATION);

end;

end.

MQ.PAS (90.9KB)
MQI.PAS (75.3KB)
MQIC.PAS (75.4KB)
MQI_Delphi.pdf (59.7KB)
cmqpas.zip (111.8KB)