Oracle Advanced Queue

Source: Internet
Author: User

Reprint: http://www.idevelopment.info/data/Oracle/DBA_tips/Advanced_Queuing/AQ_2.shtml

Overview

This
article provides a brief overview on configuring and Using Oracle's advanced Queuing features using PL/SQL. This would demonstrate the basic functionality of Oracle Advanced Queuing (AQ). AQ is first introduced in Oracle8 and have been extended and improved on into the future versions.

Setup and Configuration

Within this sections of the article, I provide the steps required to configure our QA environment. This involves creating users and assigning them the privileges required to perform all necessary AQ operations.

Before creating the users, let's take a look at the the major roles that is provided for performing AQ administration and User operations:

Aq_administrator_role

This role allows for the creation and administration of the
queuing infrastructure.
Aq_user_role
This
role allows the users to access queues for enqueue and dequeue operations.

Now, let's create the following and the schemas; One owning the queuing infrastructure and another with access to it:

Create_aq_users.sql

CONNECT Sys/change_on_install as SYSDBA---------------------------------------------------------DROP USER aq_admin_ Plsql CASCADE; CREATE USER Aq_admin_plsql identified by Aq_admin_plsql DEFAULT tablespace users temporary tablespace temp; ALTER USER aq_admin_plsql QUOTA UNLIMITED on users; GRANT Aq_administrator_role to Aq_admin_plsql; GRANT Connect to aq_admin_plsql; GRANT Create type to aq_admin_plsql; GRANT create sequence to aq_admin_plsql; EXECUTE dbms_aqadm.grant_type_access (' aq_admin_plsql ');-------------------------------------------------------- -drop USER Aq_user_plsql CASCADE; CREATE USER Aq_user_plsql identified by Aq_user_plsql DEFAULT tablespace users temporary tablespace temp; GRANT Aq_user_role to Aq_user_plsql; GRANT Connect to aq_user_plsql;

define Payload

The content, or payload, of a message is often defined using an OBJECT TYPE. We must define this before creating the queue. We also grant EXECUTE on the Payload object type to our AQ User:
Create_payload.sql

CONNECT aq_admin_plsql/aq_admin_plsqlcreate TYPE Message_type as OBJECT ( message_id number ) , subject VARCHAR2 (+) , text VARCHAR2 (+) , dollar_value number (4,2))/grant EXECUTE on Message_type to Aq_user_plsql; CREATE SEQUENCE message_seq INCREMENT by 1 START with nomaxvalue nocycle; GRANT Select on Message_seq to Aq_user_plsql;

Create Queue Table and queue

Now and we have the payload created, it's time to create the queuing infrastructure. Queues is implemented using a queue table which can hold multiple Queues with the same payload type.

First the queue table must is defined using the payload type, then the queue can be defined and started. All of these operations can is performed using the Dbms_aqadm package as follows:

Create_queue_table_and_queue.sql

CONNECT aq_admin_plsql/aq_admin_plsqlset serveroutput Onbegin---------------------------------------------------- -----Dbms_aqadm. Create_queue_table (queue_table = ' aq_admin_plsql.msg_qt ', Queue_payload_type = ' aq_admin_p Lsql.message_type '); ---------------------------------------------------------dbms_aqadm. Create_queue (queue_name = ' Msg_queue ', queue_table = ' aq_admin_plsql.msg_qt ' , Queue_type = Dbms_aqadm. Normal_queue, max_retries = 0, Retry_delay = 0, Retention_time = 120960 0, dependency_tracking = FALSE, comment = ' Test Object Type Queue ', auto_commit = FALSE); ---------------------------------------------------------dbms_aqadm. Start_queue (' Msg_queue '); ---------------------------------------------------------dbms_aqadm. Grant_queue_privilege (priVilege = ' All ', queue_name = ' aq_admin_plsql.msg_queue ', grantee = ' Aq_us Er_plsql ', grant_option = FALSE); ---------------------------------------------------------end;/

Enqueue Message

Use the Dbms_aq. ENQUEUEProcedure to write messages to the queue:
Enqueue_message.sql

CONNECT aq_user_plsql/aq_user_plsqlset serveroutput ondeclare enqueue_options dbms_aq.enqueue_options_t; Message_properties dbms_aq.message_properties_t; Message_handle RAW (16); Message Aq_admin_plsql.message_type; MESSAGE_ID number; BEGIN---------------------------------------------------------SELECT aq_admin_plsql.message_seq.nextval into message_id from dual; ---------------------------------------------------------message: = Aq_admin_plsql. Message_type (message_id, ' subject:example message ', ' message: This is A SAMPLE MESSAGE. ' , 10.2); ---------------------------------------------------------enqueue_options. VISIBILITY: = Dbms_aq. On_commit; Enqueue_options. Sequence_deviation: = null; ---------------------------------------------------------message_properties. Priority: =-5; MesSage_properties. DELAY: = Dbms_aq.no_delay; Message_properties. Expiration: = Dbms_aq. Never; Message_properties. CORRELATION: = ' TEST MESSAGE '; ---------------------------------------------------------Dbms_aq. ENQUEUE (queue_name = ' Aq_admin_plsql.msg_queue ', enqueue_options = enqueue_options , message_properties = message_properties, payload = message, MsgId = Message_handle); ---------------------------------------------------------COMMIT; ---------------------------------------------------------end;/

Dequeue Message

Use the Dbms_aq. DEQUEUEProcedure to read messages from the queue:
Dequeue_message.sql

CONNECT aq_user_plsql/aq_user_plsqlset serveroutput ondeclare dequeue_options dbms_aq.dequeue_options_t; Message_properties dbms_aq.message_properties_t; Message_handle RAW (16); Message Aq_admin_plsql.message_type; BEGIN---------------------------------------------------------dequeue_options. Consumer_name: = NULL; Dequeue_options. Dequeue_mode: = Dbms_aq. REMOVE; Dequeue_options. NAVIGATION: = Dbms_aq. Next_message; Dequeue_options. VISIBILITY: = Dbms_aq. IMMEDIATE; Dequeue_options. WAIT: = Dbms_aq. FOREVER; Dequeue_options. MSGID: = null; Dequeue_options. CORRELATION: = ' TEST MESSAGE '; ---------------------------------------------------------Dbms_aq. DEQUEUE (queue_name = ' Aq_admin_plsql.msg_queue ', dequeue_options = dequeue_options , message_properties = message_properties, payload = = message, MsgId = Message_handle); ---------------------------------------------------------dbms_output.put_line (' +-----------------+ '); Dbms_output.put_line (' | MESSAGE PAYLOAD | '); Dbms_output.put_line (' +-----------------+ '); Dbms_output.put_line ('-Message ID: = ' | | message.message_id); Dbms_output.put_line ('-Subject: = ' | | message.subject); Dbms_output.put_line ('-Message: = ' | | message.text); Dbms_output.put_line ('-Dollar Value: = ' | | message.dollar_value); ---------------------------------------------------------COMMIT; ---------------------------------------------------------end;/
Script Output
connected.+-----------------+| Message PAYLOAD |+-----------------+-Message ID   : = 1000-subject      : = Subject:example message-message      : = Mes Sage:this is A SAMPLE message.-Dollar Value: = 10.2pl/sql procedure successfully completed.

Dropping all Objects

Drop_aq_objects.sql

Connect Aq_admin_plsql/aq_admin_plsqlexecute dbms_aqadm.stop_queue (queue_name = ' aq_admin_plsql.msg_queue '); EXECUTE dbms_aqadm.drop_queue (queue_name = ' aq_admin_plsql.msg_queue '); EXECUTE dbms_aqadm.drop_queue_table (queue_table = ' aq_admin_plsql.msg_qt ');D ROP TYPE Aq_admin_plsql.message_ Type;drop SEQUENCE aq_admin_plsql.message_seq;connect Sys/change_on_install as Sysdbadrop USER aq_user_plsql CASCADE;D ROP USER Aq_admin_plsql CASCADE;

Oracle Advanced Queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.