Oracle Advance Queue - Dequeue not working

SaintLike picture SaintLike · May 28, 2015 · Viewed 10.8k times · Source

I can't seem to find the solution to my problem, I've been stuck at this for hours.

I'm usings Oracle AQs:

       Dbms_Aqadm.Create_Queue_Table(Queue_Table        => 'ITEM_EVENT_QT',
                                    Queue_Payload_Type => 'ITEM_EVENT',
                                    Multiple_Consumers => TRUE);

       Dbms_Aqadm.Create_Queue(Queue_Name          => 'ITEM_EVENT_QUEUE',
                              Queue_Table         => 'ITEM_EVENT_QT',
                              Max_Retries         => 5,
                              Retry_Delay         => 0,
                              Retention_Time      => 432000, -- 5 DAYS
                              Dependency_Tracking => FALSE,
                              COMMENT             => 'Item Event Queue');
       -- START THE QUEUE
       Dbms_Aqadm.Start_Queue('ITEM_EVENT_QUEUE');
       -- GRANT QUEUE PRIVILEGES
       Dbms_Aqadm.Grant_Queue_Privilege(Privilege    => 'ALL',
                                       Queue_Name   => 'ITEM_EVENT_QUEUE',
                                       Grantee      => 'PUBLIC',
                                       Grant_Option => FALSE);
    END;

Here's one of my subscribers:

Dbms_Aqadm.Add_Subscriber(Queue_Name => 'ITEM_EVENT_QUEUE',
                            Subscriber => Sys.Aq$_Agent('ITEM_SUBSCRIBER_1',
                                                        NULL,
                                                        NULL),
                            rule   =>  'tab.user_data.header.thread_no = 1');

   Dbms_Aq.Register(Sys.Aq$_Reg_Info_List(Sys.Aq$_Reg_Info('ITEM_EVENT_QUEUE:ITEM_SUBSCRIBER_1',
                                                          Dbms_Aq.Namespace_Aq,
                                                          'plsql://ITEM_API.GET_QUEUE_FROM_QUEUE',
                                                          HEXTORAW('FF'))),1);

The subscriber registration:

Whenever a certain event occurs on my DB, I'm using a trigger to add "the event" to my AQ by calling the following procedure from my ITEM_API package:

  PROCEDURE ADD_EVENT_TO_QUEUE(I_EVENT       IN ITEM_EVENT,
                               O_STATUS_CODE OUT VARCHAR2,
                               O_ERROR_MSG   OUT VARCHAR2) IS

    ENQUEUE_OPTIONS    DBMS_AQ.ENQUEUE_OPTIONS_T;
    MESSAGE_PROPERTIES DBMS_AQ.MESSAGE_PROPERTIES_T;
    MESSAGE_HANDLE     RAW(16);
    EVENT              ITEM_EVENT;
    HEADER_PROP        HEADER_PROPERTIES;
  BEGIN
    EVENT                              := I_EVENT;
    EVENT.SEQ_NO                       := ITEM_EVENT_SEQ.NEXTVAL;
    ENQUEUE_OPTIONS.VISIBILITY         := DBMS_AQ.ON_COMMIT;
    ENQUEUE_OPTIONS.SEQUENCE_DEVIATION := NULL;
    MESSAGE_PROPERTIES.PRIORITY        := 1;
    MESSAGE_PROPERTIES.DELAY           := DBMS_AQ.NO_DELAY;
    MESSAGE_PROPERTIES.EXPIRATION      := DBMS_AQ.NEVER;
    HEADER_PROP                        := HEADER_PROPERTIES(1);
    EVENT.HEADER                       := HEADER_PROP;
    DBMS_AQ.ENQUEUE(QUEUE_NAME         => 'ITEM_EVENT_QUEUE',
                    ENQUEUE_OPTIONS    => ENQUEUE_OPTIONS,
                    MESSAGE_PROPERTIES => MESSAGE_PROPERTIES,
                    PAYLOAD            => EVENT,
                    MSGID              => MESSAGE_HANDLE);
  EXCEPTION
    WHEN OTHERS THEN
      ERROR_HANDLER.LOG_ERROR(NULL,
                              EVENT.ITEM,
                              EVENT.SEQ_NO,
                              SQLCODE,
                              SQLERRM,
                              O_STATUS_CODE,
                              O_ERROR_MSG);
      RAISE;
  END ADD_EVENT_TO_QUEUE;

And it's working because when I check my AQ table, I can find "the event", however my dequeue method is not dequeing, as you can see in the image bellow, there's no DEQ_TIME.

Here's my dequeue method, also from my ITEM_API package:

  PROCEDURE GET_QUEUE_FROM_QUEUE(CONTEXT  RAW,
                                 REGINFO  SYS.AQ$_REG_INFO,
                                 DESCR    SYS.AQ$_DESCRIPTOR,
                                 PAYLOAD  RAW,
                                 PAYLOADL NUMBER) IS

    R_DEQUEUE_OPTIONS    DBMS_AQ.DEQUEUE_OPTIONS_T;
    R_MESSAGE_PROPERTIES DBMS_AQ.MESSAGE_PROPERTIES_T;
    V_MESSAGE_HANDLE     RAW(16);
    I_PAYLOAD            ITEM_EVENT;
    L_PROC_EVENT         BOOLEAN;
    O_TARGETS            CFG_EVENT_STAGE_TBL;
    O_ERROR_MSG          VARCHAR2(300);
    O_STATUS_CODE        VARCHAR2(100);
  BEGIN
    R_DEQUEUE_OPTIONS.MSGID         := DESCR.MSG_ID;
    R_DEQUEUE_OPTIONS.CONSUMER_NAME := DESCR.CONSUMER_NAME;
    R_DEQUEUE_OPTIONS.DEQUEUE_MODE  := DBMS_AQ.REMOVE;
    --R_DEQUEUE_OPTIONS.WAIT          := DBMS_AQ.NO_WAIT;
    DBMS_AQ.DEQUEUE(QUEUE_NAME         => DESCR.QUEUE_NAME,
                    DEQUEUE_OPTIONS    => R_DEQUEUE_OPTIONS,
                    MESSAGE_PROPERTIES => R_MESSAGE_PROPERTIES,
                    PAYLOAD            => I_PAYLOAD,
                    MSGID              => V_MESSAGE_HANDLE);
    IF I_PAYLOAD IS NOT NULL THEN
      L_PROC_EVENT := PROCESS_EVENT(I_PAYLOAD,
                                    O_TARGETS,
                                    O_STATUS_CODE,
                                    O_ERROR_MSG);
    END IF;
  EXCEPTION
    WHEN OTHERS THEN
      ERROR_HANDLER.LOG_ERROR(NULL,
                              NULL,
                              NULL,
                              SQLCODE,
                              SQLERRM,
                              O_STATUS_CODE,
                              O_ERROR_MSG);
      RAISE;
  END GET_QUEUE_FROM_QUEUE;

Am I doing something wrong? How can I fix this? I think there might be a problem with my subscriber registration, but I'm not sure.

EDIT: I've just figured out that if I remove the subscribers and the register, and then re-add them, they'll dequeue all messages. Howerver if another event gets enqueued, it stays there indefinetly (or until I remove and add the subscribers again):

The record with state 0 and no DEQ_TIME is the new one.

Do I need a scheduler or something like that?

EDIT: I've added a scheduler propagation to my AQ:

DBMS_AQADM.SCHEDULE_PROPAGATION('ITEM_EVENT_QUEUE');

and even added the next_time field:

DBMS_AQADM.SCHEDULE_PROPAGATION('ITEM_EVENT_QUEUE', SYSDATE + 30/86400);

Still doesn't work. Any suggestions? I guess the AQ Notifications aren't working, and my callback procedure is never called. How can I fix this?

EDIT: I've removed my procedure from the package just for testing purposes, so my team mates can compile the ITEM_API package (I don't know if recompiling the package, may or may not have impacts on the dequeue process). Still doesn't work.

Answer

Fudztown picture Fudztown · May 28, 2015

Create a code block and run the following:

DECLARE
  dequeue_options      DBMS_AQ.dequeue_options_t;
  message_properties   DBMS_AQ.message_properties_t;
  message_handle       RAW (16);
  I_PAYLOAD            ITEM_EVENT;
  no_messages exception;
  msg_content          VARCHAR2 (4000);
  PRAGMA EXCEPTION_INIT (no_messages, -25228);
BEGIN
  dequeue_options.wait := DBMS_AQ.NO_WAIT;
  dequeue_options.consumer_name := 'ITEM_SUBSCRIBER_1';   
  dequeue_options.navigation := DBMS_AQ.FIRST_MESSAGE;
LOOP    
 DBMS_AQ.DEQUEUE (queue_name           => 'ITEM_EVENT_QUEUE',
                      dequeue_options      => dequeue_options,
                      message_properties   => message_properties,
                      payload              => I_PAYLOAD,
                      msgid                => message_handle
                     );
END LOOP;
  EXCEPTION
  WHEN no_messages
  THEN
     DBMS_OUTPUT.PUT_LINE ('No more messages left');
END;

Let me know what happens to your enqueued messages.

You should have a table where you're dequing the data.

Can you also try adding the enqueud table in the agent and then specify the agent to the dequeue table.

DECLARE
  aSubscriber sys.aq$_agent;
BEGIN 
  aSubscriber := sys.aq$_agent('ITEM_SUBSCRIBER_1',
                          'ITEM_EVENT_QUEUE',
                          0);
  dbms_aqadm.add_subscriber
 ( queue_name     => 'ITEM_EVENT_QUEUE'
  ,subscriber     => aSubscriber);
END;
/