高级队列操作

简介: grant aq_administrator_role to test; begin   dbms_aqadm.grant_type_access('test'); end;  create or replace type messageobj as object (   title ...

grant aq_administrator_role to test;

begin
  dbms_aqadm.grant_type_access('test');
end; 

create or replace type messageobj as object
(
  title varchar2(30),
  data1 number,
  data2 varchar2(100),
  data3 date,
 
  member procedure print(v_Message in varchar2)
);

create or replace type body MessageObj as
  member procedure print(v_Message in varchar2) is
  begin
    dbms_output.put_line(v_Message || ':'||title);
    dbms_output.put_line('Data 1:' || data1);
    dbms_output.put_line('Data 2:' || data2);
    dbms_output.put_line('Data 3:' || data3);
  end print;
end;
 
   -- prompt SimpleQ...
  begin
    --Create a simple table,with all of the defaults.This will allow
    --FIFO queues,with no message grouping or multiple consumers.
    dbms_aqadm.create_queue_table(
      queue_table=>'SimpleQTab',   
      queue_payload_type=>'MessageObj',
      comment=>'Simple Queue Table');
    dbms_aqadm.create_queue(
      queue_name=>'SimpleQ',
      queue_table=>'SimpleQTab',
      comment=>'Simple Queue');
    --Enable enqueue and dequeue within operations simpleQ.
    dbms_aqadm.create_queue(
      queue_name=>'ExceptionQ',
      queue_table=>'SimpleQTab',
      queue_type=>dbms_aqadm.exception_queue,
      comment=>'Exception Queue');
    --Enable dequeue operations for exceptionQ.
    dbms_aqadm.start_queue('ExceptionQ',False,true);
    end;
   
    begin
      --create a priority queue table,by sqecitying the sort order.
      --this queue has no message grouping or multiple consumers.
      dbms_aqadm.create_queue_table(
        queue_table=>'PriorityQTab',
        queue_payload_type=>'MessageObj',
        sort_list=>'priority,enq_time',
        comment=>'Priority queue table');
      dbms_aqadm.create_queue(
        queue_name=>'PriorityQ',
        queue_table=>'PriorityQTab',
        comment=>'Priority Queue');
       
      --Enable enqueue and dequeue operations for priorityq.
      dbms_aqadm.start_queue('PriorityQ');
    end;

--enqueue and dequeue opertion   
declare
  v_Message MessageObj;
  v_EnqueueOptions dbms_aq.enqueue_options_t;
  v_DequeueOptions dbms_aq.dequeue_options_t;   
  v_MessageProperties dbms_aq.message_properties_t;
  v_msgid raw(16);
  c_numMessages constant integer:=10;
 
  e_QTimeOut exception;
  pragma exception_init(e_QTimeOut,-25228);
 
  begin
    for v_Counter in 1..c_NumMessages loop
      --Create a message to enqueue.
      v_message := messageobj('Message'||v_counter,v_counter*10,'abcdefghijklmnopqrstuvwxyz',sysdate+v_counter);
     
      --enqueue it with the default options.
      dbms_aq.enqueue(
        queue_name => 'SimpleQ',
        enqueue_options => v_enqueueOptions,
        message_properties => v_messageproperties,
        payload =>v_message,
        msgid =>v_msgid);
    end loop;
    --commit all the enqueue.
    commit;
   
    --loop until there are no more message to dequeue.
    begin
      loop
        --dequeue the first message into v_message,waiting a maximum
        --of 1 second.
        v_dequeueOptions.wait :=1;
        dbms_aq.dequeue(
          queue_name =>'SimpleQ',
          dequeue_options =>v_dequeueOptions,
          message_properties =>v_messageProperties,
          payload =>v_message,
          msgid => v_msgid);
     
        --and print it.
        v_message.print('after dequeue');
      end loop;
    exception
      when e_QTimeOut then
        --end of the queue reached
        null;
    end;
    --commit all the dequeue.
   
    commit;
  end;
输出结果:
after dequeue:Message1
Data 1:10
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:27-11月-07
after dequeue:Message2
Data 1:20
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:28-11月-07
after dequeue:Message3
Data 1:30
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:29-11月-07
after dequeue:Message4
Data 1:40
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:30-11月-07
after dequeue:Message5
Data 1:50
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:01-12月-07
after dequeue:Message6
Data 1:60
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:02-12月-07
after dequeue:Message7
Data 1:70
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:03-12月-07
after dequeue:Message8
Data 1:80
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:04-12月-07
after dequeue:Message9
Data 1:90
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:05-12月-07
after dequeue:Message10
Data 1:100
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:06-12月-07
  
 
--priority enqueue and dequeue opertion   
declare
  v_Message MessageObj;
  v_EnqueueOptions dbms_aq.enqueue_options_t;
  v_DequeueOptions dbms_aq.dequeue_options_t;   
  v_MessageProperties dbms_aq.message_properties_t;
  v_msgid raw(16);
  c_numMessages constant integer:=10;
 
  e_QTimeOut exception;
  pragma exception_init(e_QTimeOut,-25228);
 
  begin
    for v_Counter in 1..c_NumMessages loop
      --Create a message to enqueue.
      v_message := messageobj('Message'||v_counter,v_counter*10,'abcdefghijklmnopqrstuvwxyz',sysdate+v_counter);
     
      --enqueue it with priority equal to v_counter.thus the last message to be enqueueed will have highest priority
      v_MessageProperties.priority := -v_counter;
      dbms_aq.enqueue(
        queue_name => 'PriorityQ',
        enqueue_options => v_enqueueOptions,
        message_properties => v_messageproperties,
        payload =>v_message,
        msgid =>v_msgid);
    end loop;
    --commit all the enqueue.
    commit;
   
    --loop until there are no more message to dequeue.
    begin
      loop
        --dequeue the first message into v_message,waiting a maximum
        --of 1 second.
        v_dequeueOptions.wait :=1;
        dbms_aq.dequeue(
          queue_name =>'PriorityQ',
          dequeue_options =>v_dequeueOptions,
          message_properties =>v_messageProperties,
          payload =>v_message,
          msgid => v_msgid);
     
        --and print it.
        v_message.print('after dequeue');
      end loop;
    exception
      when e_QTimeOut then
        --end of the queue reached
        null;
    end;
    --commit all the dequeue.
   
    commit;
  end; 

输出结果:
after dequeue:Message10
Data 1:100
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:06-12月-07
after dequeue:Message9
Data 1:90
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:05-12月-07
after dequeue:Message8
Data 1:80
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:04-12月-07
after dequeue:Message7
Data 1:70
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:03-12月-07
after dequeue:Message6
Data 1:60
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:02-12月-07
after dequeue:Message5
Data 1:50
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:01-12月-07
after dequeue:Message4
Data 1:40
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:30-11月-07
after dequeue:Message3
Data 1:30
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:29-11月-07
after dequeue:Message2
Data 1:20
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:28-11月-07
after dequeue:Message1
Data 1:10
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:27-11月-07


 --簡單的enqueue和dequeue操作
DECLARE
  v_Message MessageObj;
  v_EnqueueOptions dbms_aq.enqueue_options_t;
  v_DequeueOptions dbms_aq.dequeue_options_t;
  v_messageProperties dbms_aq.message_properties_t;
  v_MsgID RAW(16);
  c_numMessages CONSTANT INTEGER :=10;
  e_QTimeOut EXCEPTION;
  PRAGMA EXCEPTION_INIT(e_QTimeOut,-25228);
BEGIN
  FOR v_Counter IN 1..c_NumMessages LOOP
    --Create a message to enqueue.
    v_Message :=MessageObj('Message'||v_Counter,v_Counter*10,'abcdefghijklmnopqrstuvwxyz',SYSDATE+v_Counter);
   
    --Enqueue it with the default options.
    dbms_aq.enqueue(queue_name => 'SimpleQ',
      enqueue_options => v_enqueueoptions,
      message_properties => v_messageProperties,
      payload => v_Message,
      msgid => v_MsgID);
  END LOOP;
 
  --commit all the enqueue.
  COMMIT;
 
  --loop until there are no more message to dequeue.
  BEGIN
    LOOP
      --dequeue the first message into v_message,waiting a maximum of 1 second.
      v_DequeueOptions.WAIT := 1 ;
      dbms_aq.dequeue(queue_name => 'SimpleQ',
        dequeue_options => v_DequeueOptions,
        message_properties => v_messageProperties,
        payload => v_message,
        msgid => v_MsgID);
      --and print it.
      v_message.print('after dequeue');
    END LOOP;
  EXCEPTION
    WHEN e_QTimeOut THEN
      --end of the queue reached
      NULL;
  END;
  --commit all the dequeue.
  COMMIT;
                  
END;   

輸出結果
after dequeue:Message1
Data 1:10
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:02-12月-07
after dequeue:Message2
Data 1:20
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:03-12月-07
after dequeue:Message3
Data 1:30
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:04-12月-07
after dequeue:Message4
Data 1:40
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:05-12月-07
after dequeue:Message5
Data 1:50
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:06-12月-07
after dequeue:Message6
Data 1:60
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:07-12月-07
after dequeue:Message7
Data 1:70
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:08-12月-07
after dequeue:Message8
Data 1:80
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:09-12月-07
after dequeue:Message9
Data 1:90
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:10-12月-07
after dequeue:Message10
Data 1:100
Data 2:abcdefghijklmnopqrstuvwxyz
Data 3:11-12月-07

目录
相关文章
|
7月前
|
Java
动态移除异步
动态移除异步
43 2
|
2月前
|
索引 Python
列表作为序列型对象都支持哪些操作,它们在底层是怎么实现的?
列表作为序列型对象都支持哪些操作,它们在底层是怎么实现的?
53 4
|
7月前
|
算法 编译器 C语言
【C/C++ 编程题 02】用两个栈实现一个队列的功能
【C/C++ 编程题 02】用两个栈实现一个队列的功能
55 0
|
7月前
|
算法 数据安全/隐私保护 C++
【C/C++ 编程题 03】用两个栈实现一个队列的功能
【C/C++ 编程题 03】用两个栈实现一个队列的功能
35 0
|
7月前
|
安全 算法 调度
C++队列探秘:队列容器的使用技巧与实战案例解析
C++队列探秘:队列容器的使用技巧与实战案例解析
228 0
|
7月前
|
存储 调度
FreeRTOS深入教程(队列内部机制和源码分析)
FreeRTOS深入教程(队列内部机制和源码分析)
167 0
|
存储
什么是队列,如何实现?
什么是队列,如何实现?
116 0
什么是队列,如何实现?
|
消息中间件
消息队列:第四章:延迟检查队列
消息队列:第四章:延迟检查队列
160 0
消息队列:第四章:延迟检查队列
|
Java 容器
并发队列简介
并发队列简介
112 0
并发队列简介
|
存储 算法 JavaScript
如何实现一个队列
队列,是一种常见的逻辑数据结构。具备什么特点呢?经常性的我们会听到一个类比“队列就像队伍过桥洞”,队列中的元素遵循了“先进先出、后进后出”的原则。 在JavaScript中有很多的方式来实现一个队列,让我们一起来看看都是如何实现的呢?
338 0