MQ中简单的消息发送和接收

星期四, 十二月 6, 2007 1:13
Posted in category 工作, 技术

对mqm.dll动态链接库的封装接口
通过MQI.pas提供的接口函数我们可以很容易的发送和接收简单的文本信息
因为只是例子,所以命名不标准,注释不规范,好在代码简单,容易读懂

窗体文件不提供了,相信看到代码也应该能猜出来了

整个单元代码文件如下:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, MQI;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
private
{ Private declarations }
Hconn : MQHCONN;
Hobj : MQHOBJ;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

const
QM_NAME : MQCHAR48 = ‘MyTest’#0;
QUEUE_NAME : MQCHAR48 = ‘Test001′#0;

procedure TForm1.Button1Click(Sender: TObject);
var
ConnectOpts : MQCNO;
CompCode, Reason : MQLONG;
QMgrName : MQCHAR48;
begin
Self.Memo1.Lines.Add(’Begin connect…’);

QMgrName := QM_NAME;
ConnectOpts := MQCNO_DEFAULT;

MQCONNX ( @QMgrName, @ConnectOpts, @(Self.HConn), @Compcode, @Reason);

if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add(’Connect Failed’);
end
else
begin
Self.Memo1.Lines.Add(’Connected successed’);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
ObjDesc : MQOD;
Options, CompCode, Reason : MQLONG;
begin
{ Calling the MQCONN procedure to get a valid connection handle was left out ! }
Options := MQOO_FAIL_IF_QUIESCING + MQOO_OUTPUT + MQOO_INPUT_SHARED;
ObjDesc := MQOD_DEFAULT;
ObjDesc.ObjectName := QUEUE_NAME;

MQOPEN (Self.HConn, @ObjDesc, Options, @Self.HObj, @Compcode, @Reason );

if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add(’Open Failed’);
end
else
begin
Self.Memo1.Lines.Add(’Open Successed’);
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
Options, CompCode, Reason : MQLONG;
begin
{ Calling the MQOPEN procedure to get a valid object handle was left out ! }
Options := MQCO_NONE;
MQCLOSE ( Self.HConn, @Self.Hobj, Options, @Compcode, @Reason);

if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add(’Close Failed’);
end
else
begin
Self.Memo1.Lines.Add(’Close Successed’);
end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
ObjDesc : MQOD;
MsgDesc : MQMD;
PutMsgOptions : MQPMO;
BufferLength : MQLONG;
Buffer : array [0..1024] of Char;
CompCode, Reason : MQLONG;
begin
{ Calling the MQCONN procedure to get a valid connection handle was left out ! }
Buffer := ‘This message was sent from Delphi by put1′#0;
BufferLength := Length(Trim(Buffer));
ObjDesc := MQOD_DEFAULT;
ObjDesc.ObjectName := QUEUE_NAME;

MsgDesc := MQMD_DEFAULT;
PutMsgOptions := MQPMO_DEFAULT;

//在MQ的13个函数中,MQPUT1实现了这样一种功能,
//即它合并了MQOPEN, MQPUT, MQCLOSE三个函数的功能,
//在打开队列并且只希望发送一条消息时,它的CPU消耗比上述三个函数相加要少。
MQPUT1 ( Self.HConn, @ObjDesc, @MsgDesc, @PutMsgOptions, BufferLength, @Buffer, @Compcode, @Reason );

if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add(’Put1 Failed’);
end
else
begin
Self.Memo1.Lines.Add(’Put1 Successed’);
end;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
MsgDesc : MQMD;
PutMsgOptions : MQPMO;
BufferLength : MQLONG;
Buffer : array [0..1024] of Char;
CompCode, Reason : MQLONG;
begin
{ Calling the MQCONN or MQCONNX and MQOPEN procedure to get a valid }
{ connection handle and object handle was left out ! }
Buffer := ‘This message was sent from Delphi by put’#0;
BufferLength := Length(Trim(Buffer));
MsgDesc := MQMD_DEFAULT;
PutMsgOptions := MQPMO_DEFAULT;

MQPUT(Self.HConn, Self.Hobj, @MsgDesc, @PutMsgOptions, BufferLength, @Buffer, @Compcode, @Reason);

if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add(’Put Failed’);
end
else
begin
Self.Memo1.Lines.Add(’Put Successed’);
end;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
MsgDesc : MQMD;
GetMsgOptions : MQGMO;
DataLength : MQLONG;
BufferLength : Integer;
Buffer : array [0..1024] of Char;
CompCode, Reason : MQLONG;
begin
{ Calling the MQCONN or MQCONNX and MQOPEN procedure to get a valid }
{ connection handle and object handle was left out ! }
BufferLength := 1024;
DataLength := 0;
MsgDesc := MQMD_DEFAULT;
GetMsgOptions := MQGMO_DEFAULT;

//———————————
// GetMsgOptions.Options := MQGMO_WAIT + MQGMO_FAIL_IF_QUIESCING + MQGMO_ALL_MSGS_AVAILABLE;
// GetMsgOptions.WaitInterval := 10000;
//———————————

MQGET(Self.HConn, Self.Hobj, @MsgDesc, @GetMsgOptions, BufferLength, Buffer,
@DataLength, @Compcode, @Reason);

if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add(’Get Failed’);
end
else
begin
Self.Memo1.Lines.Add(Buffer);
Self.Memo1.Lines.Add(’Get Successed’);
end;
end;

procedure TForm1.Button7Click(Sender: TObject);
var
ObjDesc : MQOD;
Selectors : array [0..2] of MQLONG;
Options, CompCode, Reason : MQLONG;
QMgrName : MQCHAR48;
Buffer : array [0..1024] of Char;
ConnectOpts : MQCNO;
begin
QMgrName := QM_NAME;
ConnectOpts := MQCNO_DEFAULT;
// MQCONNX ( @QMgrName, @Self.HConn, @Compcode, @Reason);
MQCONNX ( @QMgrName, @ConnectOpts, @Self.HConn, @Compcode, @Reason);

if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add(’Connect Failed’);
end
else
begin
Self.Memo1.Lines.Add(’Connect Successed’);
end;

ObjDesc := MQOD_DEFAULT;

with ObjDesc do
begin
ObjectQmgrName := QMgrName;
ObjectType := MQOT_Q_MGR;
end;

Options := MQOO_INQUIRE + MQOO_FAIL_IF_QUIESCING;
MQOPEN (Self.HConn, @ObjDesc, Options, @Self.HObj, @Compcode, @Reason );

if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add(’Open Failed’);
end
else
begin
Self.Memo1.Lines.Add(’Open Successed’);
end;

Selectors[0] := MQCA_Q_MGR_NAME;
MQINQ ( Self.HConn, Self.Hobj, 1, @Selectors, 0, nil, MQ_Q_MGR_NAME_LENGTH, Buffer, @Compcode, @Reason );

if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add(’Inquire Failed’);
end
else
begin
Self.Memo1.Lines.Add(’Inquire Successed’);
end;
end;

procedure TForm1.Button8Click(Sender: TObject);
var
CompCode, Reason : MQLONG;
begin
Self.Memo1.Lines.Add(’Begin Disconnect…’);

MQDISC ( @Self.HConn, @Compcode, @Reason );

if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add(’Disconnect Failed’);
end
else
begin
Self.Memo1.Lines.Add(’Disconnected successed’);
end;
end;

end.

You can leave a response, or trackback from your own site.

Leave a Reply