Skip to Main Content

ODP.NET

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Advanced queuing - How do I get notifications about new entries in a queue

1002540Apr 11 2013 — edited Apr 12 2013
Hello all,

This is my first post in this forum and I am fairly new to oracle, so please be patient with me :-) (and sorry for my clumsy english)

What I try to do is to enqueue messages and receive a "MessageAvailable" event. I believe the enqueuing already works because I see new
entries in the queue table. But the notification event does not arrive. I already check the documentation of the advanced queuing classes
and noticed that there is a "listen" method. Would this be a better approach?

My system: Windows-XPSP3 32bit with a fresh install of the Oracle 11g-XE Server and the newest version of the OPD.NET (v4.0.30319) and
Visual-Studio 2010. The server and client are on the same system. The the server and client both are running version 11.2.0.2.0.
The Oracle.DataAccess version is 4.112.2.0.

I put the sql script and the C# code I use below. The C# code is more or less the same as the sample code provided with the ODP.NET framework.

Please, let me know if you need further information, I will add them as fast as possible.

I hope someone can point me in the right direction or tell me what is wrong with the code.

Thanks!
Max

h4. My SQL-Script:
CONNECT SYSTEM/oracle;
/
ALTER USER HR ACCOUNT UNLOCK IDENTIFIED BY Pwd4Sct;
/
GRANT ALL ON DBMS_AQADM TO hr;
/
BEGIN
  DBMS_AQADM.CREATE_QUEUE_TABLE(
    queue_table=>'hr.test_q_tab',
    queue_payload_type=>'RAW',
    multiple_consumers=>FALSE);

  DBMS_AQADM.CREATE_QUEUE(
    queue_name=>'hr.test_q',
    queue_table=>'hr.test_q_tab');

  DBMS_AQADM.START_QUEUE(queue_name=>'hr.test_q');
END;
/
h4. My C# code
using System;
using System.Text;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

namespace ODPSample
{
    class Notification
    {
        static bool isNotified = false;

        static void Main(string[] args)
        {
            // Create connection
            string constr = "user id=hr;password=Pwd4Sct;data source=OraTest";              // !!!!!!!! <===== data source anpassen
            OracleConnection con = new OracleConnection(constr);

            // Create queue
            OracleAQQueue queue = new OracleAQQueue("hr.test_q", con);                      // !!!!!!!! <=== GEƄNDERT von "scott" nach "hr"

            try
            {
                // Open connection
                con.Open();

                // Set message type for the queue
                queue.MessageType = OracleAQMessageType.Raw;

                // Add the event handler to handle the notification. The
                // MsgReceived method will be invoked when a message is enqueued
                queue.MessageAvailable += new OracleAQMessageAvailableEventHandler(Notification.MsgReceived);

                Console.WriteLine("Notification registered...");

                // Begin txn for enqueue
                OracleTransaction txn = con.BeginTransaction();

                Console.WriteLine("Now enqueuing message...");

                // Prepare message and RAW payload
                OracleAQMessage enqMsg = new OracleAQMessage();
                byte[] bytePayload = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                enqMsg.Payload = bytePayload;

                // Prepare to Enqueue
                queue.EnqueueOptions.Visibility = OracleAQVisibilityMode.OnCommit;

                // Enqueue message
                queue.Enqueue(enqMsg);

                Console.WriteLine("Enqueued Message Payload      : " + ByteArrayToString(enqMsg.Payload as byte[]));
                Console.WriteLine("MessageId of Enqueued Message : " + ByteArrayToString(enqMsg.MessageId));
                Console.WriteLine();

                // Enqueue txn commit
                txn.Commit();

                // Loop while waiting for notification
                while (isNotified == false)
                {
                    System.Threading.Thread.Sleep(2000);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
            finally
            {
                // Close/Dispose objects
                queue.Dispose();
                con.Close();
                con.Dispose();
            }
        }

        static void MsgReceived(object src, OracleAQMessageAvailableEventArgs arg)
        {
            try
            {
                Console.WriteLine("Notification Received...");
                Console.WriteLine("QueueName : {0}", arg.QueueName);
                Console.WriteLine("Notification Type : {0}", arg.NotificationType);

                //following type-cast to "byte[]" is required only for .NET 1.x
                byte[] notifiedMsgId = (byte[])arg.MessageId[0];
                Console.WriteLine("MessageId of Notified Message : " + ByteArrayToString(notifiedMsgId));
                isNotified = true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }

        // Function to conver byte[] to string
        static private string ByteArrayToString(byte[] byteArray)
        {
            StringBuilder sb = new StringBuilder();
            for (int n = 0; n < byteArray.Length; n++)
            {
                sb.Append((int.Parse(byteArray[n].ToString())).ToString("X"));
            }

            return sb.ToString();
        }
    }
}
Edited by: 999537 on 11.04.2013 23:08
rephrased the question
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 9 2013
Added on Apr 11 2013
0 comments
275 views