program PutGetDemo;<br><br>// **************************************************************************<br>// **************************************************************************<br>// **************************************************************************<br>// MQSeries IBM library test program for Delphi<br>// Author: Dinko Miljak<br>// e-mail: dinmil@flashmail.com<br>// version 0.0.3<br>// **************************************************************************<br>// **************************************************************************<br>// **************************************************************************<br><br>{$APPTYPE CONSOLE}<br>{$A-} // alignment settings - aviod problems between different languages<br><br>uses<br> SysUtils,<br> CMQPas in '../lib/CMQPas.pas';<br><br>procedure WriteCommonError(ErrorMsg: string; CompCode, Reason: integer);<br>begin<br> Write(ErrorMsg);<br> Write(' Completiton Code[', CompCode, ']: ', DecodeCompletionCode(CompCode));<br> Writeln(' Reason Code[', Reason, ']: ', DecodeReason(Reason));<br>end;<br><br>var<br> Hconn : MQHCONN; // Connection handle<br> CompCode : MQLONG; // Completion code - used by all routines<br> OpenCode : MQLONG; // Completion code - used by MQOPEN function<br> Reason : MQLONG; // Reason code - used by all function<br> CReason : MQLONG; // Connect Reason code qualifying CompCode<br>
ptions: MQLONG; // Open connection flags<br> C_options: MQLONG; // Close connection flags<br> HObj : MQHOBJ;<br><br> od : TMQOD; // Object descriptor<br> gmo : TMQGMO; // Get message options<br> md : TMQMD; // message descripton structure<br> pmo : TMQPMO; // Put message options<br><br> buffer: array[0..8191] of char; // message buffer in which program receive messages<br> buflen: MQLONG; // buffer length - 1 - zero terminated for strings<br> messlen: MQLONG; // message length received - number of bytes I want to send or I received<br><br> QueueName: String;<br> MessageStr: String;<br><br>begin<br> if (ParamCount <> 2) then begin<br> Writeln('Usage: ', ExtractFileName(ParamStr(0)), ' QueueName Message');<br> Writeln('QueueName - existing MQSeries local queue');<br> Writeln('Message - message string');<br> Writeln('Example: ', ExtractFileName(ParamStr(0)), ' RIBA01 "test message"');<br> Halt;<br> end;<br><br> QueueName := ParamStr(1);<br> MessageStr := ParamStr(2);<br><br> Writeln('QueueName: ', QueueName);<br> Writeln('MessageStr: ', MessageStr);<br><br> // ***********************************************************************<br> // Step 1 - connect to connection manager<br> // ***********************************************************************<br> Writeln('Opening connection to default connection manager');<br> MQCONN(Pchar(''), // Connection manager name<br> HConn, // Connection Handle<br> CompCode, // Completition Code<br> CReason); // Reason<br><br> if (CompCode <> MQCC_OK) then begin<br> WriteCommonError('MQCONN failed with CompCode', CompCode, Reason);<br> Readln; Exit;<br> end<br> else begin<br> Writeln('Connection manager connection opened');<br> end;<br><br> // ***********************************************************************<br> // Step 2 - Open Queue<br> // ***********************************************************************<br> // reset object descriptor structure to defaults<br> SetMQOD_DEFAULT(od);<br><br> // copy queue name string to object structure<br> StrPLCopy(od.ObjectName, QueueName, SizeOf(od.ObjectName));<br><br> // Set connection options<br>
ptions := MQOO_INPUT_AS_Q_DEF // open queue for input - read, get<br> + MQOO_OUTPUT // open queue for output - write, put<br> + MQOO_FAIL_IF_QUIESCING; // but not if Message Queue Manager is in stopping state<br><br> // Finally open queue<br> Writeln('Opening queue: "', QueueName, '"');<br> MQOPEN(Hconn, // connection handle<br> od, // object descriptor for queue<br>
ptions, // open options<br> Hobj, // object handle<br> OpenCode, // completion code<br> Reason); // reason code<br><br> // Check the results of openning action<br> if (Reason <> MQRC_NONE) then begin<br> WriteCommonError('MQOPEN ended with reason code ', OpenCode, Reason);<br> Readln; Exit;<br> end;<br><br> if (OpenCode = MQCC_FAILED) then begin<br> WriteCommonError('Unable to open queue for input or output ', OpenCode, Reason);<br> Readln; Exit;<br> end;<br><br> Writeln('Queue opened');<br><br> // ***********************************************************************<br> // Step 3 - Put one test message to queue<br> // ***********************************************************************<br><br> // reset message descriptor structure to defaults<br> SetMQMD_DEFAULT(md);<br><br> // Copy my custom message string to my local buffer<br> FillChar(buffer, SizeOf(Buffer), 0);<br> StrPLCopy(buffer, MessageStr, SizeOf(buffer));<br><br> // Calculate message length<br> messlen := Length(MessageStr);<br><br> // Reset Put Message Object structure to defaults<br> SetMQPMO_DEFAULT(pmo);<br> md.Format := MQFMT_STRING;<br><br> Writeln('Sending test message to queue');<br><br> // Put message to queue<br> MQPUT(Hconn, // connection handle<br> Hobj, // object handle<br> md, // message descriptor<br> pmo, // default options (datagram)<br> messlen, // message length<br> @buffer, // pointer to message buffer<br> CompCode, // completion code<br> Reason); // reason code<br><br> // report reason, if any<br> if (Reason <> MQRC_NONE) then begin<br> WriteCommonError('MQPUT failed ', CompCode, Reason);<br> Readln; Exit;<br> end<br> else begin<br> Write('Message is in the queue. Press any key to continue...');<br> Readln;<br> end;<br><br><br> // ***********************************************************************<br> // Step 4 - Read messages from queue in loop<br> // ***********************************************************************<br> Writeln('Receive messages in loop');<br> Writeln('If programm can not read message in 15 seconds, loop is finished');<br><br> // reset Get Message Option structure to defaults<br> SetMQGMO_DEFAULT(gmo);<br><br> //gmo.Version = MQGMO_VERSION_2; // Avoid need to reset Message<br> //gmo.MatchOptions = MQMO_NONE; // ID and Correlation ID after<br> // every MQGET<br> gm
ptions := MQGMO_WAIT // wait for new messages<br> + MQGMO_CONVERT; // convert if necessary<br> gmo.WaitInterval := 15000; // 15 seconds limit for waiting<br><br><br> // assume that everything is OK with - see loop condition<br> CompCode := MQCC_OK;<br><br> // how much bytes my receive buffer can handle<br> // note - in this application my send and receive buffers are the same<br> buflen := SizeOf(buffer) - 1;<br><br> // enter loop in which programm receives messages from queue<br> while (CompCode <> MQCC_FAILED) do begin<br> // before message is received you always must<br> // reset this fields in Messsage Descriptor structure<br> move(MQMI_NONE, md.MsgId, SizeOf(md.MsgId));<br> move(MQCI_NONE, md.CorrelId, SizeOf(md.CorrelId));<br> md.Encoding := MQENC_NATIVE;<br> md.CodedCharSetId := MQCCSI_Q_MGR;<br><br> MQGET(Hconn, // connection handle<br> Hobj, // object handle<br> md, // message descriptor<br> gmo, // get message options<br> buflen, // buffer length<br> @buffer, // message buffer<br> messlen, // message length<br> CompCode, // completion code<br> Reason); // reason code<br><br> if (CompCode <> MQCC_FAILED) then begin<br> Writeln('Received message: ', buffer);<br> end<br> else begin<br> if (Reason = MQRC_NO_MSG_AVAILABLE) then begin<br> WriteCommonError('No more messages', CompCode, Reason);<br> end<br> else if (Reason <> MQRC_NONE) then begin<br> WriteCommonError('Get message failed', CompCode, Reason);<br> Readln; Exit;<br> end;<br> end;<br> end;<br><br><br> // ***********************************************************************<br> // Step 5 - Close my connection to queue<br> // ***********************************************************************<br> if (OpenCode <> MQCC_FAILED) then begin<br> C_options := 0; // no close options<br> MQCLOSE(Hconn, // connection handle<br> Hobj, // object handle<br> C_options, // close options<br> CompCode, // completion code<br> Reason); // reason code<br><br> if (Reason <> MQRC_NONE) then begin<br> WriteCommonError('MQCLOSE ended with reason code ', CompCode, Reason);<br> end<br> else begin<br> Writeln('Queue closed');<br> end;<br> end;<br><br> // ***********************************************************************<br> // Step 6 - Close my connection to queue manager<br> // ***********************************************************************<br> MQDISC(Hconn, // connection handle<br> CompCode, // completion code<br> Reason); // reason code<br><br> if (Reason <> MQRC_NONE) then begin<br> WriteCommonError('MQDISC ended with reason code ', CompCode, Reason);<br> end<br> else begin<br> Writeln('Connection to Queue Manager closed');<br> end;<br><br> Write('Press any key to continue...');<br> // Test finished<br> Readln;<br>end.