分类: oracle
2007-03-20 17:06:14
dbms_scheduler
) to replace and extend the functionality provided by the dbms_job
package.
jobs form the core of the functionality, but there are several other components available:create_program
procedure:notice how programs that accept arguments must have the arguments defined before they can be enabled.-- create the test programs.
begin
-- pl/sql block.
dbms_scheduler.create_program (
program_name => 'test_plsql_block_prog',
program_type => 'plsql_block',
program_action => 'begin dbms_stats.gather_schema_stats(''scott''); end;',
enabled => true,
comments => 'program to gather scott''s statistics using a pl/sql block.');
-- shell script.
dbms_scheduler.create_program (
program_name => 'test_executable_prog',
program_type => 'executable',
program_action => '/u01/app/oracle/dba/gather_scott_stats.sh',
number_of_arguments => 0,
enabled => true,
comments => 'program to gather scott''s statistics us a shell script.');
-- stored procedure with arguments.
dbms_scheduler.create_program (
program_name => 'test_stored_procedure_prog',
program_type => 'stored_procedure',
program_action => 'dbms_stats.gather_schema_stats',
number_of_arguments => 1,
enabled => false,
comments => 'program to gather scott''s statistics using a stored procedure.');
dbms_scheduler.define_program_argument (
program_name => 'test_stored_procedure_prog',
argument_name => 'ownname',
argument_position => 1,
argument_type => 'varchar2',
default_value => 'scott');
dbms_scheduler.enable (name => 'test_stored_procedure_prog');
end;
/
pl/sql procedure successfully completed.
-- display the program details.
select owner, program_name, enabled from dba_scheduler_programs;
owner program_name enabl
------------------------------ ------------------------------ -----
sys purge_log_prog true
sys gather_stats_prog true
sys test_plsql_block_prog true
sys test_executable_prog true
sys test_stored_procedure_prog true
5 rows selected.
drop_program
procedure:begin
dbms_scheduler.drop_program (program_name => 'test_plsql_block_prog');
dbms_scheduler.drop_program (program_name => 'test_stored_procedure_prog');
dbms_scheduler.drop_program (program_name => 'test_executable_prog');
end;
/
pl/sql procedure successfully completed.
-- display the program details.
select owner, program_name, enabled from dba_scheduler_programs;
owner program_name enabl
------------------------------ ------------------------------ -----
sys purge_log_prog true
sys gather_stats_prog true
2 rows selected.
create_schedule
procedure:notice how the interval is defined using the calendaring syntax. a schedule can be dropped using the-- create the schedule.
begin
dbms_scheduler.create_schedule (
schedule_name => 'test_hourly_schedule',
start_date => systimestamp,
repeat_interval => 'freq=hourly; byminute=0',
end_date => null,
comments => 'repeats hourly, on the hour, for ever.');
end;
/
pl/sql procedure successfully completed.
-- display the schedule details.
select owner, schedule_name from dba_scheduler_schedules;
owner schedule_name
------------------------------ ------------------------------
sys daily_purge_schedule
sys test_hourly_schedule
2 rows selected.
drop_schedule
procedure:begin
dbms_scheduler.drop_schedule (schedule_name => 'test_hourly_schedule');
end;
/
pl/sql procedure successfully completed.
-- display the schedule details.
select owner, schedule_name from dba_scheduler_schedules;
owner schedule_name
------------------------------ ------------------------------
sys daily_purge_schedule
1 row selected.
create_job
procedure is used to create them:jobs are normally run asynchronously under the control of the job coordinator, but they can be controlled manually using the-- create jobs.
begin
-- job defined entirely by the create job procedure.
dbms_scheduler.create_job (
job_name => 'test_full_job_definition',
job_type => 'plsql_block',
job_action => 'begin dbms_stats.gather_schema_stats(''scott''); end;',
start_date => systimestamp,
repeat_interval => 'freq=hourly; byminute=0',
end_date => null,
enabled => true,
comments => 'job defined entirely by the create job procedure.');
-- job defined by an existing program and schedule.
dbms_scheduler.create_job (
job_name => 'test_prog_sched_job_definition',
program_name => 'test_plsql_block_prog',
schedule_name => 'test_hourly_schedule',
enabled => true,
comments => 'job defined by an existing program and schedule.');
-- job defined by existing program and inline schedule.
dbms_scheduler.create_job (
job_name => 'test_prog_job_definition',
program_name => 'test_plsql_block_prog',
start_date => systimestamp,
repeat_interval => 'freq=hourly; byminute=0',
end_date => null,
enabled => true,
comments => 'job defined by existing program and inline schedule.');
-- job defined by existing schedule and inline program.
dbms_scheduler.create_job (
job_name => 'test_sched_job_definition',
schedule_name => 'test_hourly_schedule',
job_type => 'plsql_block',
job_action => 'begin dbms_stats.gather_schema_stats(''scott''); end;',
enabled => true,
comments => 'job defined by existing schedule and inline program.');
end;
/
pl/sql procedure successfully completed.
-- display job details.
select owner, job_name, enabled from dba_scheduler_jobs;
owner job_name enabl
------------------------------ ------------------------------ -----
sys purge_log true
sys gather_stats_job true
sys test_full_job_definition true
sys test_prog_sched_job_definition true
sys test_prog_job_definition true
sys test_sched_job_definition true
6 rows selected.
run_job
and
stop_job
procedures:jobs can be deleted using thebegin
-- run job synchronously.
dbms_scheduler.run_job (job_name => 'test_full_job_definition',
use_current_session => false);
-- stop jobs.
dbms_scheduler.stop_job (job_name => 'test_full_job_definition, test_prog_sched_job_definition');
end;
/
drop_job
procedure:begin
dbms_scheduler.drop_job (job_name => 'test_full_job_definition');
dbms_scheduler.drop_job (job_name => 'test_prog_sched_job_definition');
dbms_scheduler.drop_job (job_name => 'test_prog_job_definition');
dbms_scheduler.drop_job (job_name => 'test_sched_job_definition');
end;
/
pl/sql procedure successfully completed.
-- display job details.
select owner, job_name, enabled from dba_scheduler_jobs;
owner job_name enabl
------------------------------ ------------------------------ -----
sys purge_log true
sys gather_stats_job true
2 rows selected.
job_class
parameter
of the create_job
procedure is undefined the job is assigned to the default_job_class
. a job class is created using the
create_job_class
procedure:jobs can be assigned to a job class either during or after creation using the-- display the current resource consumer groups.
select consumer_group from dba_rsrc_consumer_groups;
consumer_group
------------------------------
other_groups
default_consumer_group
sys_group
low_group
auto_task_consumer_group
5 rows selected.
-- create a job class.
begin
dbms_scheduler.create_job_class (
job_class_name => 'test_job_class',
resource_consumer_group => 'low_group');
end;
/
pl/sql procedure successfully completed.
-- display job class details.
select job_class_name, resource_consumer_group from dba_scheduler_job_classes;
job_class_name resource_consumer_group
------------------------------ ------------------------------
default_job_class
auto_tasks_job_class auto_task_consumer_group
test_job_class low_group
3 rows selected.
set_attribute
procedure:job classes can be dropped usingbegin
-- job defined by an existing program and schedule and assigned toa job class.
dbms_scheduler.create_job (
job_name => 'test_prog_sched_class_job_def',
program_name => 'test_plsql_block_prog',
schedule_name => 'test_hourly_schedule',
job_class => 'test_job_class',
enabled => true,
comments => 'job defined by an existing program and schedule and assigned toa job class.');
dbms_scheduler.set_attribute (
name => 'test_prog_sched_job_definition',
attribute => 'job_class',
value => 'test_job_class');
end;
/
pl/sql procedure successfully completed.
-- display job details.
select owner, job_name, job_class, enabled from dba_scheduler_jobs;
owner job_name job_class enabl
------------------------------ ------------------------------ ------------------------------ -----
sys purge_log default_job_class true
sys gather_stats_job auto_tasks_job_class true
sys test_full_job_definition default_job_class true
sys test_prog_sched_job_definition test_job_class true
sys test_prog_job_definition default_job_class true
sys test_sched_job_definition default_job_class true
sys test_prog_sched_class_job_def test_job_class true
7 rows selected.
drop_job_class
procedure:the force option disables any dependent jobs and sets their job class to the default value. if the job class has no dependents the force option is not necessary.begin
dbms_scheduler.drop_job_class (
job_class_name => 'test_job_class',
force => true);
end;
/
pl/sql procedure successfully completed.
-- display job class details.
select job_class_name, resource_consumer_group from dba_scheduler_job_classes;
job_class_name resource_consumer_group
------------------------------ ------------------------------
default_job_class
auto_tasks_job_class auto_task_consumer_group
2 rows selected.
create_window
procedure with a predefined or an inline schedule:windows can be opened and closed manually using thebegin
-- window with a predefined schedule.
dbms_scheduler.create_window (
window_name => 'test_window_1',
resource_plan => null,
schedule_name => 'test_hourly_schedule',
duration => interval '60' minute,
window_priority => 'low',
comments => 'window with a predefined schedule.');
-- window with an inline schedule.
dbms_scheduler.create_window (
window_name => 'test_window_2',
resource_plan => null,
start_date => systimestamp,
repeat_interval => 'freq=hourly; byminute=0',
end_date => null,
duration => interval '60' minute,
window_priority => 'low',
comments => 'window with an inline schedule.');
end;
/
pl/sql procedure successfully completed.
-- display window group details.
select window_name, resource_plan, enabled, active
from dba_scheduler_windows;
window_name resource_plan enabl activ
------------------------------ ------------------------------ ----- -----
weeknight_window true false
weekend_window true false
test_window_1 true false
test_window_2 true false
4 rows selected.
open_window
and close_window
procedures:windows can be dropped using thebegin
-- open window.
dbms_scheduler.open_window (
window_name => 'test_window_2',
duration => interval '1' minute,
force => true);
end;
/
pl/sql procedure successfully completed.
-- display window group details.
select window_name, resource_plan, enabled, active
from dba_scheduler_windows;
window_name resource_plan enabl activ
------------------------------ ------------------------------ ----- -----
weeknight_window true false
weekend_window true false
test_window_1 true false
test_window_2 true true
4 rows selected.
begin
-- close window.
dbms_scheduler.close_window (
window_name => 'test_window_2');
end;
/
pl/sql procedure successfully completed.
-- display window group details.
select window_name, resource_plan, enabled, active
from dba_scheduler_windows;
window_name resource_plan enabl activ
------------------------------ ------------------------------ ----- -----
weeknight_window true false
weekend_window true false
test_window_1 true false
test_window_2 true false
4 rows selected.
drop_window
procedure:begin
dbms_scheduler.drop_window (
window_name => 'test_window_1',
force => true);
dbms_scheduler.drop_window (
window_name => 'test_window_2',
force => true);
end;
/
pl/sql procedure successfully completed.
-- display window group details.
select window_name, resource_plan, enabled, active
from dba_scheduler_windows;
window_name resource_plan enabl activ
------------------------------ ------------------------------ ----- -----
weeknight_window true false
weekend_window true false
2 rows selected.
create_window_group
procedure:windows can be added and removed from a group using thebegin
dbms_scheduler.create_window_group (
group_name => 'test_window_group',
window_list => 'test_window_1, test_window_2',
comments => 'a test window group');
end;
/
pl/sql procedure successfully completed.
-- display window group details.
select window_group_name, enabled, number_of_windows
from dba_scheduler_window_groups;
window_group_name enabl number_of_windows
------------------------------ ----- -----------------
maintenance_window_group true 2
test_window_group true 2
2 rows selected.
add_window_group_member
and remove_window_group_member
procedures:window groups can be dropped using thebegin
-- create a new window.
dbms_scheduler.create_window (
window_name => 'test_window_3',
resource_plan => null,
schedule_name => 'test_hourly_schedule',
duration => interval '60' minute,
window_priority => 'low',
comments => 'window with a predefined schedule.');
dbms_scheduler.add_window_group_member (
group_name => 'test_window_group',
window_list => 'test_window_3');
end;
/
pl/sql procedure successfully completed.
-- display window group members.
select window_group_name, window_name
from dba_scheduler_wingroup_members;
window_group_name window_name
------------------------------ ------------------------------
maintenance_window_group weeknight_window
maintenance_window_group weekend_window
test_window_group test_window_1
test_window_group test_window_2
test_window_group test_window_3
5 rows selected.
begin
dbms_scheduler.remove_window_group_member (
group_name => 'test_window_group',
window_list => 'test_window_3');
end;
/
pl/sql procedure successfully completed.
-- display window group members.
select window_group_name, window_name
from dba_scheduler_wingroup_members;
window_group_name window_name
------------------------------ ------------------------------
maintenance_window_group weeknight_window
maintenance_window_group weekend_window
test_window_group test_window_1
test_window_group test_window_2
4 rows selected.
drop_window_group
procedure:the force option must be used if the window group currently has members.begin
dbms_scheduler.drop_window_group (
group_name => 'test_window_group',
force => true);
end;
/
pl/sql procedure successfully completed.
-- display window group details.
select window_group_name, enabled, number_of_windows
from dba_scheduler_window_groups;
window_group_name enabl number_of_windows
------------------------------ ----- -----------------
maintenance_window_group true 2
1 row selected.
enable
and disable
procedures:the values for individual attributes of all scheduler objects can be altered using one of thebegin
-- enable programs and jobs.
dbms_scheduler.enable (name => 'test_stored_procedure_prog');
dbms_scheduler.enable (name => 'test_full_job_definition');
-- disable programs and jobs.
dbms_scheduler.disable (name => 'test_stored_procedure_prog');
dbms_scheduler.disable (name => 'test_full_job_definition');
end;
/
set_attribute
overloads:the values can be set to null using thebegin
dbms_scheduler.set_attribute (
name => 'hourly_schedule',
attribute => 'repeat_interval',
value => 'freq=hourly; byminute=30');
end;
/
set_attribute_null
procedure:for more information see:begin
dbms_scheduler.set_attribute_null (
name => 'hourly_schedule',
attribute => 'repeat_interval');
end;
/