Bo's Oracle Station

查看: 2186|回复: 0

基于事件的schedule

[复制链接]

1005

主题

1469

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12012
发表于 2020-12-6 15:01:44 | 显示全部楼层 |阅读模式
  1. --SYS:
  2. 队列
  3. select  * from dba_queues   q  where  q.QUEUE_TYPE='NORMAL_QUEUE' ;
  4. select  * from dba_queues q where q.QUEUE_TYPE='EXCEPTION_QUEUE';
  5. select  * from dba_queues q where q.QUEUE_TYPE   not in ('NORMAL_QUEUE','EXCEPTION_QUEUE');

  6. 类型
  7. CREATE OR REPLACE TYPE t_event_queue_payload AS OBJECT (
  8.   event_name  VARCHAR2(30)
  9. );
  10. grant execute on t_event_queue_payload to hr;

  11. 需要AQ_ADMINISTRATOR_ROLE
  12. BEGIN
  13.   -- Create a queue table to hold the event queue.
  14.   DBMS_AQADM.create_queue_table(
  15.     queue_table        => 'event_queue_tab',
  16.     queue_payload_type => 't_event_queue_payload',
  17.     multiple_consumers => TRUE,
  18.     comment            => 'Queue Table For Event Messages');
  19.   -- Create the event queue.
  20.   DBMS_AQADM.create_queue (
  21.     queue_name  => 'event_queue',
  22.     queue_table => 'event_queue_tab');
  23.   -- Start the event queue.
  24.   DBMS_AQADM.start_queue (queue_name => 'event_queue');
  25. END;

  26. select  * from dba_queues   q  where  q.QUEUE_TYPE='NORMAL_QUEUE' ;
  27. select  * from dba_queues q where q.QUEUE_TYPE='EXCEPTION_QUEUE';
  28. select  * from dba_queues q where q.QUEUE_TYPE   not in ('NORMAL_QUEUE','EXCEPTION_QUEUE');

  29. begin
  30.    dbms_aqadm.grant_queue_privilege(privilege => 'ENQUEUE',queue_name => 'event_queue',grantee => 'HR');
  31. end;
  32.    
  33. begin
  34.    dbms_aqadm.grant_queue_privilege(privilege => 'DEQUEUE',queue_name => 'event_queue',grantee => 'HR');
  35. end;

  36. select  * from dba_tab_privs p  where p.grantee='HR';
  37. grant execute on dbms_aq to hr;
  38. grant create job to hr;

  39. --HR:
  40. DROP TABLE scheduler_test;
  41. DROP SEQUENCE scheduler_test_seq;
  42. CREATE TABLE scheduler_test (
  43.   id            NUMBER(10)    NOT NULL,
  44.   created_date  DATE          NOT NULL,
  45.   CONSTRAINT scheduler_test_pk PRIMARY KEY (id)
  46. );

  47. select  * from scheduler_test;
  48. CREATE SEQUENCE scheduler_test_seq;
复制代码

2020-12-06 15-04-59屏幕截图.png

  1. BEGIN
  2. sys.dbms_scheduler.create_event_schedule(
  3. event_condition => 'tab.user_data.event_name=''give_me_a_prod''',
  4. queue_spec => '"SYS"."EVENT_QUEUE"',
  5. start_date => systimestamp at time zone 'Asia/Shanghai',
  6. schedule_name => '"HR"."SCHEDULE4"');
  7. END;
复制代码
2020-12-06 15-10-11屏幕截图.png

  1.         
  2.                         
  3. BEGIN
  4. DBMS_SCHEDULER.CREATE_PROGRAM(
  5. program_name=>'"HR"."PROGRAM4"',
  6. program_action=>'BEGIN
  7.                             INSERT INTO scheduler_test (id, created_date)
  8.                             VALUES (scheduler_test_seq.NEXTVAL, SYSDATE);
  9.                             COMMIT;
  10.                           END;',
  11. program_type=>'PLSQL_BLOCK',
  12. number_of_arguments=>0,
  13. comments=>'',
  14. enabled=>TRUE);
  15. END;
复制代码
  1. BEGIN
  2. sys.dbms_scheduler.create_job(
  3. job_name => '"HR"."JOB4"',
  4. program_name => '"HR"."PROGRAM4"',
  5. schedule_name => '"HR"."SCHEDULE4"',
  6. job_class => '"DEFAULT_JOB_CLASS"',
  7. auto_drop => FALSE,
  8. enabled => TRUE);
  9. END;
复制代码

显式打ADT信号:
  1. DECLARE
  2.   l_enqueue_options     DBMS_AQ.enqueue_options_t;
  3.   l_message_properties  DBMS_AQ.message_properties_t;
  4.   l_message_handle      RAW(16);
  5.   l_queue_msg           sys.t_event_queue_payload;
  6. BEGIN
  7.   l_queue_msg := sys.t_event_queue_payload('give_me_a_prod');

  8.   DBMS_AQ.enqueue(queue_name          => 'sys.event_queue',
  9.                   enqueue_options     => l_enqueue_options,
  10.                   message_properties  => l_message_properties,
  11.                   payload             => l_queue_msg,
  12.                   msgid               => l_message_handle);
  13.   COMMIT;
  14. END;
复制代码
  1. SELECT * FROM scheduler_test order by id;
复制代码

--------------------------------
job chain:
  1. --hr:
  2. DROP TABLE scheduler_test;
  3. DROP SEQUENCE scheduler_test_seq;

  4. CREATE TABLE scheduler_test (
  5.   id            NUMBER(10)    NOT NULL,
  6.   description   VARCHAR2(20)  NOT NULL,
  7.   created_date  DATE          NOT NULL,
  8.   CONSTRAINT scheduler_test_pk PRIMARY KEY (id)
  9. );

  10. CREATE SEQUENCE scheduler_test_seq;

  11. BEGIN
  12.   DBMS_SCHEDULER.create_program (
  13.     program_name   => 'test_program_1',
  14.     program_type   => 'PLSQL_BLOCK',
  15.     program_action => 'BEGIN
  16.                          INSERT INTO scheduler_test (id, description, created_date)
  17.                          VALUES (scheduler_test_seq.NEXTVAL, ''test_program_1'', SYSDATE);
  18.                          COMMIT;
  19.                        END;',
  20.     enabled        => TRUE,
  21.     comments       => 'Step2');

  22.   DBMS_SCHEDULER.create_program (
  23.     program_name   => 'test_program_2',
  24.     program_type   => 'PLSQL_BLOCK',
  25.     program_action => 'BEGIN
  26.                          INSERT INTO scheduler_test (id, description, created_date)
  27.                          VALUES (scheduler_test_seq.NEXTVAL, ''test_program_2'', SYSDATE);
  28.                          COMMIT;
  29.                        END;',
  30.     enabled        => TRUE,
  31.     comments       => 'Step3');

  32.   DBMS_SCHEDULER.create_program (
  33.     program_name   => 'test_program_3',
  34.     program_type   => 'PLSQL_BLOCK',
  35.     program_action => 'BEGIN
  36.                          INSERT INTO scheduler_test (id, description, created_date)
  37.                          VALUES (scheduler_test_seq.NEXTVAL, ''test_program_3'', SYSDATE);
  38.                          COMMIT;
  39.                        END;',
  40.     enabled        => TRUE,
  41.     comments       => 'Step4');
  42. END;

  43. --SYS
  44. em做

  45. 如果HR本人,需要以下权限
  46. BEGIN
  47.   DBMS_RULE_ADM.grant_system_privilege(
  48.      privilege    => DBMS_RULE_ADM.create_rule_set_obj,
  49.      grantee      => 'HR',
  50.      grant_option => FALSE);

  51.   DBMS_RULE_ADM.grant_system_privilege(
  52.      privilege    => DBMS_RULE_ADM.create_evaluation_context_obj,
  53.      grantee      => 'HR',
  54.      grant_option => FALSE);

  55.   DBMS_RULE_ADM.grant_system_privilege(
  56.      privilege    => DBMS_RULE_ADM.create_rule_obj,
  57.      grantee      => 'HR',
  58.      grant_option => FALSE);
  59. END;

  60. 另外:
  61. analyze_chain:
  62. dbms_scheduler.analyze_chain(chain_name => ,rules => ,steps => ,step_pairs => );
  63. evaluate_running_chain:
  64. dbms_scheduler.evaluate_running_chain(job_name => );

  65. 规则制定:
  66. 每一条规则都要有“condition”和“action”。

  67. 如果condition为TRUE, the action执行。 Conditions are usually based on the outcome of one or more previous steps.
  68. A condition accepts Boolean and numeric integer values in an expression. The entire expression must evaluate to a Boolean value.

  69. The simplified syntax of a chain condition is as follows:
  70. 'factor|NOT(factor)[AND|OR factor]'

  71. factor:
  72. stepname ERROR_CODE number|[NOT]step_condition

  73. When creating a rule condition using the simplified syntax:
  74. You specify one or more factors, and a Boolean operator (AND, OR, or NOT).
  75. A factor can be either a simple Boolean value (TRUE or FALSE) or a chain condition. A chain condition describes the condition of another step in the job chain. You can use the following to describe the chain condition:
  76. The current state of the chain step:
  77. SUCCEEDED
  78. FAILED
  79. STOPPED
  80. COMPLETED
  81. The error code returned by the chain step. The error is a numeric value, and can be:
  82. Evaluated within a numeric clause
  83. Compared to a list of values using an IN clause
  84. You can use negative factors, by enclosing the factor in parentheses and prefixing the factor with the NOT operator.

  85. Examples:

  86. 'step1 SUCCEEDED AND step2 ERROR_CODE = 3'

  87. 'TRUE'

  88. 'step3 NOT COMPLETED AND NOT (step1 SUCCEEDED)'

  89. 'step2 ERROR_CODE NOT IN (1,2,3)'

  90. You can also refer to attributes of chain steps of the chain (this is called bind-variable syntax). The syntax is as follows:

  91. STEP_NAME.ATTRIBUTE

  92. Possible attributes are: completed, state, start_date, end_date, error_code, and duration.
  93. Possible values for the state attribute include: 'NOT_STARTED', 'SCHEDULED', 'RUNNING', 'PAUSED', 'SUCCEEDED', 'FAILED', and 'STOPPED'.
  94. If a step is in the state 'SUCCEEDED', 'FAILED', or 'STOPPED', its completed attribute is set to 'TRUE'; otherwise, completed is 'FALSE'.
  95. Some examples of the bind variable syntax are:

  96. ':step1.state=''SUCCEEDED'' and :step2.error_code=3'

  97. '1=1'

  98. ':step3.state != ''COMPLETED'''

  99. ':step2.error_code not in (1,2,3)'

  100. ':step1.state = ''NOT_STARTED'''

  101. The rule action specifies what is to be done as a result of the rule being triggered. A typical action is to run a specified step. Possible actions include:

  102. START step_1[,step_2...]
  103. STOP step_1[,step_2...]
  104. END [{end_value | step_name.error_code}]
  105. When the job starts and at the end of each step, all rules are evaluated to see what action or actions occur next. You can also configure rules to be evaluated at regular intervals by using the EVALUATION_INTERVAL attribute of the chain.

  106. You add a rule to a chain with the DEFINE_CHAIN_RULE procedure:

  107. BEGIN
  108. DBMS_SCHEDULER.DEFINE_CHAIN_RULE (
  109. CHAIN_NAME => 'bulk_load_chain',
  110. CONDITION => 'TRUE', -- starting step
  111. ACTION => 'START load_data_evt,stop_when_disk_full_evt',
  112. Rule_Name => 'dataload_rule1',
  113. COMMENTS => 'start the chain');

  114. DBMS_SCHEDULER.DEFINE_CHAIN_RULE (
  115. CHAIN_NAME => 'bulk_load_chain',
  116. CONDITION => 'load_data_evt COMPLETED',
  117. ACTION => 'START do_bulk_load',
  118. RULE_NAME => 'dataload_rule2');
  119. END;
  120. 又比如:
  121. BEGIN
  122.   DBMS_SCHEDULER.define_chain_rule (
  123.     chain_name => 'test_chain_1',
  124.     condition  => 'TRUE',
  125.     action     => 'START chain_step_1',
  126.     rule_name  => 'chain_rule_1',
  127.     comments   => 'First link in the chain.');

  128.   DBMS_SCHEDULER.define_chain_rule (
  129.     chain_name => 'test_chain_1',
  130.     condition  => 'chain_step_1 completed',
  131.     action     => 'START chain_step_2',
  132.     rule_name  => 'chain_rule_2',
  133.     comments   => 'Second link in the chain.');

  134.   DBMS_SCHEDULER.define_chain_rule (
  135.     chain_name => 'test_chain_1',
  136.     condition  => 'chain_step_2 completed',
  137.     action     => 'START chain_step_3',
  138.     rule_name  => 'chain_rule_3',
  139.     comments   => 'Third link in the chain.');

  140.   DBMS_SCHEDULER.define_chain_rule (
  141.     chain_name => 'test_chain_1',
  142.     condition  => 'chain_step_3 completed',
  143.     action     => 'END',
  144.     rule_name  => 'chain_rule_4',
  145.     comments   => 'End of the chain.');
  146. END;
  147. /

  148. SET LINESIZE 200
  149. COLUMN owner FORMAT A10
  150. COLUMN chain_name FORMAT A15
  151. COLUMN rule_owner FORMAT A10
  152. COLUMN rule_name FORMAT A15
  153. COLUMN condition FORMAT A25
  154. COLUMN action FORMAT A20
  155. COLUMN comments FORMAT A25

  156. SELECT owner,
  157.        chain_name,
  158.        rule_owner,
  159.        rule_name,
  160.        condition,
  161.        action,
  162.        comments
  163. FROM   dba_scheduler_chain_rules
  164. ORDER BY owner, chain_name, rule_owner, rule_name;
  165. /

  166. SET LINESIZE 200
  167. COLUMN owner FORMAT A10
  168. COLUMN job_name FORMAT A20
  169. COLUMN chain_owner FORMAT A10
  170. COLUMN chain_name FORMAT A15
  171. COLUMN step_name FORMAT A25

  172. SELECT owner,
  173.        job_name,
  174.        chain_owner,
  175.        chain_name,
  176.        step_name,
  177.        state
  178. FROM   dba_scheduler_running_chains
  179. ORDER BY owner, job_name, chain_name, step_name;

  180. BEGIN
  181.   DBMS_SCHEDULER.run_chain (
  182.     chain_name    =>  'test_chain_1',
  183.     job_name      =>  'test_chain_1_run_job',
  184.     start_steps   =>  'chain_step_1, chain_step_2, chain_step_3');
  185. END;
  186. /
  187. /*
  188. EXEC DBMS_SCHEDULER.drop_job(job_name => 'test_chain_1_job');

  189. EXEC DBMS_SCHEDULER.drop_chain (chain_name  => 'test_chain_1');

  190. EXEC DBMS_SCHEDULER.drop_program (program_name  => 'test_program_1');
  191. EXEC DBMS_SCHEDULER.drop_program (program_name  => 'test_program_2');
  192. EXEC DBMS_SCHEDULER.drop_program (program_name  => 'test_program_3');

  193. DROP TABLE scheduler_test;
  194. DROP SEQUENCE scheduler_test_seq;
  195. PURGE RECYCLEBIN;
  196. */
  197. --hr:
  198. DECLARE
  199.   l_enqueue_options     DBMS_AQ.enqueue_options_t;
  200.   l_message_properties  DBMS_AQ.message_properties_t;
  201.   l_message_handle      RAW(16);
  202.   l_queue_msg           sys.t_event_queue_payload;
  203. BEGIN
  204.   l_queue_msg := sys.t_event_queue_payload('give_me_a_prod');

  205.   DBMS_AQ.enqueue(queue_name          => 'sys.event_queue',
  206.                   enqueue_options     => l_enqueue_options,
  207.                   message_properties  => l_message_properties,
  208.                   payload             => l_queue_msg,
  209.                   msgid               => l_message_handle);
  210.   COMMIT;
  211. END;

  212. select  * from scheduler_test  order by id;
复制代码

2020-12-06 16-00-18屏幕截图.png


2020-12-06 16-02-56屏幕截图.png
回复

使用道具 举报

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

本版积分规则

QQ|手机版|Bo's Oracle Station   

GMT+8, 2024-11-21 22:40 , Processed in 0.039685 second(s), 27 queries .

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