Bo's Oracle Station

查看: 2474|回复: 0

第59次:2015-06-02星期二

[复制链接]

1005

主题

1469

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12012
发表于 2015-5-29 09:50:39 | 显示全部楼层 |阅读模式
本帖最后由 botang 于 2015-6-3 08:43 编辑
  1. select    from dba_scheduler_windows;

  2. select   username, resource_consumer_group      from  v$session where terminal='pts/6';

  3. select     username , u.initial_rsrc_consumer_group     from dba_users  u where username in ('SYS','SYSTEM','HR');

  4. select  * from dba_rsrc_plans;

  5. BEGIN
  6. dbms_resource_manager.clear_pending_area();
  7. dbms_resource_manager.create_pending_area();
  8. dbms_resource_manager.create_plan( plan => 'PLAN2', comment => 'PLAN2',mgmt_mth => 'RATIO' );
  9. dbms_resource_manager.create_plan_directive(
  10.     plan => 'PLAN2',
  11.     group_or_subplan => 'SYS_GROUP',
  12.     comment => 'SYS_GROUP',
  13.     mgmt_p1 => 5   
  14. );

  15. dbms_resource_manager.create_plan_directive(
  16.     plan =>'PLAN2' ,
  17.     group_or_subplan => 'GROUP1',
  18.     comment => 'GROUP1',
  19.     mgmt_p1 => 3
  20. );
  21. dbms_resource_manager.create_plan_directive(
  22.     plan => 'PLAN2',
  23.     group_or_subplan => 'GROUP2',
  24.     comment => 'GROUP2',
  25.     mgmt_p1 => 2
  26. );
  27. dbms_resource_manager.create_plan_directive(
  28.     plan => 'PLAN2',
  29.     group_or_subplan => 'OTHER_GROUPS',
  30.     comment => 'OTHER_GROUPS',
  31.     mgmt_p1 => 1
  32. );
  33. dbms_resource_manager.submit_pending_area();
  34. END;


  35. select  * from dba_rsrc_plan_directives rpd where rpd.plan='PLAN1';


  36. select  degree  from dba_indexes where index_name='IBIG' and owner='HR';


  37. select  username,  status ,resource_consumer_group    from  v$session where  username='HR' ;

复制代码





复杂scheduler
  1. BEGIN
  2. /* Complex scheduling example 1: Public Holidays */
  3. DBMS_SCHEDULER.CREATE_SCHEDULE
  4. (schedule_name => 'SCHD_2008_PUBLIC_HOLIDAYS'
  5. ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
  6. ,end_date => to_timestamp_tz('31/12/2008 23:59:59','dd/mm/yyyy hh24:mi:ss')
  7. ,repeat_interval =>
  8. 'freq=DAILY;
  9. bydate=20080101,20080128,20080303,20080321,20080324,20080425,
  10. 20080929,20081225,20081226'
  11. ,comments => 'Public Holidays 2008'
  12. );
  13. END;
  14. /
  15. The following example creates a schedule consisting of all 2008 working days:
  16. BEGIN
  17. /* Complex scheduling example 2: Working Days */
  18. DBMS_SCHEDULER.CREATE_SCHEDULE
  19. (schedule_name => 'SCHD_2008_WORKING_DAYS'
  20. ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
  21. ,end_date => to_timestamp_tz('31/12/2008 23:59:59','dd/mm/yyyy hh24:mi:ss')
  22. ,repeat_interval =>
  23. 'freq=DAILY;
  24. byday=MON,TUE,WED,THU,FRI;
  25. exclude=SCHD_2008_PUBLIC_HOLIDAYS'
  26. ,comments => 'Run on every working day'
  27. );
  28. END;
  29. /
  30. The following example creates a schedule consisting of the first working day of every month in 2008:
  31. BEGIN
  32. /* Complex scheduling example 3: 1st Working Day in a Month */
  33. DBMS_SCHEDULER.CREATE_SCHEDULE
  34. (schedule_name => 'SCHD_2008_1ST_WK_DAY_EACH_MTH'
  35. ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
  36. ,end_date => to_timestamp_tz('31/12/2008 23:59:59','dd/mm/yyyy hh24:mi:ss')
  37. ,repeat_interval =>
  38. 'freq=MONTHLY;
  39. byday=MON,TUE,WED,THU,FRI;
  40. exclude=SCHD_2008_PUBLIC_HOLIDAYS;
  41. bysetpos=1'
  42. ,comments => 'Run on 1st working day every month'
  43. );
  44. END;
  45. /
  46. -------------------------------------------------------------
  47. BEGIN
  48. /* Complex scheduling example 4: 1st and 3rd Monday each month */
  49. DBMS_SCHEDULER.CREATE_SCHEDULE
  50. (schedule_name => 'SCHD_1ST_3RD_MON_EACH_MTH'
  51. ,start_date => to_timestamp_tz('03/12/2007 05:00:00','dd/mm/yyyy hh24:mi:ss')
  52. ,repeat_interval =>
  53. 'freq=MONTHLY;
  54. byday=1MON,3MON'
  55. ,comments => 'Run on every 1st and 3rd Monday each month'
  56. );
  57. END;
  58. /
  59. The following example creates a schedule consisting of the last day of each quarter:
  60. BEGIN
  61. /* Complex scheduling example 5: Quarter */
  62. DBMS_SCHEDULER.CREATE_SCHEDULE
  63. (schedule_name => 'SCHD_LAST_DAY_EACH_QTR'
  64. ,start_date => to_timestamp_tz('31/12/2008 05:00:00','dd/mm/yyyy hh24:mi:ss')
  65. ,repeat_interval =>
  66. 'freq=YEARLY;
  67. bymonth=MAR,JUN,SEP,DEC;
  68. bymonthday=-1'
  69. ,comments => 'Run on every last day each quarter'
  70. );
  71. END;
  72. /
  73. -------------------------------------------------------------
  74. CREATE OR REPLACE PROCEDURE show_schedule
  75. (schedule_name in varchar2
  76. ,start_date in timestamp
  77. ,repeat_interval in varchar2
  78. ,iteration in number)
  79. AS
  80. v_start_date timestamp;
  81. v_after_date timestamp;
  82. v_next_date timestamp;
  83. BEGIN
  84. dbms_output.put_line (schedule_name);
  85. v_start_date := start_date;
  86. v_after_date := v_start_date-(1/86400);
  87. for i in 1..iteration loop
  88. dbms_scheduler.evaluate_calendar_string
  89. (repeat_interval
  90. ,v_start_date
  91. ,v_after_date
  92. ,v_next_date
  93. );
  94. dbms_output.put_line (v_next_date);
  95. v_after_date:= v_next_date;
  96. end loop;
  97. dbms_output.put_line ('============================');
  98. END;
  99. /
  100. -- --------------------------------------------------------
  101. set serveroutput on
  102. exec dbms_output.enable (1000000);
  103. begin
  104. show_schedule
  105. (schedule_name => 'SCHD_2008_PUBLIC_HOLIDAYS'
  106. ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
  107. ,repeat_interval =>
  108. 'freq=DAILY;
  109. bydate=20080101,20080128,20080303,20080321,20080324,20080425,
  110. 20080929,20081225,20081226'
  111. ,iteration => 9);
  112. show_schedule
  113. (schedule_name => 'SCHD_2008_WORKING_DAYS'
  114. ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
  115. ,repeat_interval =>
  116. 'freq=DAILY;
  117. byday=MON,TUE,WED,THU,FRI;
  118. exclude=SCHD_2008_PUBLIC_HOLIDAYS'
  119. ,iteration => 21);
  120. show_schedule
  121. (schedule_name => 'SCHD_2008_1ST_WK_DAY_EACH_MTH'
  122. ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
  123. ,repeat_interval =>
  124. 'freq=MONTHLY;
  125. byday=MON,TUE,WED,THU,FRI;
  126. exclude=SCHD_2008_PUBLIC_HOLIDAYS;
  127. bysetpos=1'
  128. ,iteration => 12);
  129. show_schedule
  130. (schedule_name => 'SCHD_1ST_3RD_MON_EACH_MTH'
  131. ,start_date => to_timestamp_tz('03/12/2007 05:00:00','dd/mm/yyyy hh24:mi:ss')
  132. ,repeat_interval =>
  133. 'freq=MONTHLY;
  134. byday=1MON,3MON'
  135. ,iteration => 26);
  136. show_schedule
  137. (schedule_name => 'SCHD_LAST_DAY_EACH_QTR'
  138. ,start_date => to_timestamp_tz('31/12/2008 05:00:00','dd/mm/yyyy hh24:mi:ss')
  139. ,repeat_interval =>
  140. 'freq=YEARLY;
  141. bymonth=MAR,JUN,SEP,DEC;
  142. bymonthday=-1'
  143. ,iteration => 5);
  144. end;
  145. /
  146. set serverout off
  147. -- ---------------------
复制代码





event_based_jobs.sql:
  1. CONN sys/oracle AS SYSDBA
  2. DROP USER test CASCADE;

  3. CREATE USER test IDENTIFIED BY test QUOTA UNLIMITED ON USERS;
  4. GRANT CONNECT TO test;
  5. GRANT CREATE TABLE TO test;
  6. GRANT CREATE SEQUENCE TO test;
  7. GRANT CREATE TYPE TO test;
  8. GRANT AQ_ADMINISTRATOR_ROLE TO test;
  9. GRANT CREATE JOB TO test;


  10. CONN test/test
  11. DROP TABLE scheduler_test;
  12. DROP SEQUENCE scheduler_test_seq;

  13. CREATE TABLE scheduler_test (
  14.   id            NUMBER(10)    NOT NULL,
  15.   created_date  DATE          NOT NULL,
  16.   CONSTRAINT scheduler_test_pk PRIMARY KEY (id)
  17. );

  18. CREATE SEQUENCE scheduler_test_seq;


  19. CREATE OR REPLACE TYPE t_event_queue_payload AS OBJECT (
  20.   event_name  VARCHAR2(30)
  21. );
  22. /

  23. BEGIN
  24.   -- Create a queue table to hold the event queue.
  25.   DBMS_AQADM.create_queue_table(
  26.     queue_table        => 'event_queue_tab',
  27.     queue_payload_type => 't_event_queue_payload',
  28.     multiple_consumers => TRUE,
  29.     comment            => 'Queue Table For Event Messages');

  30.   -- Create the event queue.
  31.   DBMS_AQADM.create_queue (
  32.     queue_name  => 'event_queue',
  33.     queue_table => 'event_queue_tab');

  34.   -- Start the event queue.
  35.   DBMS_AQADM.start_queue (queue_name => 'event_queue');
  36. END;
  37. /

  38. BEGIN
  39.    DBMS_SCHEDULER.create_job (
  40.       job_name        => 'event_based_job',
  41.       job_type        => 'PLSQL_BLOCK',
  42.       job_action      => 'BEGIN
  43.                             INSERT INTO scheduler_test (id, created_date)
  44.                             VALUES (scheduler_test_seq.NEXTVAL, SYSDATE);
  45.                             COMMIT;
  46.                           END;',
  47.       start_date      => SYSTIMESTAMP,
  48.       event_condition => 'tab.user_data.event_name = ''give_me_a_prod''',
  49.       queue_spec      => 'event_queue',
  50.       enabled         => TRUE);
  51. END;
  52. /

  53. SELECT * FROM scheduler_test;

  54. DECLARE
  55.   l_enqueue_options     DBMS_AQ.enqueue_options_t;
  56.   l_message_properties  DBMS_AQ.message_properties_t;
  57.   l_message_handle      RAW(16);
  58.   l_queue_msg           t_event_queue_payload;
  59. BEGIN
  60.   l_queue_msg := t_event_queue_payload('give_me_a_prod');

  61.   DBMS_AQ.enqueue(queue_name          => 'event_queue',
  62.                   enqueue_options     => l_enqueue_options,
  63.                   message_properties  => l_message_properties,
  64.                   payload             => l_queue_msg,
  65.                   msgid               => l_message_handle);
  66.   COMMIT;
  67. END;
  68. /

  69. /*
  70. SELECT * FROM scheduler_test;

  71. -- Remove the job.
  72. EXEC DBMS_SCHEDULER.drop_job('event_based_job');
  73.   
  74. -- Stop the event queue.
  75. EXEC DBMS_AQADM.stop_queue (queue_name => 'event_queue');

  76. -- Drop the event queue.
  77. EXEC DBMS_AQADM.drop_queue (queue_name => 'event_queue');

  78. -- Remove the queue table.
  79. EXEC DBMS_AQADM.drop_queue_table(queue_table => 'event_queue_tab');

  80. DROP TYPE t_event_queue_payload;
  81. DROP TABLE scheduler_test;
  82. DROP SEQUENCE scheduler_test_seq;
  83. PURGE RECYCLEBIN;
  84. */
复制代码

04314chain.sql

  1. CREATE TABLE scheduler_test (

  2.   id            NUMBER(10)    NOT NULL,

  3.   description   VARCHAR2(20)  NOT NULL,

  4.   created_date  DATE          NOT NULL,

  5.   CONSTRAINT scheduler_test_pk PRIMARY KEY (id)

  6. );


  7. CREATE SEQUENCE scheduler_test_seq;

  8. BEGIN

  9.   DBMS_SCHEDULER.create_program (

  10.     program_name   => 'test_program_1',

  11.     program_type   => 'PLSQL_BLOCK',

  12.     program_action => 'BEGIN

  13.                          INSERT INTO scheduler_test (id, description, created_date)

  14.                          VALUES (scheduler_test_seq.NEXTVAL, ''test_program_1'', SYSDATE);

  15.                          COMMIT;

  16.                        END;',

  17.     enabled        => TRUE,

  18.     comments       => 'Program for first link in the chain.');



  19.   DBMS_SCHEDULER.create_program (

  20.     program_name   => 'test_program_2',

  21.     program_type   => 'PLSQL_BLOCK',

  22.     program_action => 'BEGIN

  23.                          INSERT INTO scheduler_test (id, description, created_date)

  24.                          VALUES (scheduler_test_seq.NEXTVAL, ''test_program_2'', SYSDATE);

  25.                          COMMIT;

  26.                        END;',

  27.     enabled        => TRUE,

  28.     comments       => 'Program for second link in the chain.');



  29.   DBMS_SCHEDULER.create_program (

  30.     program_name   => 'test_program_3',

  31.     program_type   => 'PLSQL_BLOCK',

  32.     program_action => 'BEGIN

  33.                          INSERT INTO scheduler_test (id, description, created_date)

  34.                          VALUES (scheduler_test_seq.NEXTVAL, ''test_program_3'', SYSDATE);

  35.                          COMMIT;

  36.                        END;',

  37.     enabled        => TRUE,

  38.     comments       => 'Program for last link in the chain.');

  39. END;


  40. CREATE OR REPLACE TYPE t_event_queue_payload AS OBJECT (

  41.   event_name  VARCHAR2(30)

  42. );



  43. BEGIN

  44.   -- Create a queue table to hold the event queue.

  45.   DBMS_AQADM.create_queue_table(

  46.     queue_table        => 'event_queue_tab',

  47.     queue_payload_type => 't_event_queue_payload',

  48.     multiple_consumers => TRUE,

  49.     comment            => 'Queue Table For Event Messages');



  50.   -- Create the event queue.

  51.   DBMS_AQADM.create_queue (

  52.     queue_name  => 'event_queue',

  53.     queue_table => 'event_queue_tab');



  54.   -- Start the event queue.

  55.   DBMS_AQADM.start_queue (queue_name => 'event_queue');

  56. END;



  57. BEGIN
  58. sys.dbms_scheduler.create_event_schedule(
  59. event_condition => 'tab.user_data.event_name = ''give_me_a_orange''',
  60. queue_spec => 'TEST.EVENT_QUEUE',
  61. start_date => systimestamp at time zone '+8:00',
  62. comments => 'schedule_event',
  63. schedule_name => '"TEST"."SCHEDULE_EVENT"');
  64. END;


  65. BEGIN

  66.   DBMS_SCHEDULER.create_chain (

  67.     chain_name          => 'test_chain_1',

  68.     rule_set_name       => NULL,

  69.     evaluation_interval => NULL,

  70.     comments            => 'A test chain.');

  71. END;




  72. SELECT

  73.        chain_name,

  74.        rule_set_owner,

  75.        rule_set_name,

  76.        number_of_rules,

  77.        number_of_steps,

  78.        enabled,

  79.        comments

  80. FROM   user_scheduler_chains;







  81. BEGIN
  82.   
  83.   DBMS_SCHEDULER.define_chain_event_step (

  84.     chain_name   => 'test_chain_1',

  85.     step_name    => 'chain_step_1',

  86.     event_schedule_name => 'TEST.SCHEDULE_EVENT');



  87.   DBMS_SCHEDULER.define_chain_step (

  88.     chain_name   => 'test_chain_1',

  89.     step_name    => 'chain_step_2',

  90.     program_name => 'test_program_1');



  91.   DBMS_SCHEDULER.define_chain_step (

  92.     chain_name   => 'test_chain_1',

  93.     step_name    => 'chain_step_3',

  94.     program_name => 'test_program_2');



  95.   DBMS_SCHEDULER.define_chain_step (

  96.     chain_name   => 'test_chain_1',

  97.     step_name    => 'chain_step_4',

  98.     program_name => 'test_program_3');

  99. END;






  100. BEGIN
  101.   
  102.   DBMS_SCHEDULER.define_chain_rule (
  103.   

  104.     chain_name => 'test_chain_1',

  105.     condition  => '1=1',

  106.     action     => 'START chain_step_1',

  107.     rule_name  => 'chain_rule_1',

  108.     comments   => 'First link in the chain.');







  109.   DBMS_SCHEDULER.define_chain_rule (
  110.   

  111.     chain_name => 'test_chain_1',

  112.     condition  => 'chain_step_1 completed',

  113.     action     => 'START chain_step_2',

  114.     rule_name  => 'chain_rule_2',

  115.     comments   => 'Second  link in the chain.');



  116.   DBMS_SCHEDULER.define_chain_rule (

  117.     chain_name => 'test_chain_1',

  118.     condition  => 'chain_step_2 completed',

  119.     action     => 'START chain_step_3',

  120.     rule_name  => 'chain_rule_3',

  121.     comments   => 'Third  link in the chain.');



  122.   DBMS_SCHEDULER.define_chain_rule (

  123.     chain_name => 'test_chain_1',

  124.     condition  => 'chain_step_3 completed',

  125.     action     => 'START chain_step_4',

  126.     rule_name  => 'chain_rule_4',

  127.     comments   => 'Fourth link in the chain.');



  128.   DBMS_SCHEDULER.define_chain_rule (

  129.     chain_name => 'test_chain_1',

  130.     condition  => 'chain_step_4 completed',

  131.     action     => 'END',

  132.     rule_name  => 'chain_rule_5',

  133.     comments   => 'End of the chain.');

  134. END;



  135. BEGIN
  136.   DBMS_SCHEDULER.ENABLE ('test_chain_1');
  137. END;





  138. SELECT *

  139. FROM   user_scheduler_running_chains;






  140. select  * from  scheduler_test;





  141. DECLARE

  142.   l_enqueue_options     DBMS_AQ.enqueue_options_t;

  143.   l_message_properties  DBMS_AQ.message_properties_t;

  144.   l_message_handle      RAW(16);

  145.   l_queue_msg           t_event_queue_payload;

  146. BEGIN

  147.   l_queue_msg := t_event_queue_payload('give_me_a_orange');



  148.   DBMS_AQ.enqueue(queue_name          => 'event_queue',

  149.                   enqueue_options     => l_enqueue_options,

  150.                   message_properties  => l_message_properties,

  151.                   payload             => l_queue_msg,

  152.                   msgid               => l_message_handle);

  153.   COMMIT;

  154. END;
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Bo's Oracle Station   

GMT+8, 2024-4-20 20:19 , Processed in 0.083002 second(s), 24 queries .

快速回复 返回顶部 返回列表