如何从PL / SQL向Oracle AQ消息队列上的主题发布消息

时间:2017-11-30 12:25:52

标签: oracle plsql publish advanced-queuing

我知道我可以使用JMS TopicPublisher从Java代码向Oracle AQ队列发布消息到给定主题(有大量的doc和样本)但我真的需要从PL /发布消息到给定主题Qracle AQ队列的SQL代码。 我找不到文档,也没有关于如何做到这一点的例子。可能吗?有人可以指出正确的文件或样品位置???

感谢任何帮助,

Jan van de klok

1 个答案:

答案 0 :(得分:0)

您可以在Streams Advanced Queuing User's Guide

中找到包含示例的所有信息

下面的示例是从文档中提取的,稍作修改

    CREATE TYPE message_typ AS object(
       sender_id              NUMBER,
       subject                VARCHAR2(30),
       text                   VARCHAR2(1000));
    /


    BEGIN
       DBMS_AQADM.CREATE_QUEUE_TABLE(
          queue_table            => 'obj_qtab',
          queue_payload_type     => 'message_typ',
          multiple_consumers     => true
          );
    END;
    /

    BEGIN
       DBMS_AQADM.CREATE_QUEUE(
          queue_name         =>  'obj_queue',
          queue_table        =>  'obj_qtab');
    END;
    /

    BEGIN
       DBMS_AQADM.START_QUEUE (
          queue_name         => 'obj_queue');
    END;
    /

DECLARE
   subscriber         sys.aq$_agent;
BEGIN
   subscriber     :=  sys.aq$_agent('RED', NULL, NULL);
   DBMS_AQADM.ADD_SUBSCRIBER(
      queue_name  =>  'obj_queue',
      subscriber  =>  subscriber);

   subscriber     :=  sys.aq$_agent('GREEN', NULL, NULL);
   DBMS_AQADM.ADD_SUBSCRIBER(
      queue_name  =>  'obj_queue',
      subscriber  =>  subscriber); 

         subscriber     :=  sys.aq$_agent('YELLOW', NULL, NULL);
   DBMS_AQADM.ADD_SUBSCRIBER(
      queue_name  =>  'obj_queue',
      subscriber  =>  subscriber);

END;
/

脚本执行,创建对象,创建队列表,创建队列,启动队列和注册3个订阅者。

DECLARE
   enqueue_options     DBMS_AQ.enqueue_options_t;
   message_properties  DBMS_AQ.message_properties_t;
   recipients          DBMS_AQ.aq$_recipient_list_t;
   message_handle      RAW(16);
   message             message_typ;

    recipients2          DBMS_AQ.aq$_recipient_list_t;
BEGIN
   message := message_typ(001, 'MESSAGE 1','For queue subscribers');
   DBMS_AQ.ENQUEUE(
      queue_name          => 'obj_queue',
      enqueue_options     => enqueue_options,
      message_properties  => message_properties,
      payload             => message,
      msgid               => message_handle);
-- Message for all subscriber

   message := message_typ(001, 'MESSAGE 2', 'For two recipients');
   recipients(1) := sys.aq$_agent('RED', NULL, NULL);
   recipients(2) := sys.aq$_agent('BLUE', NULL, NULL);
   message_properties.recipient_list := recipients;
   DBMS_AQ.ENQUEUE(
      queue_name          => 'obj_queue',
      enqueue_options     => enqueue_options,
      message_properties  => message_properties,
      payload             => message,
      msgid               => message_handle);
-- Message for RED,BLUE  subscriber   


   recipients2(1)   := sys.aq$_agent('RED', NULL, NULL);   
   message := message_typ(001, 'MESSAGE 3', 'For RED Subscriber');      
   message_properties.recipient_list := recipients2;
   DBMS_AQ.ENQUEUE(
      queue_name          => 'obj_queue',
      enqueue_options     => enqueue_options,
      message_properties  => message_properties,
      payload             => message,
      msgid               => message_handle);
-- Message for RED  subscriber   
   COMMIT;
END;
/

要检查结果,您必须查询队列表视图(AQ$queue_table_name) 在这种情况下它是。

select o.user_data,CONSUMER_NAME  from AQ$obj_qtab o order by MSG_ID;