Skip Headers
Oracle® Objects for OLE C++ Class Library Developer's Guide
10g Release 2 (10.2)

Part Number B14308-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Dequeuing Messages

This topic includes examples for dequeuing RAW (string of characters) and user-defined type messages.

Note: For more information about sample code, see Sample Code and Applications.

Dequeuing RAW (string of characters) Messages:

#include <oracl.h>

OSession osess ;
int DequeueRaw();
int main(int argc, char **argv)
{
        int retVal = 0;
        OStartup(OSTARTUP_MULTITHREADED);
        try
        {
                osess.Open();
                if ( ! osess.IsOpen() )
                {
                        cout <<  "Session not opened: Error:
                              " << osess.GetErrorText() << endl;
                        osess.Close();
                        OShutdown();
                        return -1;
                }
                retVal =  DequeueRaw();
        }
        catch(OException oerr)
        {
                cout << "Exception while dequeuing message : " 
                        << oerr.GetErrorText() << endl;
                retVal =  -1;
        }
        OShutdown();
        return retVal;
}


// This function dequeues a message of default 
// type (string of characters) from the raw_msg_queue.

// Gets the message priority after dequeuing
// Checks if any message with correlation like 'AQ' is available 
// on the queue.
int DequeueRaw()
{
        ODatabase odb;
        OAQ  oaq;
        OAQMsg oaqmsg;
        OValue msg;
        const char *msgid = 0;

        odb.Open(osess, "ExampleDB", "SCOTT", "TIGER");

        // Open the 'raw_msg_queue'
        oaq.Open(odb,"raw_msg_queue");
       
        // Get an instance of the default message(of RAW type)
        oaqmsg.Open(oaq);

        // Dequeue a message 
        msgid = oaq.Dequeue();
      
        // Retrieve the message attributes
        oaqmsg.GetValue(&msg);
        const char *msgval = msg;
        cout <<"Message '"<<msgval<<"' dequeued at 
                      priority : "<<oaqmsg.GetPriority()<<endl;
        // Dequeue message with correlation like "AQ"
        oaq.SetCorrelate("%AQ%");
        oaq.SetDequeueMode(OAQ_DQ_REMOVE);
        msgid = oaq.Dequeue();
       
        // Retrieve the message attributes
       char msgval[101];
        long len = oaqmsg.GetValue(msgval,100);
        msgval[len] = '\0';
        cout<<"Message '"<<msgval<<"' dequeued at 
                       priority : "<<oaqmsg.GetPriority()<<endl;
       //Close all of the objects
        oaqmsg.Close();
        oaq.Close();
        odb.Close();
        return 0;
}
Dequeuing User-Defined Type Messages:
// This function dequeues a message of user-defined type MESSAGE_TYPE
// from the msg_queue.

// Gets the message priority after dequeuing
// Checks if any message with correlation like 'SCOTT' is available on 
// the queue.
int DequeueObject()
{
        ODatabase odb;
        OAQ  oaq;
        OAQMsg oaqmsg;
        const char *msgid = 0;
        OValue msg;
        char subject[255];
        char text[255];

        odb.Open(osess, "ExampleDB", "SCOTT", "TIGER");

        // Open the 'msg_queue'
        oaq.Open(odb,"msg_queue");
     
        // Get an instance of the udt MESSAGE_TYPE (check out schema
        //for details)
        oaqmsg.Open(oaq, OTYPE_OBJECT, "MESSAGE_TYPE");

        // Dequeue message with correlation like "SCOTT"
        oaq.SetCorrelate("%SCOTT%");
        oaq.SetDequeueMode(OAQ_DQ_REMOVE);
        msgid = oaq.Dequeue();
      
        // Retrieve the message attributes
        // Get the subject,text attributes of the message
        OObject msgval;
        oaqmsg.GetValue(&msgval);
        msgval.GetAttrValue("subject", subject,255);    
        msgval.GetAttrValue("text", text,255);

        cout<<"Message '"<<(subject ? subject :"")<<"' & Body : '"<<text<<"
                 'dequeued at priority : "<<oaqmsg.GetPriority()<<endl;

        msgid = 0;
        oaq.SetNavigation(OAQ_DQ_FIRST_MSG);
        oaq.SetCorrelate("");

        // Dequeue a message 
        msgid = oaq.Dequeue();
       
              // Retrieve the message attributes
              OObject msgval;
              oaqmsg.GetValue(&msg);
              msgval = msg;     
            
              // Get the subject,text attributes of the message
              msgval.GetAttrValue("subject", subject,255);    
              msgval.GetAttrValue("text", text,255);
              cout<<"Message '"<<(subject ? subject :"")<<"' & Body :
                         '"<<text<<"' dequeued at priority : "<<oaqmsg.GetPriority()<<endl;
       
        // Close all of the objects
        oaqmsg.Close();
        oaq.Close();
        odb.Close();
        return 0;

}