unit MQ_PutGetPas;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, CMQPas, CMQBPas, CMQCFPas, CMQPSPas, CMQXPas,
CMQZPas, ComCtrls;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Label2: TLabel;
BitBtn1: TBitBtn;
StatusBar1: TStatusBar;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
Memo1: TMemo;
Label3: TLabel;
Memo2: TMemo;
Label4: TLabel;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Hconn : MQHCONN; // Connection handle
CompCode : MQLONG; // Completion code - used by all routines
OpenCode : MQLONG; // Completion code - used by MQOPEN function
Reason : MQLONG; // Reason code - used by all function
CReason : MQLONG; // Connect Reason code qualifying CompCode
ptions: MQLONG; // Open connection flags
C_options: MQLONG; // Close connection flags
HObj : MQHOBJ;
od : TMQOD; // Object descriptor
gmo : TMQGMO; // Get message options
md : TMQMD; // message descripton structure
pmo : TMQPMO; // Put message options
buffer: array[0..8191] of char; // message buffer in which program receive messages
buflen: MQLONG; // buffer length - 1 - zero terminated for strings
messlen: MQLONG; // message length received - number of bytes I want to send or I received
QueueName : String;
QueueManagerName : String;
MessageStr : String;
implementation
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if Trim(Edit2.Text)='' then
begin
StatusBar1.Panels[0].Text:='Queue Name Error';
Exit;
end;
// ****************************************
// Step 1 - connect to connection manager
// ****************************************
QueueManagerName:=Trim(Edit1.Text);
MQCONN(Pchar(QueueManagerName), // Connection manager name
HConn, // Connection Handle
CompCode, // Completition Code
CReason); // Reason
if (CompCode <> MQCC_OK) then
begin
StatusBar1.Panels[0].Text:=Format('MQCONN failed with CompCode[%d] Reason[%d]', [CompCode, Reason]);
Exit;
end
else
StatusBar1.Panels[0].Text:='Connection manager connection opened';
// *****************************************
// Step 2 - Open Queue
// *****************************************
// reset object descriptor structure to defaults
QueueName:=Trim(Edit2.Text);
SetMQOD_DEFAULT(od);
// copy queue name string to object structure
StrPLCopy(od.ObjectName, QueueName, SizeOf(od.ObjectName));
// Set connection options
ptions := 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
if RadioButton2.Checked then
ptions := 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
MQOPEN(Hconn, // connection handle
od, // object descriptor for queue
ptions, // open options
Hobj, // object handle
OpenCode, // completion code
Reason); // reason code
// Check the results of openning action
if (Reason <> MQRC_NONE) then
begin
StatusBar1.Panels[0].Text:=Format('MQOPEN ended with OpenCode[%d] Reason[%d]', [OpenCode, Reason]);
Exit;
end;
if (OpenCode = MQCC_FAILED) then
begin
StatusBar1.Panels[0].Text:=Format('Unable to open queue for input or output with OpenCode[%d] Reason[%d]', [OpenCode, Reason]);
Exit;
end;
StatusBar1.Panels[0].Text:='Queue opened';
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
// *****************************************
// Step 3 - Put one test message to queue
// *****************************************
MessageStr:=Memo1.Text;
// reset message descriptor structure to defaults
SetMQMD_DEFAULT(md);
// Copy my custom message string to my local buffer
FillChar(buffer, SizeOf(Buffer), 0);
StrPLCopy(buffer, MessageStr, SizeOf(buffer));
// Calculate message length
messlen := Length(MessageStr);
// Reset Put Message Object structure to defaults
SetMQPMO_DEFAULT(pmo);
md.Format := MQFMT_STRING;
StatusBar1.Panels[0].Text:='Sending message to queue';
// Put message to queue
MQPUT(Hconn, // connection handle
Hobj, // object handle
md, // message descriptor
pmo, // default options (datagram)
messlen, // message length
@buffer, // pointer to message buffer
CompCode, // completion code
Reason); // reason code
// report reason, if any
if (Reason <> MQRC_NONE) then
begin
StatusBar1.Panels[0].Text:=Format('MQPUT failed with CompCode[%d] Reason[%d]', [CompCode, Reason]);
Exit;
end
else
StatusBar1.Panels[0].Text:='Message is in the queue';
end;
procedure TForm1.BitBtn4Click(Sender: TObject);
begin
// *******************************************
// Step 4 - Read messages from queue in loop
// *******************************************
StatusBar1.Panels[0].Text:='Receive messages in loop 15 seconds';
Screen.Cursor:=crHourGlass;
// reset Get Message Option structure to defaults
SetMQMD_DEFAULT(md);
SetMQGMO_DEFAULT(gmo);
//gmo.Version = MQGMO_VERSION_2; // Avoid need to reset Message
//gmo.MatchOptions = MQMO_NONE; // ID and Correlation ID after
// every MQGET
gm
ptions := MQGMO_WAIT // wait for new messages
+ MQGMO_CONVERT; // convert if necessary
gmo.WaitInterval := 15000; // 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
FillChar(buffer, SizeOf(Buffer), 0);
buflen := SizeOf(buffer) - 1;
// enter loop in which programm receives messages from queue
while (CompCode <> MQCC_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.CodedCharSetId := MQCCSI_Q_MGR;
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 (CompCode <> MQCC_FAILED) then
Memo2.Lines.Add(buffer)
else
begin
if (Reason = MQRC_NO_MSG_AVAILABLE) then begin
StatusBar1.Panels[0].Text:=Format('No more messages with CompCode[%d] Reason[%d]', [CompCode, Reason]);
end
else
if (Reason <> MQRC_NONE) then
begin
StatusBar1.Panels[0].Text:=Format('Get message failed with CompCode[%d] Reason[%d]', [CompCode, Reason]);
Screen.Cursor:=crDefault;
Exit;
end;
end;
end;
Screen.Cursor:=crDefault;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
Memo1.Clear;
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
Memo2.Clear;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
// ***************************************
// Step 5 - Close my connection to queue
// ***************************************
if (OpenCode <> MQCC_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 (Reason <> MQRC_NONE) then
StatusBar1.Panels[0].Text:=Format('MQCLOSE ended with CompCode[%d] Reason[%d]', [CompCode, Reason])
else
StatusBar1.Panels[0].Text:='Queue closed';
end;
// ***********************************************
// Step 6 - Close my connection to queue manager
// ***********************************************
MQDISC(Hconn, // connection handle
CompCode, // completion code
Reason); // reason code
if (Reason <> MQRC_NONE) then
StatusBar1.Panels[0].Text:=Format('MQDISC ended with CompCode[%d] Reason[%d]', [CompCode, Reason])
else
StatusBar1.Panels[0].Text:='Connection to Queue Manager closed';
end;
end.