Skip to Main Content

Database Software

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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

dbms_scheduler - dbms_aq - dbms_aqadm - event driven job runs only once/sec

925824Mar 22 2012 — edited Mar 23 2012
hi, i tried here to process events with 2 jobs... surprisingly i had to call dbms_lock.sleep(1) to be able to enqueue events
at inserts in a row ... even more surprisingly the enqueuing doesn't function if the processing (prc_evsched) lasts some seconds...

how must the code be changed to be able to enqueue all events after the inserts without any sleep()?
and how must the code be changed to be able to enqueue all events with a proc (prc_evsched) running longer?

thanx in advance for your help!




-----------------------------------------
--evsched
--
h4.
--to remove all test-objects
h4.
--test table
h4.
--define the object type to act as the payload for the queue
h4.
--creating the event queue
h4.
--creating the proc for the prog/job
h4.
--creating the proc for the prog/job
h4.
--creating the program
h4.
--creating the first job (for parallel execution)
h4.
--creating the second job (for parallel execution)
h4.
--test block
h4.
--test scenarios/results
-----------------------------------------
h4.
--to remove all test-objects
DECLARE
exc_ora_27475 EXCEPTION; --ora-27475: <job_name> muss job sein
PRAGMA EXCEPTION_INIT (exc_ora_27475, -27475);

exc_ora_27476 EXCEPTION; --ora-27476: <program_name> ist nicht vorhanden
PRAGMA EXCEPTION_INIT (exc_ora_27476, -27476);

exc_ora_24010 EXCEPTION; --ORA-24010: QUEUE SYSGIS.EVENT_QUEUE ist nicht vorhanden
PRAGMA EXCEPTION_INIT (exc_ora_24010, -24010);

exc_ora_24002 EXCEPTION; --ORA-24002: QUEUE_TABLE SYSGIS.EVENT_QUEUE_TAB ist nicht vorhanden
PRAGMA EXCEPTION_INIT (exc_ora_24002, -24002);

exc_ora_4043 EXCEPTION; --ORA-04043: Objekt T_EVENT_QUEUE_PAYLOAD ist nicht vorhanden
PRAGMA EXCEPTION_INIT (exc_ora_4043, -4043);

exc_ora_942 EXCEPTION; --ORA-00942: Tabelle oder View nicht vorhanden
PRAGMA EXCEPTION_INIT (exc_ora_942, -942);

exc_ora_2289 EXCEPTION; --ORA-02289: Sequence ist nicht vorhanden.
PRAGMA EXCEPTION_INIT (exc_ora_2289, -2289);
v_fpos PLS_INTEGER := 1;
BEGIN
v_fpos := 10;
BEGIN
--remove job
SYS.DBMS_SCHEDULER.DROP_JOB(job_name=> 'job_evsched', FORCE=>TRUE);
EXCEPTION WHEN exc_ora_27475 THEN NULL;
END;

v_fpos := 12;
BEGIN
--remove job
SYS.DBMS_SCHEDULER.DROP_JOB(job_name=> 'job_evsched_2', FORCE=>TRUE);
EXCEPTION WHEN exc_ora_27475 THEN NULL;
END;

v_fpos := 20;
BEGIN
--remove program
SYS.DBMS_SCHEDULER.DROP_PROGRAM(program_name=>'prg_evsched',FORCE=>TRUE);
EXCEPTION WHEN exc_ora_27476 THEN NULL;
END;

v_fpos := 30;
BEGIN
-- stop the event queue.
DBMS_AQADM.stop_queue (queue_name => 'evsched_event_queue');
EXCEPTION WHEN exc_ora_24010 THEN NULL;
END;

v_fpos := 40;
BEGIN
-- drop the event queue.
DBMS_AQADM.drop_queue (queue_name => 'evsched_event_queue');
EXCEPTION WHEN exc_ora_24010 THEN NULL;
END;

v_fpos := 50;
BEGIN
-- Remove the queue table.
DBMS_AQADM.drop_queue_table(queue_table => 'tab_evsched_event_queue');
EXCEPTION WHEN exc_ora_24002 THEN NULL;
END;

v_fpos := 60;
BEGIN
-- remove type
EXECUTE IMMEDIATE 'DROP TYPE typ_evsched_payload';
EXCEPTION WHEN exc_ora_4043 THEN NULL;
END;

v_fpos := 70;
BEGIN
-- remove table
EXECUTE IMMEDIATE 'DROP TABLE tab_evsched';
EXCEPTION WHEN exc_ora_942 THEN NULL;
END;

v_fpos := 80;
BEGIN
-- remove sequence
EXECUTE IMMEDIATE 'DROP SEQUENCE seq_evsched';
EXCEPTION WHEN exc_ora_2289 THEN NULL;
END;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('v_fpos='||v_fpos);
END;
/
-----------------------------------------
h4.
--test table
CREATE TABLE tab_evsched
( id NUMBER
, sys_date DATE
, status VARCHAR2(1)
, processed VARCHAR2(1) DEFAULT 'N'
, processed_by varchar2(128)
)
/
GRANT DELETE, INSERT, SELECT, UPDATE ON tab_evsched TO PUBLIC
/
CREATE SEQUENCE seq_evsched
/
-----------------------------------------
h4.
--define the object type to act as the payload for the queue
CREATE OR REPLACE TYPE typ_evsched_payload AS OBJECT
( event_name VARCHAR2(30)
, tab_evsched_id NUMBER
, daterf DATE
);
/
-----------------------------------------
h4.
--creating the event queue
BEGIN
-- Create a queue table to hold the event queue.
DBMS_AQADM.create_queue_table
( queue_table => 'tab_evsched_event_queue'
, queue_payload_type => 'typ_evsched_payload'
, multiple_consumers => TRUE
, COMMENT => 'Queue Table For Event Messages'
);
-- Create the event queue.
DBMS_AQADM.create_queue
( queue_name => 'evsched_event_queue'
, queue_table => 'tab_evsched_event_queue'
, queue_type => DBMS_AQADM.NORMAL_QUEUE
, max_retries => 0
, retry_delay => 0
, dependency_tracking => FALSE
, comment => 'Test Object Type Queue'
, auto_commit => FALSE
);
-- Start the event queue.
DBMS_AQADM.start_queue
( queue_name => 'evsched_event_queue'
);
END;
/
-----------------------------------------
h4.
--creating the proc for the prog/job
CREATE OR REPLACE PROCEDURE prc_evsched
( p_message IN typ_evsched_payload
, p_job_name IN VARCHAR2
)
IS
BEGIN
UPDATE tab_evsched
SET processed = 'J'
, processed_by = p_job_name
, sys_date = p_message.daterf
WHERE 1=1
AND id = p_message.tab_evsched_id
;

dbms_lock.sleep(5); --#sleep-1#

COMMIT;
END prc_evsched;
/
-----------------------------------------
h4.
--creating the program
DECLARE
exc_ora_27476 EXCEPTION; --ora-27476: <program_name> ist nicht vorhanden
PRAGMA EXCEPTION_INIT (exc_ora_27476, -27476);
BEGIN
BEGIN
SYS.DBMS_SCHEDULER.DROP_PROGRAM(program_name=>'prg_evsched',FORCE=>TRUE);
EXCEPTION
WHEN exc_ora_27476 THEN
NULL;
END;

SYS.DBMS_SCHEDULER.CREATE_PROGRAM
( program_name => 'prg_evsched'
, program_type => 'STORED_PROCEDURE'
, program_action => '"SYSGIS"."PRC_EVSCHED"'
, number_of_arguments => 2
, enabled => FALSE
, comments => 'Program-Komponent für den Test von DBMS_SCHEDULER'
);

--event message as the first param to prc_evsched
DBMS_SCHEDULER.DEFINE_METADATA_ARGUMENT
( program_name => 'prg_evsched'
, argument_position => 1
, metadata_attribute => 'EVENT_MESSAGE'
);

--name of the job as the second param to prc_evsched
SYS.DBMS_SCHEDULER.define_program_argument
( program_name => 'prg_evsched'
, argument_name => 'p_job_name'
, argument_position => 2
, argument_type => 'VARCHAR2'
, default_value => 'dummy'
);

--enable program
SYS.DBMS_SCHEDULER.ENABLE(NAME=>'prg_evsched');
END;
/
-----------------------------------------
h4.
--creating the first job (for parallel execution)
DECLARE
exc_ora_27475 EXCEPTION; --ora-27475: <job_name> muss job sein
PRAGMA EXCEPTION_INIT (exc_ora_27475, -27475);
BEGIN
BEGIN
--remove job
SYS.DBMS_SCHEDULER.DROP_JOB(job_name=> 'job_evsched', FORCE=>TRUE);
EXCEPTION WHEN exc_ora_27475 THEN NULL;
END;

DBMS_SCHEDULER.create_job
( job_name => 'job_evsched'
, program_name => 'prg_evsched'
, start_date => SYSTIMESTAMP
, event_condition => 'tab.user_data.event_name = ''MYEVENT'''
, queue_spec => 'evsched_event_queue'
, auto_drop => FALSE
, enabled => FALSE
);

SYS.DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE
( job_name => 'job_evsched'
, argument_name => 'p_job_name'
, argument_value => 'job_evsched'
);

SYS.DBMS_SCHEDULER.ENABLE(NAME=>'job_evsched');
END;
/
-----------------------------------------
h4.
--creating the second job (for parallel execution)
DECLARE
exc_ora_27475 EXCEPTION; --ora-27475: <job_name> muss job sein
PRAGMA EXCEPTION_INIT (exc_ora_27475, -27475);
BEGIN
BEGIN
--remove job
SYS.DBMS_SCHEDULER.DROP_JOB(job_name=> 'job_evsched_2', FORCE=>TRUE);
EXCEPTION WHEN exc_ora_27475 THEN NULL;
END;

DBMS_SCHEDULER.create_job
( job_name => 'job_evsched_2'
, program_name => 'prg_evsched'
, start_date => SYSTIMESTAMP
, event_condition => 'tab.user_data.event_name = ''MYEVENT'''
, queue_spec => 'evsched_event_queue'
, auto_drop => FALSE
, enabled => FALSE
);

SYS.DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE
( job_name => 'job_evsched_2'
, argument_name => 'p_job_name'
, argument_value => 'job_evsched_2'
);

SYS.DBMS_SCHEDULER.ENABLE(NAME=>'job_evsched_2');
END;
/
-----------------------------------------
h4.
--test block
DECLARE
PROCEDURE pr_ins_tab_evsched
( p_id IN NUMBER
, p_status IN VARCHAR2
)
IS
BEGIN
INSERT INTO tab_evsched(id,status) VALUES (p_id, p_status);
COMMIT;

--enqueue the MYEVENT-event
IF p_status = 'M' THEN

--enqueue works in 1sekunden-taktung.... !?
dbms_lock.sleep(1); --#sleep-2#

DECLARE
v_enqueue_options DBMS_AQ.enqueue_options_t;
v_message_properties DBMS_AQ.message_properties_t;
v_message_handle RAW(16);
v_queue_msg typ_evsched_payload;
BEGIN
v_queue_msg := typ_evsched_payload
( event_name => 'MYEVENT'
, tab_evsched_id => p_id
, daterf => SYSDATE
);
v_enqueue_options.VISIBILITY := DBMS_AQ.ON_COMMIT;
v_enqueue_options.delivery_mode := DBMS_AQ.PERSISTENT;
v_message_properties.PRIORITY := 1;
v_message_properties.DELAY := DBMS_AQ.NO_DELAY;
v_message_properties.EXPIRATION := DBMS_AQ.NEVER;
v_message_properties.CORRELATION := 'TEST MESSAGE';
DBMS_AQ.enqueue
( queue_name => 'evsched_event_queue'
, enqueue_options => v_enqueue_options
, message_properties => v_message_properties
, payload => v_queue_msg
, msgid => v_message_handle
);
END;
END IF;
COMMIT;
END pr_ins_tab_evsched;
BEGIN
DELETE tab_evsched;
pr_ins_tab_evsched (seq_evsched.NEXTVAL,'M');
pr_ins_tab_evsched (seq_evsched.NEXTVAL,'M');
pr_ins_tab_evsched (seq_evsched.NEXTVAL,'M');
pr_ins_tab_evsched (seq_evsched.NEXTVAL,'M');
pr_ins_tab_evsched (seq_evsched.NEXTVAL,'M');
pr_ins_tab_evsched (seq_evsched.NEXTVAL,'M');
pr_ins_tab_evsched (seq_evsched.NEXTVAL,'M');
pr_ins_tab_evsched (seq_evsched.NEXTVAL,'M');
END;
/
-----------------------------------------
SELECT * FROM tab_evsched ORDER BY id;
SELECT * FROM sysgis.tab_evsched_event_queue;


-----------------------------------------
h4.
--test scenarios/resultsh

h5.
--test results #sleep-1#=5, #sleep-2#=0
--job (job_evsched) runs only once, only the first event is enqued/processed

/*
ID SYS_DATE STATUS PROCESSED PROCESSED_BY
.. ..................... ....... ......... .............
9 22.03.2012 17:00:41 M J job_evsched
10 (Null) M N (Null)
11 (Null) M N (Null)
12 (Null) M N (Null)
13 (Null) M N (Null)
14 (Null) M N (Null)
15 (Null) M N (Null)
16 (Null) M N (Null)
*/

-----------------------------------------
h5.
--test results #sleep-1#=0, #sleep-2#=1
--jobs (job_evsched/job_evsched2) run alternately, every events are enqued/processed
/*
ID SYS_DATE STATUS PROCESSED PROCESSED_BY
.. ..................... ....... ......... .............
25 22.03.2012 17:04:31 M J job_evsched_2
26 22.03.2012 17:04:32 M J job_evsched_2
27 22.03.2012 17:04:33 M J job_evsched
28 22.03.2012 17:04:34 M J job_evsched_2
29 22.03.2012 17:04:35 M J job_evsched_2
30 22.03.2012 17:04:36 M J job_evsched_2
31 22.03.2012 17:04:37 M J job_evsched
32 22.03.2012 17:04:38 M J job_evsched_2
*/

-----------------------------------------
h5.
--test results #sleep-1#=5, #sleep-2#=1
--jobs (job_evsched/job_evsched2) run alternately, only two events are enqued/processed
/*
ID SYS_DATE STATUS PROCESSED PROCESSED_BY
.. ..................... ....... ......... .............
41 22.03.2012 17:07:42 M J job_evsched_2
42 (Null) M N (Null)
43 (Null) M N (Null)
44 (Null) M N (Null)
45 (Null) M N (Null)
46 (Null) M N (Null)
47 22.03.2012 17:07:48 M J job_evsched
48 (Null) M N (Null)
*/

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 20 2012
Added on Mar 22 2012
4 comments
587 views