|
3. 基于日历的schedule:
- BEGIN
- /* Complex scheduling example 1: Public Holidays */
- DBMS_SCHEDULER.CREATE_SCHEDULE
- (schedule_name => 'HR.SCHEDULE3'
- ,repeat_interval =>'freq=YEARLY;bydate=20190607,20190608,20190701'
- ,comments => 'XXX 2019'
- );
- END;
- BEGIN
- /* Complex scheduling example 2: Working Days */
- DBMS_SCHEDULER.CREATE_SCHEDULE
- (schedule_name => 'HR.SCHEDULE31'
- ,repeat_interval =>
- 'freq=YEARLY;byday=MON,TUE,WED,THU,FRI;'
- ,comments => 'Run on every working day'
- );
- END;
- declare
- v_1 timestamp;
- begin
- for i in 1..10
- loop
- DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING ( calendar_string=>'FREQ=yearly;bydate=0607,0608,0701;BYHOUR=14;BYMINUTE=10;BYSECOND=0;exclude=HR.SCHEDULE31' ,
- start_date => to_date('2015-01-01','YYYY-MM-DD'),
- return_date_after => v_1,
- next_run_date => v_1 );
- dbms_output.put_line(to_char(v_1,'YYYY-MM-DD:HH24:MI:SS'));
- end loop;
- end;
- BEGIN
- sys.dbms_scheduler.create_schedule(
- repeat_interval => 'FREQ=yearly;bydate=0607,0608,0701;BYHOUR=14;BYMINUTE=10;BYSECOND=0;exclude=HR.SCHEDULE31',
- start_date => systimestamp at time zone 'Asia/Shanghai',
- schedule_name => '"HR"."SCHEDULE32"');
- END;
- declare
- v_1 timestamp;
- begin
- for i in 1..30
- loop
- DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING (
- calendar_string=>'HR.SCHEDULE32' ,
- start_date => sysdate,
- return_date_after => v_1,
- next_run_date => v_1 );
- dbms_output.put_line(to_char(v_1,'YYYY-MM-DD:HH24:MI:SS'));
- end loop;
- end;
复制代码 4. 基于事件:
准备hr的表和SYS的管道:
- CREATE TABLE hr.scheduler_test (
- id NUMBER(10) NOT NULL,
- created_date DATE NOT NULL,
- event_name varchar2(40),
- CONSTRAINT scheduler_test_pk PRIMARY KEY (id)
- );
- select * from hr.scheduler_test;
- CREATE SEQUENCE hr.scheduler_test_seq;
- select * from dba_queues q where q.QUEUE_TYPE='NORMAL_QUEUE' ;
- select * from dba_queues q where q.QUEUE_TYPE='EXCEPTION_QUEUE';
- select * from dba_queues q where q.QUEUE_TYPE not in ('NORMAL_QUEUE','EXCEPTION_QUEUE');
- CREATE OR REPLACE TYPE t_event_queue_payload AS OBJECT (
- event_name VARCHAR2(30)
- );
- grant execute on t_event_queue_payload to hr;
- BEGIN
- -- Create a queue table to hold the event queue.
- DBMS_AQADM.create_queue_table(
- queue_table => 'event_queue_tab',
- queue_payload_type => 't_event_queue_payload',
- multiple_consumers => TRUE,
- comment => 'Queue Table For Event Messages');
- -- Create the event queue.
- DBMS_AQADM.create_queue (
- queue_name => 'event_queue',
- queue_table => 'event_queue_tab');
- -- Start the event queue.
- DBMS_AQADM.start_queue (queue_name => 'event_queue');
- END;
- select * from dba_queues q where q.QUEUE_TYPE='NORMAL_QUEUE' ;
- select * from dba_queues q where q.QUEUE_TYPE='EXCEPTION_QUEUE';
- begin
- dbms_aqadm.grant_queue_privilege(privilege => 'ENQUEUE',queue_name => 'event_queue',grantee => 'HR');
- end;
-
- begin
- dbms_aqadm.grant_queue_privilege(privilege => 'DEQUEUE',queue_name => 'event_queue',grantee => 'HR');
- end;
复制代码 接下来,准备定义一句ADT文本“give me a prod”, 并创建基于事件的schedule:
- BEGIN
- sys.dbms_scheduler.create_event_schedule(
- event_condition => 'tab.user_data.event_name = ''give_me_a_prod''',
- queue_spec => '"SYS"."EVENT_QUEUE"',
- start_date => systimestamp at time zone 'Asia/Shanghai',
- schedule_name => '"HR"."SCHEDULE4"');
- END;
复制代码 再接下来,做program:
- create or replace procedure hr.procjob4 ( p_1 sys.t_event_queue_payload )
- is
- BEGIN
- INSERT INTO hr.scheduler_test
- VALUES (hr.scheduler_test_seq.NEXTVAL, SYSDATE, p_1.event_name );
- COMMIT;
- END;
- begin
- dbms_scheduler.create_program (
- program_name => 'hr.program4',
- program_action=> 'hr.procjob4',
- program_type => 'STORED_PROCEDURE',
- number_of_arguments => 1,
- enabled => false) ;
- end;
- begin
- dbms_scheduler.define_metadata_argument (
- program_name => 'hr.program4',
- argument_position => 1 ,
- metadata_attribute => 'EVENT_MESSAGE') ;
- end;
- begin
- dbms_scheduler.enable ('hr.program4');
- end;
复制代码 最后组装job4:
- BEGIN
- sys.dbms_scheduler.create_job(
- job_name => '"HR"."JOB4"',
- program_name => '"HR"."PROGRAM4"',
- schedule_name => '"HR"."SCHEDULE4"',
- job_class => '"DEFAULT_JOB_CLASS"',
- comments => 'job4',
- auto_drop => FALSE,
- enabled => TRUE);
- END;
复制代码
模拟单片机,打消息:
- SELECT * FROM hr.scheduler_test order by id;
- DECLARE
- l_enqueue_options DBMS_AQ.enqueue_options_t;
- l_message_properties DBMS_AQ.message_properties_t;
- l_message_handle RAW(16);
- l_queue_msg sys.t_event_queue_payload;
- BEGIN
- l_queue_msg := sys.t_event_queue_payload('give_me_a_prod');
- DBMS_AQ.enqueue(queue_name => 'sys.event_queue',
- enqueue_options => l_enqueue_options,
- message_properties => l_message_properties,
- payload => l_queue_msg,
- msgid => l_message_handle);
- COMMIT;
- END;
复制代码
- BEGIN
- dbms_resource_manager.set_initial_consumer_group(
- user => 'HR',
- consumer_group => 'GROUP1'
- );
- END;
复制代码
- select s.USERNAME, s.RESOURCE_CONSUMER_GROUP
- from v_$session s
- where s.USERNAME='HR' and s.TERMINAL='pts/1';
复制代码 | USERNAME | RESOURCE_CONSUMER_GROUP | 1 | HR | GROUP1 |
高级scheduler概念:
- BEGIN
- sys.dbms_scheduler.create_job_class(
- logging_level => DBMS_SCHEDULER.LOGGING_RUNS,
- resource_consumer_group => 'GROUP2',
- comments => 'jobclass1',
- job_class_name => '"JOBCLASS1"');
- END;
复制代码- grant execute on jobclass1 to hr;
复制代码- BEGIN
- DBMS_SCHEDULER.CREATE_WINDOW(
- window_name=>'"WINDOW1"',
- resource_plan=>'PLAN1',
- start_date=>to_timestamp_tz('2018-08-14 21:30:00 Asia/Shanghai', 'YYYY-MM-DD HH24:MI:SS TZR'),
- duration=>numtodsinterval(1, 'minute'),
- repeat_interval=>null,
- end_date=>null,
- window_priority=>'HIGH',
- comments=>'window1');
- END;
复制代码- BEGIN
- DBMS_SCHEDULER.CREATE_WINDOW_GROUP(
- group_name=>'"WINGROUP1"',
- window_list=>'"WINDOW1"');
- END;
复制代码-
- [Return]
- BEGIN
- sys.dbms_scheduler.create_job(
- job_name => '"HR"."JOB7"',
- job_type => 'PLSQL_BLOCK',
- job_action => 'declare
- v1 number;
- begin
- select count(*) into v1 from t_big a , t_big b;
- end;',
- schedule_name => '"SYS"."WINGROUP1"',
- job_class => '"JOBCLASS1"',
- auto_drop => FALSE,
- enabled => FALSE);
- sys.dbms_scheduler.set_attribute( name => '"HR"."JOB7"', attribute => 'stop_on_window_close', value => FALSE);
- sys.dbms_scheduler.enable( '"HR"."JOB7"' );
- END;
复制代码- create table hr.t05317_lw( a timestamp ) ;
- create or replace procedure hr.proc05317_lw
- is
- v_1 number;
- begin
- select count(*) into v_1 from employees a, employees b;
- insert into t05317_lw values(systimestamp);
- commit;
- end;
- BEGIN
- DBMS_SCHEDULER.CREATE_PROGRAM(
- program_name=>'"HR"."PROGRAM8"',
- program_action=>'"HR"."PROC05317_LW"',
- program_type=>'STORED_PROCEDURE',
- number_of_arguments=>0,
- comments=>'',
- enabled=>TRUE);
- END;
- DECLARE
- newjob sys.job;
- newjobarr sys.job_array;
- BEGIN
- -- Create an array of JOB object types
- newjobarr := sys.job_array();
- -- Allocate sufficient space in the array
- newjobarr.extend(100);
- -- Add definitions for jobs
- FOR i IN 1..100 LOOP
- -- Create a JOB object type
- newjob := sys.job(job_name => 'HR.JOB8_' || to_char(i),
- job_style => 'LIGHTWEIGHT',
- job_template => 'HR.PROGRAM8',
- enabled => TRUE );
- -- Add job to the array
- newjobarr(i) := newjob;
- END LOOP;
- -- Call CREATE_JOBS to create jobs in one transaction
- DBMS_SCHEDULER.CREATE_JOBS(newjobarr, 'TRANSACTIONAL');
- END;
- select count(*) from hr.t05317_lw;
- select max(a) - min(a) from hr.t05317_lw;
复制代码
| | | |
Scheduler Jobs | |
Page Refreshed | | Aug 14, 2018 9:48:50 PM CST | | |
|
| Select | Status | Name | Schema | Completion Date | Run Duration (minutes) | | | JOB8_95 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_96 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_91 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_9 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_93 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_94 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_92 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_98 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_99 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_90 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_97 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_89 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_88 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_87 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_86 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_8 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_73 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_70 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_7 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_77 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_78 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_69 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_81 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_79 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 | | | JOB8_68 | HR | Aug 14, 2018 9:47:01 PM +08:00 | 0.0 |
|
Copyright © 1996, 2011, Oracle. All rights reserved.
Oracle, JD Edwards, PeopleSoft, and Retek are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
|
|
|
|