This example goes through
the process of connecting to a queue manager, preparing the queue,
generating the message, closing the queue, and freeing all resources.
data _null_;
length hconn hobj cc reason 8;
length rc hod hpmo hmd hrmh 8;
length msg $ 200;
hconn=0;
hobj=0;
hod=0;
hpmo=0;
hmd=0;
hrmh=0;
put '---------------- Connect to QMgr --------------';
call mqconn("TESTQMGR", hconn, cc, reason);
if cc ^= 0 then do;
if reason = 2002 then do;
put 'Already connected to QMgr ' qmgr;
end;
else do;
if reason = 2059 then
put 'MQCONN: QMgr not available...
needs to be started';
else
put 'MQCONN: failed with reason= ' reason;
goto exit;
end;
end;
else put 'MQCONN: successfully connected to QMgr ' qmgr;
put '---------- Generate object descriptor ---------';
call mqod(hod, "GEN", rc, "OBJECTNAME", "TESTQ");
if rc ^= 0 then do;
put 'MQOD: failed with rc= ' rc;
msg = sysmsg();
put msg;
goto exit;
end;
else put 'MQOD: successfully generated
object descriptor';
put '-------- Open queue object for output ---------';
call mqopen(hconn, hod, "OUTPUT", hobj, cc, reason);
if cc ^= 0 then do;
put 'MQOPEN: failed with reason= ' reason;
goto exit;
end;
else put 'MQOPEN: successfully opened queue for output';
put '--------- Generate put message options --------';
call mqpmo(hpmo, "GEN", rc);
if rc ^= 0 then do;
put 'MQPMO: failed with rc= ' rc;
msg = sysmsg();
put msg;
goto exit;
end;
else put 'MQPMO: successfully generated put
message options';
put '--------- Generate message descriptor ---------';
/* format must be set to reference message header */
call mqmd(hmd, "GEN", rc, "PERSISTENCE,FORMAT",
"PERSISTENT", "MQHREF");
if rc ^= 0 then do;
put 'MQMD: failed with rc= ' rc;
msg = sysmsg();
put msg;
goto exit;
end;
else put 'MQMD: successfully generated
message descriptor';
/** reference message header **/
call mqrmh(hrmh, "GEN", rc,
"SRCNAME,DESTNAME,OBJECTTYPE",
"d:\test.txt", "d:\testdup.txt", "FLATFILE");
if rc ^= 0 then do;
put 'MQRMH: failed with rc= ' rc;
msg = sysmsg();
put msg;
goto exit;
end;
else put 'MQRMH: successfully generated reference
message header';
put '------------- Put message on queue ------------';
call mqput(hconn, hobj, hmd, hpmo, hrmh, cc, reason);
if cc ^= 0 then do;
put 'MQPUT: failed with reason= ' reason;
msg = sysmsg();
put msg;
goto exit;
end;
else put 'MQPUT: successfully put message on queue';
exit:
if hobj ^= 0 then do;
put '----------------- Close queue ---------------';
call mqclose(hconn, hobj, "NONE", cc, reason);
if cc ^= 0 then do;
put 'MQCLOSE: failed with reason= ' reason;
end;
else put 'MQCLOSE: successfully closed queue';
end;
if hconn ^= 0 then do;
put '------------ Disconnect from QMgr -----------';
call mqdisc(hconn, cc, reason);
if cc ^= 0 then do;
put 'MQDISC: failed with reason= ' reason;
end;
else put 'MQDISC: successfully disconnected
from QMgr';
end;
if hod ^= 0 then do;
call mqfree(hod);
put 'Object descriptor handle freed';
end;
if hpmo ^= 0 then do;
call mqfree(hpmo);
put 'Put message options handle freed';
end;
if hmd ^= 0 then do;
call mqfree(hmd);
put 'Message descriptor handle freed';
end;
if hrmh ^= 0 then do;
call mqfree(hrmh);
put 'Reference message header handle freed';
end;
run;