Previous Page | Next Page

Using the SAS Common Messaging Interface

TIB/Rendezvous Certified Messaging Coding Examples


Example 1: Sending and Receiving Messages in the Same DATA Step

In this example, the sender and listener use the same DATA step.

   data _null_;

      length msg $ 200;
      length qid qid2 tid rc 8;
      length map $80;
      length recv1 recv2 recv3 8; 
      length recv4 $50;
      length event $10;

      tid=0;
      rc=0;
      put '----';
      put 'Call INIT';
      CALL INIT(tid, 'RENDEZVOUS-CM', rc);
      if rc ^= 0 then do;
         put 'INIT: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'INIT: succeeded';

      call setmodel("RENDEZVOUS-CM", "RENDCMSENDER", 
         "REGISTRY", rc, "CMNAME, LEDGER",
         "cmsender", "c:\cmsendledger.txt"); 
      if rc ^= 0 then do;
         put 'SETMODEL: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'SETMODEL: succeeded';

      call setmodel("RENDEZVOUS-CM", "RENDCMRECEIVE", 
         "REGISTRY", rc, "CMNAME, LEDGER, REQUESTOLD, 
         SYNCLEDGER", "cmreceive", "c:\cmrcvledger.txt", 
         "YES", "NO"); 
      if rc ^= 0 then do;
         put 'SETMODEL: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'SETMODEL: succeeded';

      rc=0;
      put '----';
      put 'Call SETMAP';
      CALL SETMAP('rendmap', 'REGISTRY', rc, 
         'SHORT;LONG;DOUBLE;CHAR,,50'); 
      if rc ^= 0 then do;
         put 'SETMAP: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'SETMAP: succeeded';

      rc=0;
      qid2=0;
      put '----';
      put 'Call OPENQUEUE';
      CALL OPENQUEUE(qid2, tid, 'testcm.subject', 
         'DELIVERY', rc, "DYNAMIC(Model=rendcmsender)");
      if rc ^= 0 then do;
         put 'OPENQUEUE: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'OPENQUEUE: succeeded';
      put "qid2= " qid2;

      rc=0;
      qid=0;
      put '----';
      put 'Call OPENQUEUE';
      CALL OPENQUEUE(qid, tid, 'testcm.subject', 'FETCH', rc, 
         "DYNAMIC(Model=rendcmreceive)", "POLL(Timeout=15)");
      if rc ^= 0 then do;
         put 'OPENQUEUE: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'OPENQUEUE: succeeded';
      put "qid= " qid;

      /* send a message */
      parm1=100;
      parm2=9999;
      parm3=9999.1234;
      parm4="Demonstrating the rendezvous message api.";

      put '----';
      put 'Call SENDMESSAGE';
      call sendmessage(qid2,rc,"map","rendmap" ,
         parm1,parm2,parm3,parm4); 
      if rc ^= 0 then do;
         put 'send message failed: ';
         msg=sysmsg();
         put msg;
      end;
      else put 'send message succeeded';
   
      rc = 0;
      put '----';
      put 'Call RECEIVEMESSAGE';

      map = "rendmap";
      call receivemessage(qid, rc, event, 
         attchflg,"map",map,recv1,recv2,recv3,recv4); 
      put 'qid =' qid;
      put 'event = ' event;
      put 'attchflg =' attchflg;
      if rc ^= 0 then do;
         put 'receive message failed: ';
         msg=sysmsg();
         put msg;
      end;
      else do;
         put 'receive message succeeded';
         put map;
      end;
      if event eq 'DELIVERY' then
      do;
         put 'Message has been delivered';
         if attchflg eq 1 then do;
            put 'Attachments are associated 
               with this message';
            /* process attachments...*/
         end;
         put 'recv1 = ' recv1;
         put 'recv2 = ' recv2;
         put 'recv3 = ' recv3; 
         put 'recv4 = ' recv4;
      end; 


      rc=0;
      put '----';
      put 'Call CLOSEQUEUE for sender';
      put "qid2= " qid2;
      CALL CLOSEQUEUE(qid2, rc, "DELETE_PURGE");
      if rc ^= 0 then do;
         put 'CLOSEQUEUE: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'CLOSEQUEUE: succeeded';

      rc=0;
      put '----';
      put 'Call CLOSEQUEUE for receiver';
      put "qid= " qid;
      CALL CLOSEQUEUE(qid, rc, "DELETE_PURGE");
      if rc ^= 0 then do;
         put 'CLOSEQUEUE: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'CLOSEQUEUE: succeeded';

      rc=0;
      put '----';
      put 'Call TERM';
      CALL TERM(tid, rc);
      if rc ^= 0 then do;
         put 'TERM: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'TERM: succeeded';

   run;


Example 2: Sending and Receiving Messages in Separate DATA Steps


Overview

In this example, the sender and listener use separate DATA steps. Each DATA step is run in a separate SAS session. The receiving DATA step needs to start running before the sending DATA step ends.


Sending DATA Step

   /* SAS DATA step to send a certified message */

   
   data _null_;
 
      length msg $ 200;
      length qid2 tid rc 8;
      length map $80;
      length recv4 $50;
      length event $10;
      length queue $ 80;

 
      tid=0;
      rc=0;
      call setmodel("RENDEZVOUS-CM", "RENDCMSENDER", 
         "REGISTRY", rc, "CMNAME, LEDGER", "cmsender", 
         "c:\sendledger.txt"); 
      if rc ^= 0 then do;
         put 'SETMODEL: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'SETMODEL: succeeded';

      rc=0;
      put '----';
      put 'Call SETMAP';
      CALL SETMAP('rendmap', 'REGISTRY', rc, 
         'SHORT;LONG;DOUBLE;CHAR,,50'); 
      if rc ^= 0 then do;
         put 'SETMAP: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'SETMAP: succeeded';

	  
      call setalias("queue", "tibcmalias", "REGISTRY", 
         rc, "RENDEZVOUS-CM", "send.cmmsg");
      if rc ^= 0 then do;
         put 'set_alias failed: ';
         msg=sysmsg();
         put msg;
      end;
      else put 'set_alias succeeded';
      put ' this should be next';

      rc=0;
      qname = "tibcmalias";
      qid2=0;
      put '----';
      put 'Call OPENQUEUE for queue2';
      CALL OPENQUEUE(qid2, tid, qname, 'DELIVERY', 
         rc, "DYNAMIC(Model=rendcmsender)");
      if rc ^= 0 then do;
         put 'OPENQUEUE: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'OPENQUEUE: succeeded';

      /* send a message */
      parm1=100;
      parm2=9999;
      parm3=9999.1234;
      parm4="Demonstrating the rendezvous message api.";

      put '----';
      put 'Call SENDMESSAGE';
      call sendmessage(qid2,rc,"map, addlistener","rendmap",
         "cmreceive",parm1,parm2,parm3,parm4); 
      if rc ^= 0 then do;
         put 'send message failed: ';
         msg=sysmsg();
         put msg;
      end;
      else put 'send message succeeded';
 
      /* 
      * This or another instance of the certified transport 
      * named cmsender must be active to deliver certified 
      * messages to the listener. 
      */
      slept = sleep(15);

      rc=0;
      put '----';
      put 'Call CLOSEQUEUE for queue2';
      CALL CLOSEQUEUE(qid2, rc);
      if rc ^= 0 then do;
         put 'CLOSEQUEUE: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'CLOSEQUEUE: succeeded';
 
 
   run;


Receiving DATA Step

   /* SAS DATA step to receive certified messages */
 
   data _null_;

      length msg $ 200;
      length qid tid rc 8;
      length map $80;
      length event $10;
      length queue $ 80;
      length token $300;
      length attach $10;
      length recv1 recv2 recv3 8; 
      length recv4 $50;
      length certified $8;
      length sendername $50;
   
      rc=0;
      call setmodel("RENDEZVOUS-CM", "RENDCMRECEIVE", 
         "REGISTRY", rc, "CMNAME, LEDGER, REQUESTOLD",
         "cmreceive", "c:\recvledger.txt", "YES"); 

      if rc ^= 0 then do;
         put 'SETMODEL: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'SETMODEL: succeeded';

      call setalias("queue", "tibcmalias", "REGISTRY", 
         rc, "RENDEZVOUS-CM", "send.cmmsg");
      if rc ^= 0 then do;
         put 'set_alias failed: ';
         msg=sysmsg();
         put msg;
      end;
      else put 'set_alias succeeded';

      rc=0;
      qid=0;
      tid = 0;
      qname = "tibcmalias";
      put '----';
      put 'Call OPENQUEUE';
      CALL OPENQUEUE(qid, tid, qname, 'FETCH', rc, 
         "DYNAMIC(Model=rendcmreceive)", "POLL(TIMEOUT=30)");
      if rc ^= 0 then do;
         put 'OPENQUEUE: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'OPENQUEUE: succeeded';
      put "qid= " qid;

      put "CALL receivemessage";
      map = "rendmap";
      call receivemessage(qid, rc, event, 
         attchflg,"map,certified,sendername",map, certified, 
         sendername, recv1,recv2,recv3,recv4); 
      put 'qid =' qid;
      put 'event = ' event;
      put 'attchflg =' attchflg;
      put 'certified = ' certified;
      put 'sendername = ' sendername;
      if rc ^= 0 then do;
         put 'receive message failed: ';
         msg=sysmsg();
         put msg;
      end;
      else do;
         put 'receive message succeeded';
         put map;
      end;
      if event eq 'DELIVERY' then
      do;
         put 'Message has been delivered';
         if attchflg eq 1 then do;
            put 'Attachments are associated 
               with this message';
            /* process attachments...*/
         end;
         put 'recv1 = ' recv1;
         put 'recv2 = ' recv2;
         put 'recv3 = ' recv3; 
         put 'recv4 = ' recv4;
      end; 

   rc=0;
      put '----';
      put 'Call CLOSEQUEUE for queue1';
      CALL CLOSEQUEUE(qid, rc);
      if rc ^= 0 then do;
         put 'CLOSEQUEUE: failed';
         msg = sysmsg();
         put msg;
      end;
      else put 'CLOSEQUEUE: succeeded';

      rc=0;
      put '----';


   run;

Previous Page | Next Page | Top of Page